-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path[모의 SW 역량테스트] 보급로.txt
69 lines (58 loc) · 1.7 KB
/
[모의 SW 역량테스트] 보급로.txt
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
//보급로 [S/W 문제해결 응용] 4일차 - 보급로
//그냥 visit을 번호화 시킬것
#include <iostream>
#include <cstdio>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
typedef struct cell{
int r, c;
int count;
}Cell;
int rdir[4] = {-1,0,1,0};
int cdir[4] = {0,1,0,-1};
int result = 9999999;
int arr[101][101];
int visit[101][101];
int n;
int main(){
int test = 0;
scanf("%d", &test);
for(int t = 1; t <=test; t++){
scanf("%d", &n);
for(int i =0;i<n; i++){
string temp;
cin >> temp;
for(int j=0;j<n; j++){
arr[i][j] = temp[j] -'0';
visit[i][j] = -1;
}
}
queue<Cell> root;
root.push({0,0, arr[0][0]});
visit[0][0] = arr[0][0];
while(!root.empty()){
Cell temp = root.front();
root.pop();
if(temp.r == n-1 && temp.c == n-1){
result = min(result, temp.count);
}
else{
for(int i =0;i<4; i++){
int nowR = temp.r + rdir[i];
int nowC = temp.c + cdir[i];
if(0<=nowR && nowR<n && 0<=nowC && nowC<n){
if(visit[nowR][nowC] == -1 ||(visit[nowR][nowC] > temp.count + arr[nowR][nowC])){
visit[nowR][nowC] = temp.count + arr[nowR][nowC];
root.push({nowR, nowC, temp.count + arr[nowR][nowC]});
}
}
}
}
}
printf("#%d %d\n", t, result);
result = 9999999;
}
return 0;
}