-
Notifications
You must be signed in to change notification settings - Fork 0
/
BFS with adjacency matrix.cpp
76 lines (71 loc) · 1.39 KB
/
BFS with adjacency matrix.cpp
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
//Problem link:http://acm.timus.ru/problem.aspx?space=1&num=1145
#include <iostream>
#include <cstdio>
#include <utility>
#include <cmath>
#include <queue>
#include <stack>
#include <vector>
#include <algorithm>
#include <string>
#include <cstring>
#define rep(i,n) for(int i=0; i<(int)n; i++)
#define pb(x) push_back(x)
#define mem(x,y) memset(x,y,sizeof(x));
#define pii pair<int,int>
#define pmp make_pair
#define uu first
#define vv second
using namespace std;
#define READ(f) freopen(f, "r", stdin)
#define WRITE(f) freopen(f, "w", stdout)
int fx[]={1,-1,0,0};
int fy[]={0,0,1,-1};
int mr,mc,mx=0;
char w[1000][1000];
int d[1000][1000];
int r,c;
void bfs(int x,int y,int dep)
{
mem(d,63);
d[x][y]=0;
queue<pii>q;
q.push(pii(x,y));
while(!q.empty())
{
pii top=q.front(); q.pop();
if(d[top.uu][top.vv]>mx)
{
mx=d[top.uu][top.vv];
mr=top.uu;
mc=top.vv;
}
rep(k,4)
{
int tx=top.uu+fx[k];
int ty=top.vv+fy[k];
if(tx>=0 and tx<r and ty>=0 and ty<c and w[tx][ty]=='.' and d[top.uu][top.vv]+1<d[tx][ty])
{
d[tx][ty]=d[top.uu][top.vv]+1;
q.push(pii(tx,ty));
}
}
}
}
int main(){
// READ("in");
int sx,sy,cc=0;
cin>>r>>c;
swap(r,c);
rep(i,r)
cin>>w[i];
rep(i,r)
rep(j,c)
if(w[i][j]=='.'){sx=i;sy=j;cc++;}
bfs(sx,sy,0);
mx=0;
bfs(mr,mc,0);
if(cc==1) mx=0;
cout<<mx<<endl;
return 0;
}