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
2
5
10
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; }
上一篇: Scrapy快速上手
推荐阅读
-
cf1064D. Labyrinth(01BFS)
-
cf605D. Board Game(BFS 树状数组 set)
-
POJ 1708 Game G++ 巧妙 bfs 没掌握
-
codeforce 1064D bfs
-
codeforces D. Game With Array
-
Codeforces Round #533 (Div. 2) D. Kilani and the Game(bfs+模拟)
-
Codeforces Round #643 (Div. 2) D. Game With Array
-
Codeforces 1451D. Circle Game(博弈)
-
Atcoder - abc176 -- D - Wizard in Maze【BFS + 双端队列】
-
cf1072D. Minimum path(BFS)