- 骰子初始状态顶点为1,每次骰子只可以转90度,即顶点的对面无法到达(骰子的每个面与其对面数字和为7)
- 题中要求最小步数,初始化数组各元素为0x3f3f3f3f
dp[score][top]//得分为score,顶点为top时最小步数
dp[0][1] = 0;
//骰子每次只能转一次
//打好表,在六个顶点,score中找最小步骤,输出即可
for(int i = 0; i <= 10000;i++){
for(int j = 1; j < 7; j++){
//f[i][j]翻转,顶点为j
for(int k = 1; k < 7; k++){
if(k != j && k + j != 7) f[i + k][k] = min(f[i + k][k],f[i][j] + 1);
}
}
}
cin >> t;
int l = 0x3f3f3f3f;
for(int i = 1; i < 7; i++) l = min(f[t][i],l);
if(l != inf)cout << l << endl;
else cout << "-1" << endl;
//bfs() 到达各个点的最小步数,可以增加一个vis数组,广度优先搜索即可
void bfs(){
node cur,t;
cur.sum = cur.step = 0;
cur.top = 1;
queue<node> q;
q.push(cur);
vis[cur.sum] = true;
while(!q.empty()){
cur = q.front();
q.pop();
if(cur.sum == 10000) return;
t.step = cur.step + 1;
for(int i = 1; i < 7; i++){
//翻转
if(i != cur.top && i + cur.top != 7){
t.top = i;
t.sum = cur.sum + i;
if(!vis[t.sum]){
f[t.sum][t.top] = t.step;
vis[t.sum] = true;
q.push(t);
}
}
}
}
}
cin >> t;
if(!vis[t]) cout << "-1" << endl;
else{
l = inf;
for(int i = 1; i < 7; i++) l = min(f[t][i],l);
cout << l << endl;
}