注意事項:國王代表的點是陸地,其他都是水!!!
我是用DFS,其實也可以用BFS
#include <iostream>
#include <stdio.h>
#include <cstring>
using namespace std;
const int MAX_N = 23;
char s[MAX_N][MAX_N];
int mp[MAX_N][MAX_N];
int dfs(int x,int y,int n,int m) {
mp[x][y] = 0;
int dx[4] = {0,0,1,-1},dy[4] = {1,-1,0,0};
int ret=1;
for (int i=0;4>i;i++) {
int tx=(x + dx[i]), ty=(m+y+dy[i]) % m;
if (0<=tx && tx<n && mp[tx][ty] == 1) {
mp[tx][ty] = 0;
ret += dfs(tx,ty,n,m);
}
}
return ret;
}
int main () {
int n,m;
while (scanf("%d %d",&n,&m) != EOF) {
memset(mp,0,sizeof(mp));
getchar();
for (int x=0;n>x;x++) {
scanf("%s",s[x]);
}
int sx,sy;
scanf("%d %d",&sx,&sy);
for (int x=0;n>x;x++) {
for (int y=0;m>y;y++) {
mp[x][y] = (s[x][y] == s[sx][sy] ? 1 : 0);
}
}
dfs(sx,sy,n,m);
int ans=0;
for (int x=0;n>x;x++) {
for (int y=0;m>y;y++) {
if (mp[x][y] == 1) ans = max(ans,dfs(x,y,n,m));
}
}
printf("%d\n",ans);
}
}
沒有留言:
張貼留言