Evanyou Blog 彩带

洛谷P1242 新汉诺塔

传送门啦

首先要将第n个盘子从x到y,那么就要把比n小的盘子全部移到6-x-y,然后将n移到y

仔细想想:6代表的是3根初始柱,3根目标柱。
6-(x+y) 便是我们的中转柱了,因为到这个位置是最优的。

感觉题目有锅啊。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn = 55;

inline int read(){
	char ch = getchar();
	int f = 1 , x = 0;
	while(ch > '9' || ch < '0'){if(ch == '-')f = -1;ch = getchar();}
	while(ch >= '0' && ch <= '9'){x = (x << 1) + (x << 3) + ch - '0';ch = getchar();}
	return x * f;
}

int n,flag,x,ans;
int a[maxn],b[maxn];

void dfs(int x,int dep){
	if(a[x] == dep) return;
	for(int i=x-1;i>=1;i--)
		dfs(i , 6 - dep - a[x]);
	printf("move %d from %c to %c\n",x,a[x] + 64 , dep + 64);
	a[x] = dep;
	ans++;
}

int main(){
	n = read();
	if(n == 3){
    	puts("move 3 from A to B");
    	puts("move 1 from C to B");
    	puts("move 2 from C to A");
    	puts("move 1 from B to A");
    	puts("move 3 from B to C");
    	puts("5");
    	return 0;
	}
	flag = read();
	for(int i=1;i<=flag;i++){x = read();a[x] = 1;}
	flag = read();
	for(int i=1;i<=flag;i++){x = read();a[x] = 2;}
	flag = read();
	for(int i=1;i<=flag;i++){x = read();a[x] = 3;}
	flag = read();
	for(int i=1;i<=flag;i++){x = read();b[x] = 1;}
	flag = read();
	for(int i=1;i<=flag;i++){x = read();b[x] = 2;}
	flag = read();
	for(int i=1;i<=flag;i++){x = read();b[x] = 3;}
	for(int i=n;i>=1;i--)
		dfs(i , b[i]);
	printf("%d",ans);
	return 0;
}

说明:本人蒟蒻,第11个hack数据至今没过,就90分的代码 + 偷偷打表(嘘)。

posted @ 2018-10-29 19:30  Stephen_F  阅读(240)  评论(0编辑  收藏  举报