Title Link: Wandering Robots
Solution: First of all, for each lattice it can be from the number of places to have a value (from themselves to themselves), and then the answer is to count the number of legal lattice and the ratio of the number of all the lattice
For example, the value on the 3 0 lattice of the sample is
3 4 3
4 5 4
3 4 3
The answer is 22/33 =2/3; the next is how to count the answer, because the figure 1e4*1e4 but the point only 1e3 must be discretized to get a new graph, discretization is to add points adjacent to the two lines are also added to the new diagram, so that the omitted points in the original image adjacent to the point is not blocked, and then Dfs once, Mark one side of the point that can be reached, and then count the answers to note the boundary.
#include <bits/stdc++.h>#definell Long Longusing namespacestd;Const intn=3e3+Ten;BOOLVis[n][n],mp[n][n];intdx[]={0,1,0,-1};intdy[]={1,0,-1,0};intn,k,mm,mn;intsc[n],sr[n],cn1,cn2;intX[n],y[n];intX_hash (intx) { returnLower_bound (sc,sc+mn,x)-SC;}intY_hash (inty) { returnLower_bound (Sr,sr+mm,y)-SR;}voidDfsintXinty) {Vis[x][y]=1; for(intI=0;i<4; i++) { inttx=x+dx[i],ty=y+Dy[i]; if(tx>=0&&tx<mn&&ty>=0&&ty<mm&&!vis[tx][ty]&&!Mp[tx][ty]) DFS (tx,ty); }}int Get(intXinty) { if(!vis[x][y])return 0; intCnt=Vis[x][y]; for(intI=0;i<4; i++) { inttx=x+dx[i],ty=y+Dy[i]; if(tx>=0&&tx<mn&&ty>=0&&TY<MM) cnt+=Vis[tx][ty]; } returnCNT;}intMain () {intT; scanf ("%d",&T); intcas=1; while(t--) {cn1=0; cn2=0; //memset (vis,0,sizeof (VIS)); //memset (Mp,0,sizeof (MP));scanf"%d%d",&n,&k); SC[CN1++]=0; sc[cn1++]=n-1; SR[CN2++]=0; sr[cn2++]=n-1; for(intI=0; i<k;i++) {scanf ("%d%d",&x[i],&Y[i]); SC[CN1++]=x[i];if(x[i]-1>=0) sc[cn1++]=x[i]-1;if(x[i]+1<n) sc[cn1++]=x[i]+1; SR[CN2++]=y[i];if(y[i]-1>=0) sr[cn2++]=y[i]-1;if(y[i]+1<n) sr[cn2++]=y[i]+1; } sort (Sc,sc+CN1); Sort (sr,sr+cn2); MN=unique (SC,SC+CN1)-sc;mm=unique (SR,SR+CN2)-SR; for(intI=0; i<=mn;i++) { for(intj=0; j<=mm;j++) {Vis[i][j]=mp[i][j]=0; } } for(intI=0; i<k;i++) {Mp[x_hash (X[i])][y_hash (Y[i])=1; } DFS (0,0); intan1=0, an2=0; for(intI=0; i<mn;i++) { if(i!=0) {An1+ = (sc[i-1]+1+sc[i]+1) * (sc[i]-sc[i-1]-1)*5/2; An1-=sc[i]-sc[i-1]-1; } for(intj=0; j<mm;j++) { if(sr[j]+sc[i]>=n-1) an1+=Get(I,J); if(j<mm-1&&sr[j]+1!=sr[j+1]) { inttmp=n-1-Sc[i]; if(sr[j+1]-1>=tmp) { if(sr[j]+1>=tmp) { if(sc[i]==0|| sc[i]==n-1) an1+=4* (sr[j+1]-sr[j]-1); Elsean1+=5* (sr[j+1]-sr[j]-1); } Else { if(sc[i]==0|| sc[i]==n-1) an1+=4* (sr[j+1]-tmp); Elsean1+=5* (sr[j+1]-tmp); } }} an2+=Get(I,J); }} an2+=5* (n*n-mn*mm); An2-=2* (N-MN) +2* (nmm); intGc=__GCD (an1,an2); An1/=gc;an2/=GC; printf ("Case #%d:%d/%d\n", cas++, an1,an2); } return 0;}
HDU 6229 Wandering Robots finding law + discretization