UVA1003 (dfs+ binary conversion)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.