汉诺塔VII

题目来源:hdu1997

汉诺塔VII

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 1073    Accepted Submission(s): 718


Problem Description
n个盘子的汉诺塔问题的最少移动次数是2^n-1,即在移动过程中会产生2^n个系列。由于发生错移产生的系列就增加了,这种错误是放错了柱子,并不会把大盘放到小盘上,即各柱子从下往上的大小仍保持如下关系 : 
n=m+p+q
a1>a2>...>am
b1>b2>...>bp
c1>c2>...>cq
ai是A柱上的盘的盘号系列,bi是B柱上的盘的盘号系列, ci是C柱上的盘的盘号系列,最初目标是将A柱上的n个盘子移到C盘. 给出1个系列,判断它是否是在正确的移动中产生的系列.
例1:n=3
3
2
1
是正确的
例2:n=3
3
1
2
是不正确的。
注:对于例2如果目标是将A柱上的n个盘子移到B盘. 则是正确的.
 

Input
包含多组数据,首先输入T,表示有T组数据.每组数据4行,第1行N是盘子的数目N<=64.
后3行如下
m a1 a2 ...am
p b1 b2 ...bp
q c1 c2 ...cq
N=m+p+q,0<=m<=N,0<=p<=N,0<=q<=N,
 

Output
对于每组数据,判断它是否是在正确的移动中产生的系列.正确输出true,否则false 
 

Sample Input
  
  
6 3 1 3 1 2 1 1 3 1 3 1 1 1 2 6 3 6 5 4 1 1 2 3 2 6 3 6 5 4 2 3 2 1 1 3 1 3 1 2 1 1 20 2 20 17 2 19 18 16 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1
 

Sample Output
  
  
true false false false true true
 

Author
Zhousc@ECJTU
 

Source
 

Recommend
lcy   |   We have carefully selected several similar problems for you:   1998  1002  1078  2121  1099 
 
解析:
1.模拟解法:

    ①考虑最大盘子 n 号盘子,移动方向为A——>C,它只能在A或者C上,如果它在B上,则为false;

②如果 n 号盘子在 A 上,则其上的 n-1 号盘子必处于从A——>B的移动过程中,此时最大盘号为 n-1,移动方向为A—>B;

③如果 n 号盘子在 C 上,则其上的 n-1 号盘子必处于从B——>C的移动过程中,此时最大盘号为 n-1,移动方向为B—>C; 

④重复①②③。

2.规律求解:
    ①盘为奇数时,最底为偶数的只能在2号柱,底数为奇数的盘只能在1,3号柱
    ②偶数与上面相反
    ③每一根柱子上的盘是奇偶互换的
模拟代码:

#include<cstdio>
#define maxn 70
using namespace std;

int a[4][maxn],t,n;

void init()
{
  freopen("hdu1997.in","r",stdin);
  freopen("3hdu1997.out","w",stdout);
}

bool han(int k,int s,int z,int e)
{
  if(k<1)return 1;
  int s1=a[s][0],s2=a[z][0],s3=a[e][0];
  if(a[s][s1]==k)
    {
      a[s][0]--;
      return han(k-1,s,e,z);
    }
  if(a[e][s3]==k)
    {
      a[e][0]--;
      return han(k-1,z,s,e);
    }
  return 0;	  
}

void work()
{
  int i,j,k;
  bool flag;
  while(scanf("%d",&t)==1)
    for(j=1;j<=t;j++)
      {
        scanf("%d",&n);
        scanf("%d",&a[0][0]);
        for(i=a[0][0];i>=1;i--)scanf("%d",&a[0][i]);
        scanf("%d",&a[1][0]);
        for(i=a[1][0];i>=1;i--)scanf("%d",&a[1][i]);
        scanf("%d",&a[2][0]);
        for(i=a[2][0];i>=1;i--)scanf("%d",&a[2][i]);
        if(han(n,0,1,2))printf("true\n");
        else printf("false\n");
      }
}

int main()
{
  init();
  work();
  return 0;
}

规律代码:

#include<cstdio>
#define maxn 64
using  namespace std;

int a[4][maxn+10],n;

void redirect()
{
  freopen("hdu1997.in","r",stdin);
  freopen("4hdu1997.out","w",stdout);
}

inline bool ok()
{
  int i,j,k;
  for(i=1;i<=3;i++)
    {
      if(a[i][0]==0)continue;
      if((n+i+a[i][1])%2==0)return 0;
      for(j=2;j<=a[i][0];j++)if((a[i][j]+a[i][j-1])%2==0)return 0;
    } 
  return 1;  
}

void work()
{
  int t,i,j,k;
  while(scanf("%d",&t)==1)
    for(i=1;i<=t;i++)
      {
      	scanf("%d",&n);
      	
      	for(j=1;j<=3;j++)
      	  {
      	     scanf("%d",&a[j][0]);
      	     for(k=1;k<=a[j][0];k++)scanf("%d",&a[j][k]);
      	  }
      	
      	if(ok())printf("true\n");
      	else printf("false\n");
      }
}

int main()
{
  redirect();
  work();
  return 0;
}

规律代码(转自   气泡熊领主):

#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#define MAX(a,b) ((a)>(b)?(a):(b))
#define MIN(a,b) ((a)<(b)?(a):(b))
using namespace std;

int main(){
    int tests,n,i,j,t,flag;
    int mem[2];
    scanf("%d",&tests);
    while(tests--){
        flag = 1;
        scanf("%d",&n);
        for(i=0;i<3;i++){
            scanf("%d%d",&t,&mem[0]);
            if((n+mem[0]+i)%2) flag = 0;

            for(j=1;j<t;j++){
                scanf("%d",&mem[j%2]);
                if((mem[j%2]-mem[(j-1)%2]-1)%2) flag = 0;
            }
        }
        if(flag) printf("true\n");
        else printf("false\n");
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值