USACO3.25Magic Squares(bfs)

  1 /*

  2     ID: shangca2

  3     LANG: C++

  4     TASK: msquare

  5  */

  6 #include <iostream>

  7 #include<cstdio>

  8 #include<cstring>

  9 #include<algorithm>

 10 #include<stdlib.h>

 11 #include<queue>

 12 #define INF 0xfffffff

 13 using namespace std;

 14 int o[10];

 15 typedef struct node

 16 {

 17     int num;

 18     int a[10],pre;

 19     char c;

 20 }st;

 21 st q[50000],ss;

 22 char s1[10010],s2[10010];

 23 int f[8][8][8][8][8][8][8];

 24 void bfs()

 25 {

 26     int i,d=1,p=0,minz = INF;

 27     q[d].num = 0;

 28     q[d].pre = -1;

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

 30     q[d].a[i] = i;

 31     f[0][1][2][3][4][5][6] = 1;

 32     while(p!=d)

 33     {

 34         p++;

 35         ss = q[p];

 36         if(ss.num>minz)

 37         continue;

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

 39         {

 40             if(ss.a[i]!=o[i])

 41             break;

 42         }

 43         if(i==9)

 44         {

 45             int x = ss.pre,w=0;

 46             s2[w++] = ss.c;

 47             while(x!=-1)

 48             {

 49                 s2[w++] = q[x].c;

 50                 x = q[x].pre;

 51             }

 52             s2[w] = '\0';

 53             if(ss.num<minz)

 54             {

 55                 minz = ss.num;

 56                 strcpy(s1,s2);

 57             }

 58             else

 59             {

 60                 if(strcmp(s2,s1)>0)

 61                 strcpy(s1,s2);

 62             }

 63             continue;

 64         }

 65         int b[10];

 66         for(i = 8; i >= 1 ; i--)

 67             b[i] = ss.a[8-i+1];

 68         if(!f[b[1]-1][b[2]-1][b[3]-1][b[4]-1][b[5]-1][b[6]-1][b[7]-1])

 69         {

 70             d++;

 71             q[d].num = ss.num+1;

 72             q[d].c = 'A';

 73             q[d].pre = p;

 74             f[b[1]-1][b[2]-1][b[3]-1][b[4]-1][b[5]-1][b[6]-1][b[7]-1] = 1;

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

 76             q[d].a[i] = b[i];

 77         }

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

 79         {

 80             if(i>1&&i<=4)

 81             b[i] = ss.a[i-1];

 82             if(i==1)

 83             b[i] = ss.a[4];

 84             if(i==8)

 85             b[i] = ss.a[5];

 86             if(i>4&&i<8)

 87             b[i] = ss.a[i+1];

 88         }

 89         if(!f[b[1]-1][b[2]-1][b[3]-1][b[4]-1][b[5]-1][b[6]-1][b[7]-1])

 90         {

 91             d++;

 92             q[d].num = ss.num+1;

 93             q[d].c = 'B';

 94             q[d].pre = p;

 95             f[b[1]-1][b[2]-1][b[3]-1][b[4]-1][b[5]-1][b[6]-1][b[7]-1] = 1;

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

 97             q[d].a[i] = b[i];

 98         }

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

100         {

101             if(i==2)

102             b[i] = ss.a[7];

103             else if(i==3)

104             b[i] = ss.a[2];

105             else if(i==6)

106             b[i] = ss.a[3];

107             else if(i==7)

108             b[i] = ss.a[6];

109             else b[i] = ss.a[i];

110         }

111         if(!f[b[1]-1][b[2]-1][b[3]-1][b[4]-1][b[5]-1][b[6]-1][b[7]-1])

112         {

113             d++;

114             q[d].num = ss.num+1;

115             q[d].c = 'C';

116             q[d].pre = p;

117             f[b[1]-1][b[2]-1][b[3]-1][b[4]-1][b[5]-1][b[6]-1][b[7]-1] = 1;

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

119             q[d].a[i] = b[i];

120         }

121     }

122     printf("%d\n",minz);

123     if(minz!=0)

124     {

125         int k = strlen(s1),qq=0;

126         for(i = k-1 ; i>= 0; i--)

127         {

128             qq++;

129             cout<<s1[i];

130             if(qq%60==0||i==0)

131             puts("");

132         }

133     }

134     else

135     puts("");

136 }

137 int main()

138 {

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

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

141     int i;

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

143     cin>>o[i];

144     bfs();

145     return 0;

146 }
View Code

这题 亮点在于开了个7维数组。。。

你可能感兴趣的:(USACO)