D - Dice Game Gym - 101502D

#include <iostream>
#include <queue>
#include <bits/stdc++.h>
#include <string.h>
using namespace std;

struct node
{
    int x,y,z;
};

node n,m;
int flag[100010];
void bfs(int b)
{
    int i;
    n.x=1;
    n.y=0;
    n.z=0;
    flag[0]=1;
    queue<node>q;
    q.push(n);
    while(!q.empty())
    {
        n=q.front();
        q.pop();
        if(n.y==b)
        {
            cout << n.z << endl;
            return;
        }
        else if(b-n.y<=6&&(b-n.y)!=n.x&&(b-n.y)!=(7-n.x))
        {
            cout << n.z+1 << endl;
            return;
        }
        for(i=1; i<=6; i++)
        {
            if(i!=n.x&&i!=(7-n.x))
            {
                m.y=n.y+i;
                if(flag[m.y]==0)
                {
                    m.x=i;
                    m.z=n.z+1;
                    q.push(m);
                    flag[m.y]=1;
                }
            }
        }
    }
    cout << -1 << endl;
}
int main()
{
    int a,b;
    cin >> a;
    while(a--)
    {
        memset(flag,0,sizeof(flag));
        cin >> b;
        if(b==1)
        {
            cout << -1 << endl;
        }
        else
        {
            bfs(b);
        }
    }
    return 0;
}

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.

#include<iostream>
#include<queue>
#include<map>
#include<stdio.h>
#include<cstring>
#define mem(a,x) memset(a,x,sizeof(a));
using namespace std;
int n1[4]= {2,3,4,5};
int n2[4]= {1,3,4,6};
int n3[4]= {1,2,5,6};
typedef pair<int,int>P;
const int maxn=10010;
int ans[maxn];  //下标为和,对应值为反转次数
void BFS()
{
    queue<P>que;
    que.push(P(1,0));  //first为当前骰子朝上的值,second为总和sum
    ans[0]=0;
    while(!que.empty())
    {
        P p=que.front();
        que.pop();
        if(p.second>1e4)
        {
            continue;
        }
        if(p.first==1||p.first==6)
        {
            for(int i=0; i<4; i++)
            {
                P temp(n1[i],p.second+n1[i]);
                if(ans[temp.second]==-1)
                {
                    que.push(temp);
                    ans[temp.second]=ans[p.second]+1;
                }
            }
        }
        else if(p.first==2||p.first==5)
        {
            for(int i=0; i<4; i++)
            {
                P temp(n2[i],p.second+n2[i]);
                if(ans[temp.second]==-1)
                {
                    que.push(temp);
                    ans[temp.second]=ans[p.second]+1;
                }
            }
        }
        else
        {
            for(int i=0; i<4; i++)
            {
                P temp(n3[i],p.second+n3[i]);
                if(ans[temp.second]==-1)
                {
                    que.push(temp);
                    ans[temp.second]=ans[p.second]+1;
                }
            }
        }
    }
}

int main()
{
    int t,n;
    mem(ans,-1);
    scanf("%d",&t);
    BFS();
    while(t--)
    {
        scanf("%d",&n);
        cout<<ans[n]<<endl;
    }
    return 0;
}

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.

此题有很多种方法,可以查一查

 

#include <iostream>
#include<stdio.h>
using namespace std;
typedef long long ll;
int main()
{
    int x,n,t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        if(n==1)
        {
            printf("-1\n");
        }
        else if(n==7)
        {
            printf("3\n");
        }
        else
        {
            int ans=n/11*2;
            x=n%11;
            if(x<=5)
            {
                ans++;
            }
            else
            {
                ans+=2;
            }
            if(x==0)
            {
                ans--;
            }
            printf("%d\n",ans);
        }
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ZZ --瑞 hopeACMer

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值