-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLargest Square.cpp
84 lines (68 loc) · 1.76 KB
/
Largest Square.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
77
78
79
80
81
82
83
84
#include <bits/stdc++.h>
using namespace std;
char mp[100][100];
bool vis[100][100];
int dir[8][2]={{1,0},{-1,0},{0,1},{0,-1},{1,1},{-1,1},{1,-1},{-1,-1}};
int n,m,q;
bool check(int r,int c,char ch,int len){
int cnt=0;
for(int i=1;i<=len/2;i++){
if(r-i<0 || r+i>=m || c-i<0 || c+i>=n) continue;
if(mp[r-i][c-i]==mp[r][c] && mp[r+i][c-i]==mp[r][c] && mp[r-i][c+i]==mp[r][c] && mp[r+i][c+i]==mp[r][c] &&
mp[r][c-i]==mp[r][c] && mp[r+i][c-i]==mp[r][c] && mp[r][c+i]==mp[r][c] && mp[r-i][c]==mp[r][c]){
cnt++;
}
}
if(cnt==len/2) return true;
else return false;
}
int bfs(int xx,int yy,char ch){
queue< pair<int,int> > q;
vector<int> area;
int len=3,cnt=1;
q.push(make_pair(xx,yy));
vis[xx][yy]=true;
while(!q.empty()){
pair<int,int> cur,next;
cur=q.front();
q.pop();
for(int i=0;i<8;i++){
next.first = cur.first + dir[i][0];
next.second = cur.second + dir[i][1];
if(next.first<0 || next.first>=m || next.second<0 || next.second>=n) continue;
if(vis[next.first][next.second] || mp[next.first][next.second]!=ch) continue;
vis[next.first][next.second]=true;
q.push(next);
cnt++;
}
if(cnt==len*len && check(xx,yy,ch,len)) area.push_back(len);
else if(cnt>len*len) len+=2;
}
int M=1;
for(int i=0;i<area.size();i++){
if(M<area[i]) M=area[i];
}
return M;
}
int main(int argc, char** argv) {
cin.sync_with_stdio(0);
cin.tie(0);
int t;
cin>>t;
while(t--){
cin>>m>>n>>q;
cout << m << " " << n << " " << q << '\n';
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
cin>>mp[i][j];
}
}
while(q--){
int r,c;
memset(vis,false,sizeof(vis));
cin>>r>>c;
cout << bfs(r,c,mp[r][c]) << '\n';
}
}
return 0;
}