# Untitled

user_5379400
plain_text
a month ago
1.3 kB
4
Indexable
Never
```#include<iostream>
using namespace std;
const int mod = 1e9 + 7;

int n, m, sx, sy, ex, ey;
int a[111][111];

bool vis[111][111];

void rs(){
for (int i = 0;  i < n; i++)
for (int j = 0; j < m; j++)
vis[i][j] = 0;
}

void dfs(int x, int y, int h){
vis[x][y] = true;
if (y + 1 < m && !vis[x][y + 1] && a[x][y + 1]){
dfs(x, y + 1, h);
}
if (y - 1 >= 0 && !vis[x][y - 1] && a[x][y - 1]){
dfs(x, y - 1, h);
}
for (int nx = x - h; nx <= x + h; nx++){
if (nx >= 0 && nx < n && !vis[nx][y] && a[nx][y]){
dfs(nx, y, h);
}
}
}

void solve(int test){
cin >> n >> m;
for (int i = 0; i < n; i++){
for (int j = 0; j < m; j++){
cin >> a[i][j];
if (a[i][j] == 2) sx = i, sy = j;
if (a[i][j] == 3) ex = i, ey = j;
}
}
int l = 0, r = 55, ans = -1;
while(r >= l){
int mid = (l + r) / 2;
rs();
dfs(sx, sy, mid);
if (vis[ex][ey]){
ans = mid;
r = mid - 1;
}
else l = mid + 1;
}
cout << "Case #" << test << '\n' << ans << '\n';

}

int32_t main(){
// ios_base::sync_with_stdio(false), cin.tie(0);
// freopen("file1.txt", "r", stdin);
int t = 1;
cin >> t;
for (int i = 1; i <= t; i++) solve(i);

}```