Title Description Description
There is a 5x5 chessboard with some of the squares dyed black and the other squares white, and your task is to dye the board with a few squares so that all black squares can be connected together and you have the fewest number of squares to stain. Read into the state of an initial checkerboard, and the output needs to be dyed at least as many squares as possible so that all black squares are connected together. (Note: Connection refers to the upper and lower left and right four directions, if the two black lattice only a point, then not count the connection)
Enter a description input Description
The input includes a 5x5 01 matrix with no spaces in the middle and 1 indicating that the lattice has been dyed black.
outputs description output Description
Minimum number of squares to be dyed for output
sample input to sample
11100
11000
10000
01111
11111
Sample output Sample outputs
1
Well ... Search... First BFS Find the black re-expansion for dyeing. Judge ... Use DFS to determine if connectivity is available. With a happy mood to submit up ... Sure enough, it's tle.
Uh, uh, okay. I'll change the weight of the sentence.
Turns out to be a contrast, now I convert it to 25 bits of binary number, can save a lot of time.
AC..
#include <iostream>#include<cstring>using namespaceStd;typedefCharzt[5][5];zt m,q[500001],v;intf,r,num[500001],dx[4]={-1,1,0,0},dy[4]={0,0,-1,1};BOOLvis[33554435];intblack[ -][2],count=-1;ints;voidDFS (ZT TTPP,intXxintyy) { if(xx<0|| xx>=5|| yy<0|| yy>=5|| v[xx][yy]| | ttpp[xx][yy]=='0') return; V[XX][YY]=1; for(intDd=0;dd <4;DD + +) DFS (ttpp,xx+dx[dd],yy+dy[dd]);}intOkay (zt tp) {inti; memset (V,0,sizeof(v)); for(i=0;i< -; i++) if(tp[i/5][i%5]=='1') Break; DFS (Tp,i/5, i%5); for(intI=0;i<5; i++) for(intj=0;j<5; j + +) if(tp[i][j]=='1'&&!V[i][j])return 0; return 1;}intChange_to_int (zt tt) {intRrr=0, d=1; for(intI=4; i>=0; i--) for(intj=4; j>=0; j--) {RRR+ = (tt[i][j]-'0')*D; D*=2; } returnRRR;}intBFS () {f=0; r=1; for(intI=0;i<5; i++) for(intj=0;j<5; j + +) q[1][i][j]=M[i][j]; num[1]=0; S=Change_to_int (m); Vis[s]=1; while(f<R) {f++; if(Okay (q[f]))returnNum[f]; Count=-1; for(intI=0;i<5; i++) for(intj=0;j<5; j + +) if(q[f][i][j]=='1') {black[++count][0]=i; black[count][1]=J; } for(intI=0; i<=count;i++) for(intj=0;j<4; j + +) { intnx=black[i][0]+Dx[j]; intny=black[i][1]+Dy[j]; if(nx<0|| nx>=5|| ny<0|| ny>=5|| q[f][nx][ny]=='1') Continue; R++; for(intI=0;i<5; i++) for(intj=0;j<5; j + +) {Q[r][i][j]=Q[f][i][j]; } Q[r][nx][ny]='1'; S=Change_to_int (Q[r]); if(!Vis[s]) {Vis[s]=1; NUM[R]=num[f]+1; } ElseR--; } } return 0;}intMain () { for(intI=0;i<5; i++) for(intj=0;j<5; j + +) Cin>>M[i][j]; cout<< BFS () <<"\ n";}
Codevs 1049 Checkerboard Staining