Gym - 101502 D. Dice Game 思维+dp

62 篇文章 0 订阅
10 篇文章 0 订阅

D. Dice Game

time limit per test

1.0 s

memory limit per test

256 MB

input

standard input

output

standard output

A dice is a small cube, with each side having a different number of spots on it, ranging from 1 to 6.

Each side in the dice has 4 adjacent sides that can be reached by rotating the dice (i.e. the current side) 90 degrees. The following picture can help you to conclude the adjacent sides for each side in the dice.

In this problem, you are given a dice with the side containing 1 spot facing upwards, and a sum n, your task is to find the minimum number of required moves to reach the given sum.

On each move, you can rotate the dice 90 degrees to get one of the adjacent sides to the side that currently facing upwards, and add the value of the new side to your current sum. According to the previous picture, if the side that currently facing upwards contains 1 spot, then in one move you can move to one of sides that contain 2, 3, 4, or 5 spots.

Initially, your current sum is 0. Even though at the beginning the side that containing 1 spot is facing upwards, but its value will not be added to your sum from the beginning, which means that you must make at least one move to start adding values to your current sum.

Input

The first line contains an integer T (1 ≤ T ≤ 200), where T is the number of test cases.

Then T lines follow, each line contains an integer n (1 ≤ n ≤ 104), where n is the required sum you need to reach.

Output

For each test case, print a single line containing the minimum number of required moves to reach the given sum. If there is no answer, print -1.

Example

input

2
5
10

output

1
2

Note

In the first test case, you can rotate the dice 90 degrees one time, and make the side that contains 5 spots facing upwards, which make the current sum equal to 5. So, you need one move to reach sum equal to 5.

In the second test case, you can rotate the dice 90 degrees one time, and make the side that contains 4 spots facing upwards, which make the current sum equal to 4. Then rotate the dice another 90 degrees, and make the side that contains 6 spots facing upwards, which make the current sum equal to 10. So, you need two moves to reach sum equal to 10.


 

题意:给你一个六面相对方向已知的骰子,初始时1朝上,每次投掷时只能转到其附近的四个数字,给你一个数n,问最少要转几次才能使和达到n,如果不行输出-1

 

做法:dp[i][j]表示和达到i时,最后一次数字是j是最少的次数,开每一个数字四周的方向数字,则转移方程是dp[i+dir[j][k]][dir[j][k]]=min(dp[i][j]+1,dp[i+dir[j][k]][dir[j][k]]);


代码如下:

#include<bits/stdc++.h>
using namespace std;
const int inf=0x3f3f3f3f;
int dir[7][4]={{0,0,0,0},{2,3,4,5},{1,3,4,6},{1,2,5,6},{1,2,5,6},{1,3,4,6},{2,3,4,5}};
int dp[10005][7];
int main(){
    int t,n;
    cin>>t;
    while(t--){
        cin>>n;
        memset(dp,125,sizeof(dp));
        for(int i=0;i<4;i++){
            dp[dir[1][i]][dir[1][i]]=1;
        }
        for(int i=1;i<=n;i++){
            for(int j=1;j<=6;j++){
                for(int k=0;k<4;k++){
                    dp[i+dir[j][k]][dir[j][k]]=min(dp[i][j]+1,dp[i+dir[j][k]][dir[j][k]]);
                }
            }
        }
        int ans=inf;
        for(int i=1;i<=6;i++){
            ans=min(ans,dp[n][i]);
        }
        if(ans>=inf) printf("-1\n");
        else printf("%d\n",ans);
    }
    return 0;
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值