http://www.nocow.cn/index.php/Translate:USACO/clocks
BFS注意剪枝。
计算好数组类型和大小。
/*
LANG: C++
PROG: clocks
*/
#include <cstdio>
#include <cstring>
#include <cmath>
#include <map>
#include <set>
#include <vector>
#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <string>
#include <queue>
#include <list>
#include <stack>
#include <fstream>
using namespace std;
#define NN 16384
#define INF 0x7ffffff
#define eps 1e-6
#define pi 3.14159265358979626
#define N 270000
struct clock
{
char c[10],step;
int wh,fro;
}cl[N];
int ans[30],b[30];
int chan[10][5]=
{0,0,0,0,0,
1,2,4,5,0,
1,2,3,0,0,
2,3,5,6,0,
1,4,7,0,0,
2,4,5,6,8,
3,6,9,0,0,
4,5,7,8,0,
7,8,9,0,0,
5,6,8,9,0,
};
bool clo[5][5][5][5][5][5][5][5][5];
bool check(int m)
{
for (int i=1;i<=9;i++)
if (cl[m].c[i]!=12) return false;
return true;
}
int main()
{
//freopen("a","r",stdin);
freopen("clocks.in","r",stdin);freopen("clocks.out","w",stdout);
for (int i=1;i<=9;i++) scanf("%d",&cl[0].c[i]);
cl[0].c[0]=0;
cl[0].step=0;
cl[0].fro=-1;
memset(clo,true,sizeof(clo));
int i=0,num=0,t[10],mincount=N,step;
bool Find=false;
while (!Find)
{
int num1=num;
for (int j=1;j<=9;j++)
{
for (int k=0;k<=9;k++) t[k]=cl[i].c[k];
for (int k=0;k<5;k++) t[chan[j][k]]=(t[chan[j][k]]+2)%12+1;
if (clo[t[1]/3][t[2]/3][t[3]/3][t[4]/3][t[5]/3][t[6]/3][t[7]/3][t[8]/3][t[9]/3])
{
clo[t[1]/3][t[2]/3][t[3]/3][t[4]/3][t[5]/3][t[6]/3][t[7]/3][t[8]/3][t[9]/3]=false;
num++;
cl[num].step=cl[i].step+1;
cl[num].wh=j;
cl[num].fro=i;
for (int k=1;k<=9;k++) cl[num].c[k]=t[k];
}
}
step=cl[num].step;
for (int j=num1+1;j<=num;j++)
if (check(j))
{
Find=true;
int count=0,k=j,s=0;
while (cl[k].fro>-1)
{
b[step-s]=cl[k].wh;
count+=b[step-s];
s++;
k=cl[k].fro;
}
if (count<mincount)
{
mincount=count;
for (int k=1;k<=step;k++) ans[k]=b[k];
}
}
i++;
}
for (int i=1;i<=step;i++)
if (i<step) printf("%d ",ans[i]);
else printf("%d\n",ans[i]);
return 0;
}