Uvalive 4997 ABCD Tiles--dfs

Source: Internet
Author: User

Test instructions: NxN map, there is a color of the tile and some space points, to use the b,c,d color to fill these spaces, only a cross-shaped fill, but also to ensure that the common or common side of the lattice can not be the same color, a dictionary order the smallest fill method, if not, output "not Possible".

Solution: From the top down, if there is a space, then it must be the bottom of the grid to fill the cross tile to fill in this space, so this we will mark this space, and the following three and below a space to assign a different value, do not let the scan after scanning, those are not spaces, directly jump out can. So we can find all the need to fill the number of 10-shaped tiles, and know the location of the lattice, then just consider what color to dye things, because there are only three colors, so DFS to fill, if to a certain state to find which color can not fill, then backtracking. This until the staining scheme location is successfully found.

Code:

#include <iostream>#include<cstdio>#include<cstring>#include<cstdlib>#include<cmath>#include<algorithm>using namespacestd;Charmp[ -][ -];intN,tot;intcol[4];BOOLOK (intXintY) {returnX >=0&& x < n && y >=0&& y <N;}BOOLFindcolor (intIintJCharch) {memset (col,0,sizeof(col)); if(OK (i+1, j+1) && mp[i+1][j+1] !='.') col[mp[i+1][j+1]-'A']++; if(OK (i+1, J-1) && mp[i+1][j-1] !='.') col[mp[i+1][j-1]-'A']++; if(OK (I-1, J-1) && mp[i-1][j-1] !='.') col[mp[i-1][j-1]-'A']++; if(OK (I-1, j+1) && mp[i-1][j+1] !='.') col[mp[i-1][j+1]-'A']++;  for(intk=j-1; k<=j+1; k++)if(OK (i+2, k) && mp[i+2][K]! ='.') col[mp[i+2][k]-'A']++;  for(intk=i-1; k<=i+1; k++)if(OK (k,j+2) && mp[k][j+2] !='.') col[mp[k][j+2]-'A']++;  for(intk=j-1; k<=j+1; k++)if(OK (I-2, k) && mp[i-2][K]! ='.') col[mp[i-2][k]-'A']++;  for(intk=i-1; k<=i+1; k++)if(OK (k,j-2) && mp[k][j-2] !='.') col[mp[k][j-2]-'A']++; if(col[ch-'A'])return true; return false;}voidColorintIintJCharch) {Mp[i][j]= mp[i-1][J] = mp[i][j+1] = mp[i+1][J] = mp[i][j-1] =ch;}structNode {intx, y; Node (int_x,int_y): X (_x), Y (_y) {} node () {}}p[255];BOOLDfsintc) {if(c = = tot)return true; C++; intx = p[c].x, y =p[c].y;  for(intI=1; i<=3; i++) {//b,c,d        if(!findcolor (x, Y,'A'+i)) {//around there's no ' A ' +i this colorColor (x, Y,'A'+i); if(Dfs (c))return true; Color (x, Y,'.'); }    }    return false;}intMain () {intT,cs =1, I,j,k,h; scanf ("%d",&t);  while(t--) {scanf ("%d",&N); intSpace =0;  for(i=0; i<n;i++) scanf ("%s", Mp[i]); intFlag =1; Tot=0;  for(i=0; i<n;i++) {             for(j=0; j<n;j++) {                if(Mp[i][j] = ='.') {                     for(k=j-1; k<=j+1; k++) {                        if(! OK (i+1, k) | | mp[i+1][K]! ='.') {flag =0; Break; } Elsemp[i+1][K] ='#'; }                    if(! OK (i+2, j) | | mp[i+2][J]! ='.') {flag =0; Break; } Elsemp[i+2][J] ='#'; p[++tot] = node (i+1, j);//Coloring Center Point}}} printf ("Case %d:", cs++); if(!flag) {Puts ("Not possible!");Continue; } if(Dfs (0) {puts ("");  for(i=0; i<n;i++)            {                 for(j=0; j<n;j++) printf ("%c", Mp[i][j]); Puts (""); }        }        ElsePuts"Not possible!"); }    return 0;}
View Code

Uvalive 4997 ABCD Tiles--dfs

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.