Submission #00218
ソースコード
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 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 | #include<bits/stdc++.h> using namespace std; int main(){ int h,w;cin>>h>>w; string s[h]; for ( int i=0;i<h;i++){ cin>>s[i]; } vector<vector< int >>t(h,vector< int >(w,999)); vector<vector< int >>t2(h,vector< int >(w,999)); pair< int , int >st,go; for ( int i=0;i<h;i++){ for ( int j=0;j<w;j++){ if (s[i][j]== 'S' ){ st=make_pair(i,j); t[i][j]=0; s[i][j]= '.' ; } else if (s[i][j]== 'G' ){ go=make_pair(i,j); s[i][j]= '.' ; } } } queue<pair<pair< int , int >, int >>q; q.push(make_pair(st,0)); pair<pair< int , int >, int >now; while (!q.empty()){ now=q.front(); q.pop(); int x=now.first.first,y=now.first.second,f=now.second; if (f==0){ if (t[x][y]<t2[x][y]){ q.push(make_pair(make_pair(x,y),1)); t2[x][y]=t[x][y]; } if (s[x][y]== '.' ){ if (x-1>=0&&s[x-1][y]!= '#' &&t[x-1][y]>t[x][y]+1){ q.push(make_pair(make_pair(x-1,y),0)); t[x-1][y]=t[x][y]+1; } if (x+1<h&&s[x+1][y]!= '#' &&t[x+1][y]>t[x][y]+1){ q.push(make_pair(make_pair(x+1,y),0)); t[x+1][y]=t[x][y]+1; } if (y-1>=0&&s[x][y-1]!= '#' &&t[x][y-1]>t[x][y]+1){ q.push(make_pair(make_pair(x,y-1),0)); t[x][y-1]=t[x][y]+1; } if (y+1<w&&s[x][y+1]!= '#' &&t[x][y+1]>t[x][y]+1){ q.push(make_pair(make_pair(x,y+1),0)); t[x][y+1]=t[x][y]+1; } } else if (s[x][y]== 'U' ){ if (x-1>=0&&s[x-1][y]!= '#' &&t[x-1][y]>t[x][y]+1){ q.push(make_pair(make_pair(x-1,y),0)); t[x-1][y]=t[x][y]+1; } if (x+1<h&&s[x+1][y]!= '#' &&t2[x+1][y]>t[x][y]+1){ q.push(make_pair(make_pair(x+1,y),1)); t2[x+1][y]=t[x][y]+1; } if (y-1>=0&&s[x][y-1]!= '#' &&t2[x][y-1]>t[x][y]+1){ q.push(make_pair(make_pair(x,y-1),1)); t2[x][y-1]=t[x][y]+1; } if (y+1<w&&s[x][y+1]!= '#' &&t2[x][y+1]>t[x][y]+1){ q.push(make_pair(make_pair(x,y+1),1)); t2[x][y+1]=t[x][y]+1; } } else if (s[x][y]== 'D' ){ if (x-1>=0&&s[x-1][y]!= '#' &&t2[x-1][y]>t[x][y]+1){ q.push(make_pair(make_pair(x-1,y),1)); t2[x-1][y]=t[x][y]+1; } if (x+1<h&&s[x+1][y]!= '#' &&t[x+1][y]>t[x][y]+1){ q.push(make_pair(make_pair(x+1,y),0)); t[x+1][y]=t[x][y]+1; } if (y-1>=0&&s[x][y-1]!= '#' &&t2[x][y-1]>t[x][y]+1){ q.push(make_pair(make_pair(x,y-1),1)); t2[x][y-1]=t[x][y]+1; } if (y+1<w&&s[x][y+1]!= '#' &&t2[x][y+1]>t[x][y]+1){ q.push(make_pair(make_pair(x,y+1),1)); t2[x][y+1]=t[x][y]+1; } } else if (s[x][y]== 'L' ){ if (x-1>=0&&s[x-1][y]!= '#' &&t2[x-1][y]>t[x][y]+1){ q.push(make_pair(make_pair(x-1,y),1)); t2[x-1][y]=t[x][y]+1; } if (x+1<h&&s[x+1][y]!= '#' &&t2[x+1][y]>t[x][y]+1){ q.push(make_pair(make_pair(x+1,y),1)); t2[x+1][y]=t[x][y]+1; } if (y-1>=0&&s[x][y-1]!= '#' &&t[x][y-1]>t[x][y]+1){ q.push(make_pair(make_pair(x,y-1),0)); t[x][y-1]=t[x][y]+1; } if (y+1<w&&s[x][y+1]!= '#' &&t2[x][y+1]>t[x][y]+1){ q.push(make_pair(make_pair(x,y+1),1)); t2[x][y+1]=t[x][y]+1; } } else if (s[x][y]== 'R' ){ if (x-1>=0&&s[x-1][y]!= '#' &&t2[x-1][y]>t[x][y]+1){ q.push(make_pair(make_pair(x-1,y),1)); t2[x-1][y]=t[x][y]+1; } if (x+1<h&&s[x+1][y]!= '#' &&t2[x+1][y]>t[x][y]+1){ q.push(make_pair(make_pair(x+1,y),1)); t2[x+1][y]=t[x][y]+1; } if (y-1>=0&&s[x][y-1]!= '#' &&t2[x][y-1]>t[x][y]+1){ q.push(make_pair(make_pair(x,y-1),1)); t2[x][y-1]=t[x][y]+1; } if (y+1<w&&s[x][y+1]!= '#' &&t[x][y+1]>t[x][y]+1){ q.push(make_pair(make_pair(x,y+1),0)); t[x][y+1]=t[x][y]+1; } } } else { if (s[x][y]== '.' ){ if (x-1>=0&&s[x-1][y]!= '#' &&t2[x-1][y]>t2[x][y]+1){ q.push(make_pair(make_pair(x-1,y),1)); t2[x-1][y]=t2[x][y]+1; } if (x+1<h&&s[x+1][y]!= '#' &&t2[x+1][y]>t2[x][y]+1){ q.push(make_pair(make_pair(x+1,y),1)); t2[x+1][y]=t2[x][y]+1; } if (y-1>=0&&s[x][y-1]!= '#' &&t2[x][y-1]>t2[x][y]+1){ q.push(make_pair(make_pair(x,y-1),1)); t2[x][y-1]=t2[x][y]+1; } if (y+1<w&&s[x][y+1]!= '#' &&t2[x][y+1]>t2[x][y]+1){ q.push(make_pair(make_pair(x,y+1),1)); t2[x][y+1]=t2[x][y]+1; } } else if (s[x][y]== 'U' ){ if (x-1>=0&&s[x-1][y]!= '#' &&t2[x-1][y]>t2[x][y]+1){ q.push(make_pair(make_pair(x-1,y),1)); t2[x-1][y]=t2[x][y]+1; } } else if (s[x][y]== 'D' ){ if (x+1<h&&s[x+1][y]!= '#' &&t2[x+1][y]>t2[x][y]+1){ q.push(make_pair(make_pair(x+1,y),1)); t2[x+1][y]=t2[x][y]+1; } } else if (s[x][y]== 'L' ){ if (y-1>=0&&s[x][y-1]!= '#' &&t2[x][y-1]>t2[x][y]+1){ q.push(make_pair(make_pair(x,y-1),1)); t2[x][y-1]=t2[x][y]+1; } } else if (s[x][y]== 'R' ){ if (y+1<w&&s[x][y+1]!= '#' &&t2[x][y+1]>t2[x][y]+1){ q.push(make_pair(make_pair(x,y+1),1)); t2[x][y+1]=t2[x][y]+1; } } } } if (t[go.first][go.second]==999&&t2[go.first][go.second]==999){ cout<<-1<< "\n" ; return (0); } cout<<min(t[go.first][go.second],t2[go.first][go.second])<< "\n" ; } |
ステータス
項目 | データ |
---|---|
問題 | 0008 - 一方通行迷路ゲーム |
ユーザー名 | ei2326 |
投稿日時 | 2023-08-28 11:56:46 |
言語 | C++17 |
状態 | Wrong Answer |
得点 | 0 |
ソースコード長 | 6879 Byte |
最大実行時間 | 35 ms |
最大メモリ使用量 | 1548 KB |
セット
セット | 得点 | Cases | |
---|---|---|---|
1 | ALL | 0 / 30 | * |
テストケース
ファイル名 | 状態 | 実行時間 | メモリ使用量 | # |
---|---|---|---|---|
in1 | AC | 18 ms | 604 KB |
1
|
in2 | AC | 21 ms | 560 KB |
1
|
in3 | AC | 27 ms | 544 KB |
1
|
in4 | AC | 28 ms | 640 KB |
1
|
in5 | AC | 17 ms | 732 KB |
1
|
in6 | AC | 21 ms | 576 KB |
1
|
in7 | AC | 19 ms | 680 KB |
1
|
in8 | AC | 26 ms | 780 KB |
1
|
in9 | AC | 22 ms | 928 KB |
1
|
in10 | AC | 26 ms | 820 KB |
1
|
in11 | AC | 18 ms | 464 KB |
1
|
in12 | AC | 21 ms | 644 KB |
1
|
in13 | AC | 25 ms | 724 KB |
1
|
in14 | AC | 23 ms | 1548 KB |
1
|
in15 | AC | 30 ms | 1536 KB |
1
|
in16 | AC | 31 ms | 1520 KB |
1
|
in17 | AC | 23 ms | 1376 KB |
1
|
in18 | AC | 24 ms | 1360 KB |
1
|
in19 | AC | 23 ms | 1344 KB |
1
|
in20 | AC | 32 ms | 1424 KB |
1
|
in21 | AC | 31 ms | 1408 KB |
1
|
in22 | WA | 25 ms | 1396 KB |
1
|
in23 | WA | 31 ms | 1384 KB |
1
|
in24 | WA | 35 ms | 1376 KB |
1
|
in25 | AC | 23 ms | 1360 KB |
1
|
in26 | WA | 18 ms | 1480 KB |
1
|
in27 | WA | 17 ms | 1464 KB |
1
|
in28 | AC | 22 ms | 1452 KB |
1
|