-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlb.cpp
More file actions
executable file
·103 lines (92 loc) · 1.98 KB
/
lb.cpp
File metadata and controls
executable file
·103 lines (92 loc) · 1.98 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
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
#include <bits/stdc++.h>
using namespace std;
#define mod 1000000007
#define int long long int
#define pb push_back
#define mk make_pair
#define flp(i, k, n) for(int i=k; i<n; i++)
#define F first
#define S second
#define SIZE 1005
int dx[4] = {-1,1,0,0};
int dy[4] = {0,0,-1,1};
int n, m;
char grid[SIZE][SIZE];
bool vis[SIZE][SIZE];
int dist[SIZE][SIZE];
int len=0;
pair<int, int> diamEnd;
bool check(int x, int y)
{
if(x<0 || y<0 || x>=n || y>=m)
return false;
else
return true;
}
void bfs(int x, int y)
{
vis[x][y]=true;
queue<pair<int, int>> q;
q.push({x, y});
while(!q.empty())
{
pair<int, int> u=q.front(), next;
q.pop();
flp(k, 0, 4)
{
int x=u.F+dx[k];
int y=u.S+dy[k];
if(check(x, y))
{
if(grid[x][y]=='.' && !vis[x][y])
{
q.push({x,y});
dist[x][y]=1+dist[u.F][u.S];
vis[x][y]=true;
if(len<dist[x][y])
{
len=dist[x][y];
diamEnd={x,y};
}
}
}
}
}
}
int32_t main(void)
{
int t;
cin>>t;
while(t--)
{
scanf("%lld%lld", &m, &n);
pair<int, int> start;
len=0;
int present=0;
memset(vis, 0, sizeof(vis));
flp(i, 0, n)
scanf("%s", grid[i]);
flp(i, 0, n)
{
flp(j, 0, n)
{
if(grid[i][j]=='.')
{
start={i, j};
dist[i][j]=0;
present=1;
break;
}
}
if(present)
break;
}
bfs(start.F, start.S);
memset(vis, 0, sizeof(vis));
dist[diamEnd.F][diamEnd.S]=0;
len=0;
bfs(diamEnd.F, diamEnd.S);
printf("Maximum rope length is %lld.\n",len);
}
return 0;
}