Gym - 101502 D. Dice Game 思维+dp

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;
}

猜你喜欢

转载自blog.csdn.net/qq_41955236/article/details/81582759