-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path10102.cpp
More file actions
70 lines (64 loc) · 1.6 KB
/
Copy path10102.cpp
File metadata and controls
70 lines (64 loc) · 1.6 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
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
int xx[4] = {-1, 0, 0, 1};
int yy[4] = {0, -1, 1, 0};
void print(vector<vector<char> > &mat)
{
for( int i = 0 ; i < mat.size() ; ++i )
{
for( int j = 0 ; j < mat[i].size() ; ++j )
{
cout<<mat[i][j];
}
cout<<endl;
}
}
int bfs(vector<vector<char> > mat, int i, int j) {
queue<pair<pair<int, int>, int> > q;
q.push(make_pair(make_pair(i, j), 0));
while (!q.empty()) {
int x = q.front().first.first;
int y = q.front().first.second;
int niv = q.front().second;
q.pop();
mat[x][y] = '0';
for (int i = 0; i < 4; ++i) {
int nextx = x + xx[i];
int nexty = y + yy[i];
if (nextx >= 0 && nexty >= 0 && nextx < mat.size() &&
nexty < mat[x].size() && mat[nextx][nexty] != '0') {
// cout<<x<<" "<<y<<" "<<nextx<<" "<<nexty<<endl;
// print(mat);
if (mat[nextx][nexty] == '3')
return niv + 1;
q.push(make_pair(make_pair(nextx, nexty), niv + 1));
}
}
}
return -1;
}
int main() {
int m;
while (scanf("%d", &m) != EOF) {
vector<vector<char> > mat(m, vector<char>(m));
vector<pair<int, int> > pp;
for (int i = 0; i < m; ++i) {
for (int j = 0; j < m; ++j) {
scanf(" %c", &mat[i][j]);
if (mat[i][j] == '1')
pp.push_back(make_pair(i, j));
}
}
// print(mat);
int may = -99999999;
for (int i = 0; i < pp.size(); ++i) {
int temp=bfs(mat,pp[i].first,pp[i].second);
// cout<<temp<<endl;
may = max(temp, may);
}
printf("%d\n", may);
}
return 0;
}