Hanoi Tower Troubles Again!

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1239

题目描述:

Time Limit: 2 Seconds      Memory Limit: 65536 KB

People stopped moving discs from peg to peg after they know the number of steps needed to complete the entire task. But on the other hand, they didn't not stopped thinking about similar puzzles with the Hanoi Tower. Mr.S invented a little game on it. The game consists of N pegs and a LOT of balls. The balls are numbered 1,2,3... The balls look ordinary, but they are actually magic. If the sum of the numbers on two balls is NOT a square number, they will push each other with a great force when they're too closed, so they can NEVER be put together touching each other.

 

The player should place one ball on the top of a peg at a time. He should first try ball 1, then ball 2, then ball 3... If he fails to do so, the game ends. Help the player to place as many balls as possible. You may take a look at the picture above, since it shows us a best result for 4 pegs.


Input

The first line of the input contains a single integer T, indicating the number of test cases. (1<=T<=50) Each test case contains a single integer N(1<=N<=50), indicating the number of pegs available.


Output

For each test case in the input print a line containing an integer indicating the maximal number of balls that can be placed. Print -1 if an infinite number of balls can be placed.


Sample Input

2
4
25


Sample Output

11
337

 

 

 

个人分析:

数据对应关系分别为:

1    1

2    3

3    7

4   11

5   17

6    23

相差分别为2 4 4 6 6,那么假设后面的数字相差的是8 8 10 10等等,经过检验,假设成立,那么这个就是题目中的规律 。

#include<cstdio>
#include<algorithm>
#include<iostream>
#include<queue>
#include<cmath>
#include<map>
#include<vector>
#include<string.h>
using namespace std;
int num[60];
int main()
{
    num[1]=1;
    num[2]=3;
    num[3]=7;
    for(int i=4;i<=50;++i)
    {
        num[i]=num[i-1]+i+i%2;
    }
    int T;
    cin>>T;
    while(T--)
    {
        int  a;
        cin>>a;
        printf("%d\n",num[a]);
    }
    return 0;
}

不过由于是练习搜索的,那接下来就再讨论一下搜索的情况!

 

 

其中,i代表的是从第一个柱子开始,向右边遍历,判断当前数字应该放到哪个柱子上面,如果说当前的数据已经 放到其中的一个柱子上面了,那么这个柱子最上面的数据就要进行更新,变成这个数字,如果说遍历了所有的柱子,还是没有找到合适的位置,那么就应该结束判断,输出当前的数据个数。

 

#include<cstdio>
#include<algorithm>
#include<iostream>
#include<queue>
#include<cmath>
#include<map>
#include<vector>
#include<cstring>
using namespace std;
int  a,num,T;
int  number[2000];

void dfs(int x)
{
   int i=1;//这里代表从第一个柱子开始
   double v=sqrt(x+number[i]);
   while(v!=(int)v&&number[i]!=0)
   {
       ++i;
       v=sqrt(x+number[i]);
   }
   if(i>a) return;
   number[i]=x;
   num=max(num,x);
   dfs(x+1);
   return ;
}

int main()
{
   scanf("%d",&T);
   while(T--)
   {
       num=0;
       scanf("%d",&a);
       memset(number,0,sizeof(number));
       dfs(1);//代表从1开始判断
       num?printf("%d\n",num):printf("-1\n");
   }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值