USACO 3.2 Magic Squares(记录路径的BFS)

复杂,这种BFS,就是模拟的节奏啊,写了好久。用8进制记录每一个状态,然后顺序一定要想清楚,然后变换的时候注意一下。很多细节问题,导致我调试了很久。

  1 /*

  2      ID: cuizhe

  3      LANG: C++

  4      TASK: msquare

  5 */

  6 #include <cstdio>

  7 #include <cstring>

  8 #include <string>

  9 #include <cmath>

 10 #include <queue>

 11 #include <map>

 12 using namespace std;

 13 int o[10],p[10],que[100000],pre[100000],st[100000];

 14 map<int,int>mp;

 15 void judge(int x)

 16 {

 17     int num = 1;

 18     memset(p,0,sizeof(p));

 19     while(x)

 20     {

 21         p[num++] = x%8;

 22         x = x/8;

 23     }

 24 }

 25 int main()

 26 {

 27     int i,j,k,n,aim,str,end,sum,num = 0,tail;

 28     int t[10];

 29     char ch[1001];

 30     freopen("msquare.in","r",stdin);

 31     freopen("msquare.out","w",stdout);

 32     aim = 0;

 33     o[1] = 1;

 34     for(i = 2; i <= 8; i ++)

 35     {

 36         o[i] = o[i-1]*8;

 37     }

 38     for(i = 1; i <= 8; i ++)

 39     {

 40         scanf("%d",&n);

 41         aim += (n-1)*o[i];

 42     }

 43     que[1] = 16434824;

 44     mp[que[1]] = 1;

 45     str = end = 1;

 46     while(str <= end)

 47     {

 48         if(mp[aim]) break;

 49         j = 1;

 50         for(k = str; k <= end; k ++)

 51         {

 52             judge(que[k]);

 53             for(i = 1; i <= 8; i ++)

 54             {

 55                 t[i] = p[9-i];

 56             }

 57             for(i = 1,sum = 0; i <= 8; i ++)

 58                 sum += o[i]*t[i];

 59             if(!mp[sum])

 60             {

 61                 st[end+j] = 1;

 62                 mp[sum] = end+j;

 63                 que[end+j] = sum;

 64                 pre[end+j] = k;

 65                 j ++;

 66             }

 67             for(i = 2; i <= 4; i ++)

 68                 t[i] = p[i-1];

 69             t[1] = p[4];

 70             for(i = 5; i <= 7; i ++)

 71                 t[i] = p[i+1];

 72             t[8] = p[5];

 73             for(i = 1,sum = 0; i <= 8; i ++)

 74                 sum += o[i]*t[i];

 75             if(!mp[sum])

 76             {

 77                 st[end+j] = 2;

 78                 mp[sum] = end+j;

 79                 que[end+j] = sum;

 80                 pre[end+j] = k;

 81                 j ++;

 82             }

 83             for(i = 1; i <= 8; i ++)

 84                 t[i] = p[i];

 85             t[2] = p[7];

 86             t[3] = p[2];

 87             t[6] = p[3];

 88             t[7] = p[6];

 89             for(i = 1,sum = 0; i <= 8; i ++)

 90                 sum += o[i]*t[i];

 91             if(!mp[sum])

 92             {

 93                 st[end+j] = 3;

 94                 mp[sum] = end+j;

 95                 que[end+j] = sum;

 96                 pre[end+j] = k;

 97                 j ++;

 98             }

 99         }

100         str = end+1;

101         end = end+j-1;

102         num ++;

103     }

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

105     tail = mp[aim];

106     for(i = num; i >= 1; i --)

107     {

108         ch[i] = st[tail]-1+'A';

109         tail = pre[tail];

110     }

111     for(i = 1; i <= num; i ++)

112     {

113         printf("%c",ch[i]);

114         if(i%60 == 0)

115             printf("\n");

116     }

117     printf("\n");

118     return 0;

119 }

你可能感兴趣的:(USACO)