SW Expert Academy의 1249번 문제 보급로입니다. BFS를 진행하며 최소값을 각각의 배열에 저장하여 마지막 답을 출력하면 됩니다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | #include <cstdio> #include <queue> using namespace std; #define inf 100000; int n; int a[101][101] = { 0, }; int d[101][101]; int dx[] = { 0,0,-1,1 }; int dy[] = { -1,1,0,0 }; queue<pair<int, int>>q; int main() { int T; scanf("%d", &T); for (int test_case = 1; test_case <= T; test_case++) { scanf("%d", &n); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { scanf("%1d", &a[i][j]); d[i][j] = inf; } } d[0][0] = 0; q.push({ 0,0 }); while (!q.empty()) { int x = q.front().first; int y = q.front().second; q.pop(); for (int dir = 0; dir < 4; dir++) { int nx = x + dx[dir]; int ny = y + dy[dir]; if (nx >= 0 && nx < n&&ny >= 0 && ny < n) { int temp = d[x][y] + a[nx][ny]; if (temp < d[nx][ny]) { d[nx][ny] = temp; q.push({ nx,ny }); } } } } int ans = d[n - 1][n - 1]; printf("#%d %d\n", test_case, ans); } } | cs |
'알고리즘 > BFS 문제풀이' 카테고리의 다른 글
[B7562] - 나이트의 이동 (0) | 2018.08.30 |
---|---|
[B5014] - 스타트링크 (0) | 2018.08.26 |
[S1953] - [모의 SW 역량테스트] 탈주범검거 (0) | 2018.08.25 |
[B14442]-벽 부수고 이동하기2 (0) | 2018.08.24 |
[B2206]-벽 부수고 이동하기 (0) | 2018.08.24 |