Bzoj4395[usaco2015 dec]switching on the Lights
Test instructions
N*n a room, the cow is initially in (a), and can only be active in the bright room. Every time a cow passes a room, it can open the room to control the other room lights switch. Ask the cows how many rooms can be lit up. N≤100.
Exercises
Because as long as a room lights up, it will always be on, so you can do BFS, each time by the node in the queue to expand the node can be. This is not possible, however, because the lights that may have been tried before can be opened in the room where they arrived. The solution is to keep BFS until the answer is no longer updated.
Code:
1#include <cstdio>2#include <cstring>3#include <algorithm>4#include <queue>5 #defineInc (I,J,K) for (int i=j;i<=k;i++)6 #defineMAXN 1107 using namespacestd;8 9InlineintRead () {Ten CharCh=getchar ();intf=1, x=0; One while(ch<'0'|| Ch>'9'){if(ch=='-') f=-1; Ch=GetChar ();} A while(ch>='0'&&ch<='9') x=x*Ten+ch-'0', ch=GetChar (); - returnf*x; - } the structnd{intX,y,n;} nds[maxn* $];intG[MAXN][MAXN]; - intN,m,ans;BOOLLG[MAXN][MAXN],VIS[MAXN][MAXN]; queue<pair<int,int> >Q; - voidBFsintXinty) { - while(!q.empty ()) Q.pop (); memset (Vis,0,sizeof(VIS)); Q.push (Make_pair (x, y)); vis[x][y]=1; + for(intI=G[X][Y];I;I=NDS[I].N)if(!lg[nds[i].x][nds[i].y]) lg[nds[i].x][nds[i].y]=1, ans++; - while(!Q.empty ()) { + intNowx=q.front (). first,nowy=Q.front (). second; Q.pop (); A if(nowx>1&&lg[nowx-1][nowy]&&!vis[nowx-1][nowy]) { atQ.push (Make_pair (nowx-1, Nowy)); vis[nowx-1][nowy]=1; - for(inti=g[nowx-1][nowy];i;i=NDS[I].N) - if(!lg[nds[i].x][nds[i].y]) lg[nds[i].x][nds[i].y]=1, ans++; - } - if(nowx<n&&lg[nowx+1][nowy]&&!vis[nowx+1][nowy]) { -Q.push (Make_pair (nowx+1, Nowy)); vis[nowx+1][nowy]=1; in for(inti=g[nowx+1][nowy];i;i=NDS[I].N) - if(!lg[nds[i].x][nds[i].y]) lg[nds[i].x][nds[i].y]=1, ans++; to } + if(nowy>1&&lg[nowx][nowy-1]&&!vis[nowx][nowy-1]){ -Q.push (Make_pair (nowx,nowy-1)); vis[nowx][nowy-1]=1; the for(inti=g[nowx][nowy-1];i;i=NDS[I].N) * if(!lg[nds[i].x][nds[i].y]) lg[nds[i].x][nds[i].y]=1, ans++; $ }Panax Notoginseng if(nowy<n&&lg[nowx][nowy+1]&&!vis[nowx][nowy+1]){ -Q.push (Make_pair (nowx,nowy+1)); vis[nowx][nowy+1]=1; the for(inti=g[nowx][nowy+1];i;i=NDS[I].N) + if(!lg[nds[i].x][nds[i].y]) lg[nds[i].x][nds[i].y]=1, ans++; A } the } + } - intMain () { $N=read (); m=read (); $Inc (I,1, M) { - intA=read (), B=read (), C=read (), D=read (); nds[i]= (nd) {c,d,g[a][b]}; g[a][b]=i; - } theans=1; lg[1][1]=1; - while(1){intX=ans; BFs1,1);if(ans==x) Break;} printf"%d", ans);return 0;Wuyi}
20160908
Bzoj4395[usaco2015 dec]switching on the lights*