Test instructions: Enter 16 decimal numbers for many characters, now you need to convert to 2, and then the binary 1 and 0 characters need you to find out.
Idea: Each character is different and can be calculated by the number of circles.
#include <cstdio>#include<iostream>#include<algorithm>#include<cstring>using namespacestd;intgragh[ +][ +];intn,m;intans;intdir[4][2] = {{-1,0},{0,1},{1,0},{0,-1}};voidChangeCharCintXinty) { if(c >='0'&& C <='9'){ intD = C-'0'; gragh[x][(y-1)*4+4] = d%2;d/=2; gragh[x][(y-1)*4+3] = d%2;d/=2; gragh[x][(y-1)*4+2] = d%2;d/=2; gragh[x][(y-1)*4+1] = d%2; } Else { intD = C-'a'+Ten; gragh[x][(y-1)*4+4] = d%2;d/=2; gragh[x][(y-1)*4+3] = d%2;d/=2; gragh[x][(y-1)*4+2] = d%2;d/=2; gragh[x][(y-1)*4+1] = d%2; }}voidDFS1 (intXintY//The effect is to remove 1. Useless 0 to clear 2. If you don't have a situation, you'll get 0 out.{Gragh[x][y]= -1; for(inti =0; I <4; i++){ intxx = x + dir[i][0]; intyy = y + dir[i][1]; if(XX >=1&& xx <= n && yy >=1&& yy <= m && gragh[xx][yy] = =0) {DFS1 (XX,YY); } }}voidDFS2 (intXinty) {Gragh[x][y]= -1; for(inti =0; I <4; i++){ intxx = x + dir[i][0]; intyy = y + dir[i][1]; if(XX >=1&& xx <= n && yy >=1&& yy <= m && gragh[xx][yy] = =0) {ans++;//record the number of laps per characterDFS1 (XX,YY); } Else if(XX >=1&& xx <= n && yy >=1&& yy <= m && gragh[xx][yy] = =1) {DFS2 (XX,YY); } }}intMain () {//freopen ("In.txt", "R", stdin); intNcase =1; while(SCANF ("%d%d", &n,&m)! =EOF) { if(n = =0&& m = =0) Break; memset (Gragh,0,sizeof(Gragh)); Chara[ -]; for(inti =1; I <= N; i++) {scanf ("%s", A +1); for(intj = M;j >=1; j--) {change (A[J],I,J); //hexadecimal number, first row, column}} M*=4; for(inti =1; I <= N; i++){ if(gragh[i][1] ==0) {DFS1 (I,1); } if(Gragh[i][m] = =0) {DFS1 (i,m); } } for(intj =1; J <= M; J + +){ if(gragh[1][J] = =0) {DFS1 (1, J); } if(Gragh[n][j] = =0) {DFS1 (n,j); } } intpos =0; Chars[ -]; for(inti =1; I <= N; i++){ for(intj =1; J <= M; J + +) {ans=0; if(Gragh[i][j] = =1) {DFS2 (i,j); if(ans = =1) S[pos++] ='A'; Else if(ans = =3) S[pos++] ='J'; Else if(ans = =5) S[pos++] ='D'; Else if(ans = =4) S[pos++] ='S'; Else if(ans = =0) S[pos++] ='W'; Else if(ans = =2) S[pos++] ='K'; }}} sort (S,s+POS); printf ("Case %d:", ncase++); for(inti =0; i < POS; i++) printf ("%c", S[i]); printf ("\ n"); } return 0;}
UVA1003 (dfs+ binary conversion)