其他分享
首页 > 其他分享> > D - Dice Game (BFS)

D - Dice Game (BFS)

作者:互联网

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朝上),在每次移动中,你可以将骰子旋转90度,使相邻的边中的一个朝上,

并将新边的值添加到当前的和中(一开始数字1朝上的时候和为0),你的任务是找到达到给定和所需的最小移动次数。

 

解题思路:广搜(动态规划和直接计算也可以的,我觉得广搜容易理解)

PS:一个骰子中,相背的两个面数字之和为7

 

我们定义一个结构体,元素有当前的和(sum),当前向上的数字(up),已经走的步数(step)

一开始sum=0,up=1,step=0,放入队列,再定义一个记录步数的数组,初始化为-1,然后就可以爆搜了

详细看代码注释

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include <string.h>
#include<vector>
#include <cmath>
#include <queue>
#define maxn 10001
using namespace std;
int sum[maxn];//记录达到和的所需的最小步数
struct mian
{
    int sum,step,up;//sum为和,step为步数,up为面向上的数字
}now,nextt;    
void bfs()
{
    now.sum =0;
    now.step =0;
    now.up =1;
    queue<mian> q;
    q.push(now);
    while(!q.empty())
    {
        now=q.front();
        q.pop();
        if(now.sum<=maxn)
        {
            for(int i=1;i<=6;i++)//i为下一步翻转后面向上的数字
            {
     // 当前向上数字和i相同   i在当前数字的背面    翻转后的和已经记录   
            if(now.up==i||now.up+i==7||sum[now.sum+i]!=-1)
            continue;
            nextt.step=now.step+1;
            nextt.sum=now.sum +i;
            nextt.up =i;
            sum[nextt.sum]=nextt.step;
            q.push(nextt);
            }
        }
    }
}
int main() 
{    
    memset(sum,-1,sizeof(sum));//初始化
    bfs();
    int T;
    cin>>T;
    while(T--)
    {
        int a;
        cin>>a;
        cout<<sum[a]<<endl;
    }
    return 0;
}
                                    

标签:facing,now,dice,sum,BFS,Game,include,side,Dice
来源: https://www.cnblogs.com/Remilia-Scarlet/p/10434275.html