-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy path1383Labyrinth.cpp
More file actions
56 lines (55 loc) · 1.37 KB
/
1383Labyrinth.cpp
File metadata and controls
56 lines (55 loc) · 1.37 KB
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
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<vector>
#include<utility>
#include<queue>
#include<limits.h>
using namespace std;
typedef pair<int,int> PII;
int k;
int n,m;
char labyrinth[1000][1010];
PII start;
int dist[1000][1000];
int bfs(PII s){
queue<PII> Q;
memset(dist,-1,sizeof dist);
dist[s.first][s.second] = 0;
Q.push(s);
int ans = -1;
while(!Q.empty()){
PII head = Q.front();
Q.pop();
int x = head.first, y = head.second;
int d = dist[x][y];
if(d>ans) ans = d, start = make_pair(x,y);
int nx,ny;
for(int dx=-1;dx<=1;dx++)
for(int dy=-1;dy<=1;dy++)
if((dx==0)^(dy==0)){
nx = x+dx, ny = y+dy;
if(nx<0 || nx>=n || ny<0 || ny>=m) continue;
if(labyrinth[nx][ny]!='.') continue;
if(dist[nx][ny]!=-1) continue;
dist[nx][ny] = d+1;
Q.push(make_pair(nx,ny));
}
}
return ans;
}
int main()
{
scanf("%d",&k);
while(k--){
scanf("%d%d",&m,&n);
for(int i=0;i<n;i++)
scanf("%s",labyrinth[i]);
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
if(labyrinth[i][j]=='.') start = make_pair(i,j);
bfs(start);
printf("Maximum rope length is %d.\n",bfs(start));
}
return 0;
}