Every time you arrive at a point, or light a room, check to see if the dots around it can go.
At first I read the wrong question. What is required is the maximum number of rooms to open the lamp.
1#include <cstdio>2#include <iostream>3#include <cstring>4#include <cmath>5#include <algorithm>6 using namespacestd;7 Const intmaxn=10023;8 Const intxx[4]={0,0,1,-1},yy[4]={1,-1,0,0};9 structzs{intToo,pre;} e[20012];intTOT,LAST[MAXN];Ten intDLX[MAXN],DLY[MAXN]; One intid[103][103],X[MAXN],Y[MAXN]; A BOOLcon[103][103],can[103][103]; - intI,j,k,n,m,l,r; - intans; the - intRaCharRx; -InlineintRead () { -Rx=getchar (), ra=0; + while(rx<'0'|| Rx>'9') rx=GetChar (); - while(rx>='0'&&rx<='9') ra*=Ten, ra+=rx- -, Rx=getchar ();returnRA; + } AInlinevoidInsertintAintb) {e[++tot].too=b,e[tot].pre=last[a],last[a]=tot;} atInlinevoidRunintNxintNY) { - intx, y; - for(intI=0;i<4; i++){ -x=nx+xx[i],y=ny+Yy[i]; - if(x<1|| y<1|| x>n| | Y>n)Continue; - if(!con[x][y]&&can[x][y]) r++,dlx[r]=x,dly[r]=y; incon[x][y]=1; - } to } + intMain () { -N=read (), M=read ();intA,b,c,d,cnt=0; the for(i=1; i<=n;i++) for(j=1; j<=n;j++) id[i][j]=++cnt,x[cnt]=i,y[cnt]=J; * for(i=1; i<=m;i++) A=read (), B=read (), C=read (), d=read (), insert (Id[a][b],id[c][d]); $L=0, r=1;Panax Notoginsengdlx[1]=dly[1]=con[1][1]=can[1][1]=1; - while(l<R) { thel++; + for(i=last[id[dlx[l]][dly[l]]];i;i=e[i].pre) { A if(!can[x[e[i].too]][y[e[i].too]]&&Con[x[e[i].too]][y[e[i].too]]) ther++,dlx[r]=x[e[i].too],dly[r]=Y[e[i].too]; +can[x[e[i].too]][y[e[i].too]]=1; - } $ run (dlx[l],dly[l]); $ } - for(i=1; i<=n;i++) for(j=1; j<=n;j++)if(Can[i][j]) ans++; -printf"%d\n", ans); the}View Code
4395: [Usaco2015 dec]switching on the Lights