/* 미로 탐색 */ #include <cstdio> #include <iostream> #include <queue> using namespace std; int n, m; bool map[100][100]; int check[100][100]; int dir[4][2] = { {1,0}, {-1,0}, {0,1}, {0,-1} }; struct points { int x, y; }; void problemIn() { cin >> n >> m; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { int b; scanf("%1d", &b); if (b == 1) { map[i][j] = true; } } } } int bfs() { int cur_y = 0; int cur_x = 0; queue<points> que; que.push({ cur_x, cur_y }); check[cur_x][cur_y] = 1; while (!que.empty()) { cur_x = que.front().x; cur_y = que.front().y; que.pop(); if ((cur_x == n) && (cur_x == m)) break; for (int i = 0; i < 4; i++) { int next_x = cur_x + dir[i][0]; int next_y = cur_y + dir[i][1]; if (next_x >= 0 && next_y >= 0 && next_x < n && next_y < m) { if (check[next_x][next_y] == 0 && map[next_x][next_y]) { check[next_x][next_y] = check[cur_x][cur_y] + 1; que.push({ next_x, next_y }); } } } } return check[n - 1][m - 1]; } int main(void) { problemIn(); cout << bfs() << endl; return 0; } |
한자리씩 받고 싶을때 %1d이렇게 받도록 합니다. 오께이요. 그리고 여기서는 브레이크 문을 만나는 조건에 대해서 한번 파악해보도록 하겠습니다. 그리고 이전의체크와 현재 체크를 비교했는데, 그게 아니고 지나간적이 있으면, 안좋습니다. 지나간적이 있는 곳으로 되돌아가는 것은 마이너스 입니다. 오께이요. 그리고 없어도 되는 조건이라던지, 이런것들은 제외하도록 합니다. 그럼 아까랑 다른 것은 단순하게 전의 것 비교 이것만 있는 것 같습니다. 음음... 코드를 짜고 무엇이 틀렸는지 이제 나의 코드에서 수정해 나가는 것을 찾도록 합니다. 이거 남의 것을 보고 수정하는 것은 쉬워도 만들어내는 것은 아직 익숙하지 않습니다. |