USACO 5.2 Wisconsin Squares(DFS)

好扯淡。。。

  1 /*

  2  ID: cuizhe

  3  LANG: C++

  4  TASK: wissqu

  5 */

  6 #include <iostream>

  7 #include <cstdio>

  8 #include <cstring>

  9 #include <queue>

 10 #include <map>

 11 #include <ctime>

 12 #include <cmath>

 13 #include <algorithm>

 14 using namespace std;

 15 char str[10][10];

 16 int flag[10][10],o[6];

 17 int q1[20],q2[20],q3[20];

 18 int a[9] = {0,0,0,1,1,1,-1,-1,-1};

 19 int b[9] = {1,-1,0,0,1,-1,0,1,-1};

 20 int num = 0;

 21 int judge(int x,int y,char ch)

 22 {

 23     int i;

 24     for(i = 0; i < 9; i ++)

 25     {

 26         if(x+a[i] >= 0&&x+a[i] < 4&&y+b[i] >= 0&&y+b[i] < 4)

 27         {

 28             if(ch == str[x+a[i]][y+b[i]])

 29                 return 0;

 30         }

 31     }

 32     return 1;

 33 }

 34 void dfs(int step)

 35 {

 36     int i,j,k,top,u;

 37     char temp;

 38     int quer[21],quec[21];

 39     if(step > 16)

 40     {

 41         num ++;

 42         if(num == 1)

 43         {

 44             for(i = 1; i <= 16; i ++)

 45             {

 46                 printf("%c %d %d\n",q1[i]+'A',q2[i]+1,q3[i]+1);

 47             }

 48         }

 49         return ;

 50     }

 51     top = 1;

 52     for(j = 0; j < 4; j ++)

 53     {

 54         for(k = 0; k < 4; k ++)

 55         {

 56             if(!flag[j][k])

 57             {

 58                 quer[top] = j;

 59                 quec[top] = k;

 60                 top ++;

 61             }

 62         }

 63     }

 64     for(i = 0; i < 5; i ++)

 65     {

 66         if(o[i])

 67         {

 68             for(u = 1; u < top; u ++)

 69             {

 70                 j = quer[u];

 71                 k = quec[u];

 72                 if(judge(j,k,i+'A')&&!flag[j][k])

 73                 {

 74                     flag[j][k] = 1;

 75                     o[i] --;

 76                     temp = str[j][k];

 77                     str[j][k] = i + 'A';

 78                     q1[step] = i;

 79                     q2[step] = j;

 80                     q3[step] = k;

 81                     dfs(step+1);

 82                     o[i] ++;

 83                     flag[j][k] = 0;

 84                     str[j][k] = temp;

 85                 }

 86             }

 87         }

 88     }

 89     return ;

 90 }

 91 int main()

 92 {

 93     int i,j;

 94     char temp;

 95     for(i = 0; i < 4; i ++)

 96         scanf("%s",str[i]);

 97     for(i = 0; i < 5; i ++)

 98         o[i] = 3;

 99     for(i = 0; i < 4; i ++)

100     {

101         for(j = 0; j < 4; j ++)

102         {

103             if(judge(i,j,'D'))

104             {

105                 q1[1] = 3;

106                 q2[1] = i;

107                 q3[1] = j;

108                 temp = str[i][j];

109                 str[i][j] = 'D';

110                 dfs(2);

111                 str[i][j] = temp;

112             }

113         }

114     }

115     printf("%d\n",num);

116     return 0;

117 }

 

你可能感兴趣的:(USACO)