#include <stdio.h>
#include <string.h>
/*
hanio函數:表示當前的任務是將大小爲floor的盤子從from移動到to,當然這個盤子如果在mid(另外一個柱子)上時就證明這不是最優序列,
如果在from上時就可以推出大小爲floor-1的盤子的任務是從from移動到mid,如果floor在to上時則floor-1的任務是從mid移動到to上
*/
int h[3][64], tail[3], head[3];
bool hanio(int floor, int from, int to, int mid)
{
if(floor == 0)
return true;
int at=-1;
for(int i=0; i<3; i++)
if(h[i][ head[i] ] == floor)
{
at = i;
break;
}
if(at == -1 || at == mid)//如果沒有大小爲floor的盤子,或者這個盤子在mid柱子上,則不是最優序列
return false;
head[ at ] ++;
if(at == from)
hanio(floor-1, from, mid, to);
else if(at == to)
hanio(floor-1, mid, to, from);
}
int main()
{
int t, n;
scanf("%d", &t);
while(t--)
{
scanf("%d", &n);
memset(tail, 0, sizeof(tail));
memset(head, 0, sizeof(head));
for(int i=0; i<3; i++)
{
scanf("%d", &tail[i]);
for(int j=0; j<tail[i]; j++)
scanf("%d", &h[i][j]);
}
printf(hanio(n, 0, 2, 1)?"true\n":"false\n");
}
return 0;
}
hdoj 1997 漢諾塔VII【水】
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.