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 =;
now.step =;
now.up =;
queue<mian> q;
q.push(now);
while(!q.empty())
{
now=q.front();
q.pop();
if(now.sum<=maxn)
{
for(int i=;i<=;i++)//i为下一步翻转后面向上的数字
{
// 当前向上数字和i相同 i在当前数字的背面 翻转后的和已经记录
if(now.up==i||now.up+i==||sum[now.sum+i]!=-)
continue;
nextt.step=now.step+;
nextt.sum=now.sum +i;
nextt.up =i;
sum[nextt.sum]=nextt.step;
q.push(nextt);
}
}
}
}
int main()
{
memset(sum,-,sizeof(sum));//初始化
bfs();
int T;
cin>>T;
while(T--)
{
int a;
cin>>a;
cout<<sum[a]<<endl;
}
return ;
}

D - Dice Game (BFS)的更多相关文章

  1. 深搜(DFS)广搜(BFS)详解

    图的深搜与广搜 一.介绍: p { margin-bottom: 0.25cm; direction: ltr; line-height: 120%; text-align: justify; orp ...

  2. 【算法导论】图的广度优先搜索遍历(BFS)

    图的存储方法:邻接矩阵.邻接表 例如:有一个图如下所示(该图也作为程序的实例): 则上图用邻接矩阵可以表示为: 用邻接表可以表示如下: 邻接矩阵可以很容易的用二维数组表示,下面主要看看怎样构成邻接表: ...

  3. 深度优先搜索(DFS)与广度优先搜索(BFS)的Java实现

    1.基础部分 在图中实现最基本的操作之一就是搜索从一个指定顶点可以到达哪些顶点,比如从武汉出发的高铁可以到达哪些城市,一些城市可以直达,一些城市不能直达.现在有一份全国高铁模拟图,要从某个城市(顶点) ...

  4. 【BZOJ5492】[HNOI2019]校园旅行(bfs)

    [HNOI2019]校园旅行(bfs) 题面 洛谷 题解 首先考虑暴力做法怎么做. 把所有可行的二元组全部丢进队列里,每次两个点分别向两侧拓展一个同色点,然后更新可行的情况. 这样子的复杂度是\(O( ...

  5. 深度优先搜索(DFS)和广度优先搜索(BFS)

    深度优先搜索(DFS) 广度优先搜索(BFS) 1.介绍 广度优先搜索(BFS)是图的另一种遍历方式,与DFS相对,是以广度优先进行搜索.简言之就是先访问图的顶点,然后广度优先访问其邻接点,然后再依次 ...

  6. 图的 储存 深度优先(DFS)广度优先(BFS)遍历

    图遍历的概念: 从图中某顶点出发访遍图中每个顶点,且每个顶点仅访问一次,此过程称为图的遍历(Traversing Graph).图的遍历算法是求解图的连通性问题.拓扑排序和求关键路径等算法的基础.图的 ...

  7. 数据结构与算法之PHP用邻接表、邻接矩阵实现图的广度优先遍历(BFS)

    一.基本思想 1)从图中的某个顶点V出发访问并记录: 2)依次访问V的所有邻接顶点: 3)分别从这些邻接点出发,依次访问它们的未被访问过的邻接点,直到图中所有已被访问过的顶点的邻接点都被访问到. 4) ...

  8. 层层递进——宽度优先搜索(BFS)

    问题引入 我们接着上次“解救小哈”的问题继续探索,不过这次是用宽度优先搜索(BFS). 注:问题来源可以点击这里 http://www.cnblogs.com/OctoptusLian/p/74296 ...

  9. HDU.2612 Find a way (BFS)

    HDU.2612 Find a way (BFS) 题意分析 圣诞节要到了,坤神和瑞瑞这对基佬想一起去召唤师大峡谷开开车.百度地图一下,发现周围的召唤师大峡谷还不少,这对基佬纠结着,该去哪一个...坤 ...

随机推荐

  1. 微信小程序之图片base64解码

    不知道大家在做微信小程序的时候遇到base64解码的问题,我之前在做微信小程序的时候遇到base64解析图片一直有问题,所以在这里把遇到的问题和解决方案在这里记录一下: 在平时的项目中我们是直接用ba ...

  2. javascript中获取字符串或数组中元素的索引

    有些时候,我们需要知道一个字符串中字符的位置,或者一个数组中元素的位置,这是就需要对该变量进行迭代操作. 对于数组,有两个方法indexOf和findIndex() , 需要注意的是,findInde ...

  3. mysql中utf8和utf8mb4区别

    一.什么是utf8mb4 MySQL在5.5.3之后增加了这个utf8mb4的编码,mb4就是most bytes 4的意思,专门用来兼容四字节的unicode.好在utf8mb4是utf8的超集,除 ...

  4. Unity XLua之协程

    如何使用xlua实现协程,示例代码如下: 转载请注明出处:https://www.cnblogs.com/jietian331/p/10735773.html local unpack = unpac ...

  5. 31 Python中 sys.argv[]的用法简明解释(转)

    Python中 sys.argv[]的用法简明解释 因为是看书自学的python,开始后不久就遇到了这个引入的模块函数,且一直在IDLE上编辑了后运行,试图从结果发现它的用途,然而结果一直都是没结果, ...

  6. Fiddler使用教程(转)

    Fiddler是最强大最好用的Web调试工具之一,你对HTTP协议越了解, 你就能越掌握Fiddler的使用方法.你越使用Fiddler,就越能帮助你了解HTTP协议.Fiddler无论对开发人员或者 ...

  7. 2017.11.27 变量进阶与LED矩阵

    局部变量:函数内部声明的变量,只在函数内部有效. 全部变量:在函数外部声明的变量,全局都有效,直到程序执行完毕. 全局变量负作用: 1.降低函数的独立性 2.降低函数的通用性,不利于函数的重复调用. ...

  8. 了解Linux操作系统发展阶段

    一.硬件与软件发展历史 计算机由硬件和软件组成结构 二.Linux的发展史 Linux 操作系统是Unix操作系统的一种克隆系统.它诞生于1991年的10月5日(只是第一次正式向外公布的时间).以后借 ...

  9. SQL*Plus命令

    简介set命令 一般使用SQL PLUS导出数据时一般使用以下参数就可以了. set echo off; -- 不显示脚本中的每个sql命令(缺省为on)set feedback off; -- 禁止 ...

  10. springboot+thymeleaf+pageHelper带条件分页查询

    html层 <div> <a class="num"><b th:text="'共 '+ ${result.resultMap['pages ...