http://acm.timus.ru/problem.aspx?space=1&num=1250
最外层加上一层‘#’ 然后将island 和 sea 一层层的分离 如果某一层向外接触的 fragment 全是被诅咒的 那么这种被诅咒是会传递到这一层的
代码:
#include<iostream> #include<cstdio> #include<cstring> #include<string> #include<map> #include<vector> #include<stack> #include<set> #include<map> #include<queue> #include<algorithm> #include<cmath> #define LL long long #define sint short int //#pragma comment(linker, "/STACK:1024000000,1024000000") using namespace std; const int N=505; char s[N][N]; bool visited[N][N]; int sacred[N][N]; int X[]={0,0,-1,1,-1,-1,1,1}; int Y[]={-1,1,0,0,-1,1,-1,1}; int n,m,sx,sy; int ans; queue<int>qtx,qty; stack<int>stx,sty; void bfs(int x1,int y1) { int I=8; if(s[x1][y1]=='#') I=4; int flag = 1; if(x1==0&&y1==0) flag=-1; qtx.push(x1);qty.push(y1); stx.push(x1);sty.push(y1); visited[x1][y1]=true; while(!qtx.empty()) { int x=qtx.front();qtx.pop(); int y=qty.front();qty.pop(); for(int i=0;i<I;++i) { int l1=x+X[i]; int l2=y+Y[i]; if(l1>=0&&l1<=n+1&&l2>=0&&l2<=m+1) { if(visited[l1][l2]&&sacred[l1][l2]==-1) flag=-1; if(visited[l1][l2]||s[l1][l2]!=s[x][y]) continue; visited[l1][l2]=true; qtx.push(l1); qty.push(l2); stx.push(l1); sty.push(l2); } } } if(flag==1&&I==4) ++ans; if(x1==sx&&y1==sy) flag=1; while(!stx.empty()) { int l1=stx.top();stx.pop(); int l2=sty.top();sty.pop(); sacred[l1][l2]=flag; } } int main() { //freopen("data.in","r",stdin); while(cin>>m>>n>>sy>>sx) { getchar(); for(int i=1;i<=n;++i) gets(s[i]+1); memset(visited,false,sizeof(visited)); memset(sacred,0,sizeof(sacred)); ans=0; for(int j=0;j<=m+1;++j) s[0][j]=s[n+1][j]='#'; for(int i=0;i<=n+1;++i) s[i][0]=s[i][m+1]='#'; bfs(0,0); bfs(sx,sy); for(int i=1;i<=n;++i) for(int j=1;j<=m;++j) if(!visited[i][j]) bfs(i,j); cout<<ans<<endl; } return 0; }