Submission #00015
ソースコード
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 70 71 72 73 74 | #include<bits/stdc++.h> using namespace std; int dy[] = {-1,0,1,0}; int dx[] = {0,-1,0,1}; int n; int w,h; char maps[2000][2000]; int check[2000][2000]; int sy,sx; int ans; char goal; typedef struct { int y,x,cnt; }P; void bfs(){ queue<P> que; check[sy][sx] = 1; que.push((P){sy,sx,0}); while (!que.empty()){ P p = que.front();que.pop(); int y = p.y; int x = p.x; int cnt = p.cnt; if (maps[y][x] == goal){ sy = y; sx = x; goal++; ans += cnt; return ; } for ( int i = 0;i < 4;i++){ int ny = y + dy[i]; int nx = x + dx[i]; if (ny >= 0 && ny < h && nx >= 0 && nx < w && check[ny][nx] != 1 && maps[ny][nx] != 'X' ){ check[ny][nx] = 1; que.push((P){ny,nx,cnt + 1}); } } } return ; } int main(){ cin >> h >> w >> n; for ( int i = 0;i < h;i++){ for ( int j = 0;j < w;j++){ cin >> maps[i][j]; if (maps[i][j] == 'S' ){ sy = i; sx = j; } } } goal = '1' ; for ( int i = 0;i < n;i++){ memset (check,-1, sizeof (check)); bfs(); } cout << ans << endl; } |
ステータス
項目 | データ |
---|---|
問題 | 0005 - チーズ(Cheese) |
ユーザー名 | ei1501 |
投稿日時 | 2016-11-09 16:47:46 |
言語 | C++11 |
状態 | Accepted |
得点 | 100 |
ソースコード長 | 1186 Byte |
最大実行時間 | 106 ms |
最大メモリ使用量 | 18972 KB |
セット
セット | 得点 | Cases | |
---|---|---|---|
1 | in1 | 20 / 20 | in1.txt |
2 | in2 | 20 / 20 | in2.txt |
3 | in3 | 20 / 20 | in3.txt |
4 | in4 | 20 / 20 | in4.txt |
5 | in5 | 20 / 20 | in5.txt |
テストケース
ファイル名 | 状態 | 実行時間 | メモリ使用量 | # | ||||
---|---|---|---|---|---|---|---|---|
in1.txt | AC | 22 ms | 16864 KB |
1
|
||||
in2.txt | AC | 23 ms | 16848 KB |
2
|
||||
in3.txt | AC | 27 ms | 16956 KB |
3
|
||||
in4.txt | AC | 106 ms | 18856 KB |
4
|
||||
in5.txt | AC | 88 ms | 18972 KB |
5
|