회원가입
로그인
Toggle navigation
문제
문제
전체 문제
문제 출처
단계별로 풀어보기
알고리즘 분류
추가된 문제
문제 순위
문제
푼 사람이 한 명인 문제
아무도 못 푼 문제
최근 제출된 문제
최근 풀린 문제
랜덤
출처
ICPC
Olympiad
한국정보올림피아드
한국정보올림피아드시․도지역본선
전국 대학생 프로그래밍 대회 동아리 연합
대학교 대회
카카오 코드 페스티벌
Coder's High
ICPC
Regionals
World Finals
Korea Regional
Africa and the Middle East Regionals
Europe Regionals
Latin America Regionals
North America Regionals
South Pacific Regionals
문제집
대회
1
채점 현황
랭킹
게시판
그룹
더 보기
재채점 기록
블로그
강의
실험실
도움말
BOJ Stack
BOJ Book
전체
공지
자유
질문
오타/오역/요청
게시판 공지
홍보
업데이트
solved.ac
글쓰기
질문 도움말
자주묻는 질문
정말 모르겠습니다..ㅠㅠㅠ
3190번 - 뱀
wlsdnr929
2년 전
0
아무리 봐도 왜 틀린지 모르겠습니다.. 도와주세요 고수님들 ㅠㅠ
#include <iostream> #include <vector> #include <queue> using namespace std; // 우 하 좌 상 int dy[4] = { 0,1,0,-1 }; int dx[4] = { 1,0,-1,0 }; int board[101][101]; pair<int, int> turn(char c,int now_dir, int now_y,int now_x) { vector<pair<int, int>> cur; int next_y, next_x; if (c == 'D') // 우 { next_y = now_y + dy[now_dir + 1]; next_x = now_x + dx[now_dir + 1]; return make_pair(next_y, next_x); } else // 좌 { next_y = now_y + dy[now_dir - 1]; next_x = now_x + dx[now_dir - 1]; return make_pair(next_y, next_x); } } int main(void) { int n; cin >> n; // 보드 크기 int m; cin >> m; // 사과 개수 int a, b; for (int i = 0; i < m; i++) { cin >> a >> b; board[a][b] = 2; // 사과를 2로 표현 } int l; cin >> l; vector<pair<int, char>> info; int c; char d; for (int i = 0; i < l; i++) { cin >> c >> d; info.push_back({ c,d }); } int now_y = 1, now_x = 1; int now_dir = 0; int time_cnt = 0; int dir_cnt = info.size(); int next_y, next_x; queue<pair<int, int>> spot; spot.push(make_pair(now_y, now_x)); int info_idx = 0; board[now_y][now_x] = 1; while (1) { // 방향 바꿀 것이 남아있고, 방향 바꿀 시간이면 if ((dir_cnt > 0) && (time_cnt == info[info_idx].first)) { pair<int, int> cur; cur = turn(info[info_idx].second, now_dir, now_y, now_x); next_y = cur.first; next_x = cur.second; if (info[info_idx].second == 'D') now_dir = (now_dir + 1) % 4; else now_dir = (now_dir == 0) ? 3 : now_dir - 1; // ------> 처음에 now_dir --; // 이렇게 쓰면 문장 끝나고 나서 now_dir 값 줄어듦 info_idx++; dir_cnt--; } else { next_y = now_y + dy[now_dir]; next_x = now_x + dx[now_dir]; } // 다음 칸이 벽이거나 몸통이면 if (next_y > n || next_x < 1 || next_y<1 || next_x > n || board[next_y][next_x] == 1) { time_cnt++; break; } // 다음칸에 사과있으면 if (board[next_y][next_x] == 2) { spot.push({ next_y,next_x }); board[next_y][next_x] = 1; } else // 없으면 { spot.push(make_pair(next_y, next_x)); board[next_y][next_x] = 1; board[spot.front().first][spot.front().second] = 0; spot.pop(); } now_y = next_y; now_x = next_x; time_cnt++; } cout << time_cnt; return 0; }
댓글을 작성하려면
로그인
해야 합니다.
wlsdnr929 2년 전
아무리 봐도 왜 틀린지 모르겠습니다.. 도와주세요 고수님들 ㅠㅠ