9 1 4 0 1 0 1 1 1 1 1 0 1 0 1 1 3 0 0 2 1 0 0 2 0 0 3 2 3 4 5 1 2 4 1 1 2 3 1 1 2 4 0 3 4 0 0 5 0 0 3 1 2 0 0 0 0 5 2 2 0 0 0 0 4 3 1 2 3 0 0 1 4 1 2 3 0 0 2 3 6 0 1 1 1 0 1 1 1 0 1 1 2 3 0 1 0 2 1 2 3 0 1 1 3 1 2 4 1 0 0 4 2 0 0 0 2 0 5 2 3 4 0 1 0 6 0 1 0 2 3 4 4 6 2 0 0 0 2 0 0 0 2 1 1 2 3 0 1 0 2 1 2 3 1 1 1 3 1 0 5 1 0 1 4 5 2 0 1 1 0 5 0 2 3 0 1 1 6 1 0 1 6 0 7 5 1 0 1 1 1 1 0 1 0 1 1 1 2 3 0 0 1 6 6 0 0 1 1 0 0 0 1 0 1 1 2 3 1 0 1 2 1 2 3 5 0 1 3 4 2 3 5 3 1 4 0 2 6 5 3 0 5 1 2 1 1 3 0 6 3 3 3 5 0 4 7 1 1 1 1 1 0 1 1 1 0 1 1 2 3 0 1 0 8 1 0 2 5 3 0 3 1 1 0 1 0 2 3 5 0 0 9 2 0 1 0 1 0 1 0 0 2 1 1 1 4 2 0 0 2 2 0 0 0 0 2
以上是HDU的所有数据。。~~~嘎嘎嘎嘎
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
struct node
{
int an[4];
int step;
char op;
int set;
}Q[1000005];
void print(int x)
{
if(Q[x].op=='Z')return;
print(Q[x].set);
printf("%c",Q[x].op);
}
int C[4][4];
bool vis[205][205][205];
node tag;
bool isok(node tt)
{
for(int i=1;i<=3;i++)
{
if(tt.an[i]!=tag.an[i])return false;
}
return true;
}
void debug(node t)
{
printf("----%d %d %d\n",t.an[1],t.an[2],t.an[3]);
}
void bfs(node t)
{
int head=0;
int tail=0;
Q[tail++]=t;
node w,e;
while(head<tail)
{
w=Q[head];
if(isok(w))
{
printf("%d ",w.step);
print(head);
return;
}
for(int k=1;k<=3;k++)
{
e=w;
if(e.an[k]<=0)continue;
e.an[k]--;
for(int cnt=1;cnt<=3;cnt++)
{
e.an[cnt]+=C[k][cnt];
}
if(e.an[1]>20 || e.an[2]>20 || e.an[3]>20)continue;
e.step++;
e.op=k+'A'-1;
e.set=head;
if(!vis[e.an[1]][e.an[2]][e.an[3]])
{
vis[e.an[1]][e.an[2]][e.an[3]]=true;
Q[tail++]=e;
}
}
for(int k=1;k<=3;k++)
{
e=w;
bool flag=false;
for(int cnt=1;cnt<=3;cnt++) //先要判断能不能换 被坑了
{
e.an[cnt]-=C[k][cnt];
if(e.an[cnt]<0)flag=true;
}
e.an[k]++;
if(flag)continue;
//printf("k=%d ",k);
//debug(e);
//getchar();
if(e.an[1]>20 || e.an[2]>20 || e.an[3]>20)continue;
e.step++;
e.op='a'+k-1;
e.set=head;
if(!vis[e.an[1]][e.an[2]][e.an[3]])
{
vis[e.an[1]][e.an[2]][e.an[3]]=true;
Q[tail++]=e;
}
}
head++;
}
printf("NO SOLUTION");
}
int main()
{
int T;
//freopen("out.txt","w",stdout);
scanf("%d",&T);
while(T--)
{
int NUM,CASE;
scanf("%d%d",&NUM,&CASE);
for(int i=1;i<=3;i++)
{
for(int j=1;j<=3;j++)
scanf("%d",&C[i][j]);
}
int fuckingcase;
node t;
printf("%d %d\n",NUM,CASE);
for(int zenmezhemeduobianliang=1;zenmezhemeduobianliang<=CASE;zenmezhemeduobianliang++)//各种CASE的变量 法克
{
memset(vis,false,sizeof(vis));
scanf("%d",&fuckingcase);
for(int i=1;i<=3;i++)
scanf("%d",&t.an[i]);
for(int i=1;i<=3;i++)
scanf("%d",&tag.an[i]);
vis[t.an[1]][t.an[2]][t.an[3]]=true;
t.step=0;
t.set=-1;
t.op='Z';
printf("%d ",fuckingcase);
if(isok(t))
{
printf("0\n");
continue;
}
bfs(t);
printf("\n");
}
}
return 0;
}
/*
2
1 2
3 0 0
0 0 0
0 0 0
1 1 0 0 3 0 0
2 1 0 0 1 0 0
2 2
0 3 4
0 0 5
0 0 3
1 2 0 0 0 0 5
2 2 0 0 0 0 4
*/