The simple thing to say is the loop inside the BFS ....
OJ 1196 ...
#include <iostream>#include<string>#include<cstring>#include<cstring>#include<cstring>using namespacestd;intdx[9],dy[9];inttop=0;structww{intX,y,c,v;} q[10000000];intbig[1000002],small[1000002];intf[1001][1001]={};intmap[1001][1001];intMain () {intans=0, bns=0; dx[1]=1; dy[1]=0; dx[2]=1; dy[2]=1; dx[3]=1; dy[3]=-1; dx[4]=0; dy[4]=1; dx[5]=0; dy[5]=-1; dx[6]=-1; dy[6]=0; dx[7]=-1; dy[7]=1; dx[8]=-1; dy[8]=-1; intN; CIN>>N; intw=0; for(intI=1; i<=n;i++) for(intu=1; u<=n;u++) Cin>>Map[i][u]; intHead=1; for(intI=1; i<=n;i++) for(intu=1; u<=n;u++) { if(f[i][u]==0) {F[i][u]=1; q[++top].x=i,q[top].y=u,q[top].c=++w,q[top].v=Map[i][u]; for(; head<=top;head++) { intx, y; for(intI=1; i<=8; i++) { if(i>8) Break; X=q[head].x+dx[i],y=q[head].y+Dy[i]; if(x<=0|| y<=0){Continue;} if(x>n| | Y>n) {Continue;} if(MAP[X][Y]<Q[HEAD].V) big[w]=1; if(MAP[X][Y]>Q[HEAD].V) {small[w]=1;} if(map[x][y]==q[head].v&&f[x][y]==0) {f[x][y]=1, q[++top].x=x,q[top].y=y,q[top].v=Q[HEAD].V;} } } if(big[w]==1&&small[w]==0) {ans++;} if(big[w]==0&&small[w]==1) {bns++;} if(big[w]==0&&small[w]==0) {ans++,bns++;} }} cout<<ans<<' '<<bns<<Endl; return 0;}
View Code
The question seems quite simple ... But I've been playing for almost two hours ...
Well, actually, it's because there's no train of thought ...
So I played 3 versions of it.
Originally wanted to use DFS to check how many rings (because of the feeling that BFS check ring plus judgment will contradict (Hao Ba jiu shi zi ji tai ruo)) ....
Dfs after the fight suddenly found that BFS is not contradictory .....
Then the Danteng is the wrong direction array ... Staring at the most genius to find out ...
It is also worth noting that: Continue is a suffix operation that can perform for.
Well.. That's it
On the number of rings obtained by BFS