提出 #64545743
ソースコード 拡げる
#include <iostream>
#include <vector>
#include <algorithm>
#include <climits>
#include <queue>
using namespace std;
#define int long long
void dfs(vector<vector<char>>& arr, int i, int j) {
int n = arr.size(), m = arr[0].size();
if(i < 0 || j < 0 || i >= n || j >= m
|| arr[i][j] == '#' || arr[i][j] == 'T') return;
arr[i][j] = 'T';
dfs(arr, i+1, j);
dfs(arr, i-1, j);
dfs(arr, i, j+1);
dfs(arr, i, j-1);
}
int bfs(vector<vector<char>>& arr, int dx, int dy) {
queue<pair<int, pair<int,int>>> q;
q.push({0, {dx, dy}});
vector<int> dirx{-1, 0, 1, 0}, diry{0, -1, 0, 1};
int n = arr.size(), m = arr[0].size();
vector<vector<bool>> vis(n, vector<bool>(m, false));
int ans = LLONG_MAX;
while(!q.empty()) {
int i = q.front().second.first, j = q.front().second.second;
int dist = q.front().first;
// cout<<i<<' '<<j<<' '<<dist<<' '<<arr[i][j]<<endl;
// if(arr[i][j] == 'T') return ceil(dist/2);
q.pop();
for(int k=0;k<4;k++) {
int ti = dirx[k] + i, tj = diry[k] + j;
if(ti < 0 || tj < 0 || ti >= n || tj >= m || vis[ti][tj]) continue;
vis[ti][tj] = true;
if(arr[ti][tj] == 'T') {
ans = min((dist + 1) / 2, ans);
continue;
}
if(arr[ti][tj] == '#') q.push({dist + 1, {ti, tj}});
else q.push({dist, {ti, tj}});
}
}
return ans;
}
signed main() {
int n, m, sx, sy, dx, dy; cin>>n>>m;
vector<vector<char>> arr(n, vector<char>(m));
for(int i=0;i<n;i++) {
for(int j=0;j<m;j++) cin>>arr[i][j];
}
cin>>sx>>sy>>dx>>dy;
sx--, sy--, dx--, dy--;
dfs(arr, sx, sy);
cout<<bfs(arr, dx, dy)<<endl;
return 0;
}
提出情報
ジャッジ結果
| セット名 |
Sample |
All |
| 得点 / 配点 |
0 / 0 |
0 / 400 |
| 結果 |
|
|
| セット名 |
テストケース |
| Sample |
example_00.txt, example_01.txt, example_02.txt, example_03.txt |
| All |
example_00.txt, example_01.txt, example_02.txt, example_03.txt, hand_00.txt, hand_01.txt, hand_02.txt, hand_03.txt, hand_04.txt, hand_05.txt, hand_06.txt, hand_07.txt, hand_08.txt, hand_09.txt, random_00.txt, random_01.txt, random_02.txt, random_03.txt, random_04.txt, random_05.txt, random_06.txt, random_07.txt, random_08.txt, random_09.txt, random_10.txt, random_11.txt, random_12.txt, random_13.txt, random_14.txt, random_15.txt, random_16.txt, random_17.txt, random_18.txt, random_19.txt |
| ケース名 |
結果 |
実行時間 |
メモリ |
| example_00.txt |
AC |
1 ms |
3620 KiB |
| example_01.txt |
AC |
1 ms |
3452 KiB |
| example_02.txt |
AC |
1 ms |
3532 KiB |
| example_03.txt |
AC |
1 ms |
3544 KiB |
| hand_00.txt |
AC |
48 ms |
4896 KiB |
| hand_01.txt |
WA |
47 ms |
4732 KiB |
| hand_02.txt |
AC |
47 ms |
4796 KiB |
| hand_03.txt |
AC |
47 ms |
4704 KiB |
| hand_04.txt |
WA |
48 ms |
4748 KiB |
| hand_05.txt |
AC |
47 ms |
4712 KiB |
| hand_06.txt |
WA |
47 ms |
4692 KiB |
| hand_07.txt |
AC |
45 ms |
16416 KiB |
| hand_08.txt |
AC |
70 ms |
51496 KiB |
| hand_09.txt |
AC |
1 ms |
3500 KiB |
| random_00.txt |
WA |
45 ms |
4688 KiB |
| random_01.txt |
WA |
44 ms |
4676 KiB |
| random_02.txt |
WA |
48 ms |
4652 KiB |
| random_03.txt |
WA |
47 ms |
4740 KiB |
| random_04.txt |
AC |
43 ms |
4556 KiB |
| random_05.txt |
AC |
43 ms |
4588 KiB |
| random_06.txt |
AC |
56 ms |
14244 KiB |
| random_07.txt |
AC |
62 ms |
15780 KiB |
| random_08.txt |
AC |
60 ms |
24736 KiB |
| random_09.txt |
AC |
50 ms |
10880 KiB |
| random_10.txt |
AC |
54 ms |
16872 KiB |
| random_11.txt |
AC |
47 ms |
6768 KiB |
| random_12.txt |
WA |
50 ms |
4596 KiB |
| random_13.txt |
WA |
50 ms |
4772 KiB |
| random_14.txt |
WA |
47 ms |
4620 KiB |
| random_15.txt |
WA |
52 ms |
4712 KiB |
| random_16.txt |
WA |
52 ms |
4804 KiB |
| random_17.txt |
WA |
44 ms |
4764 KiB |
| random_18.txt |
WA |
45 ms |
4696 KiB |
| random_19.txt |
WA |
46 ms |
4672 KiB |