A Dicey Problem 骰子难题(Uva 810)
题目描述:https://uva.onlinejudge.org/external/8/810.pdf
把一个骰子放在一个M x N的地图上,让他按照规定滚动,求滚回原点的最短路径。
思路: 记忆化搜索(我这里用的dfs深度优先搜索)
难点:
如何推导骰子的状态
我没有用打表,而是先用build_dice()初始化二维数组dice, 这样,当我们有骰子的顶面top,正面face, 就可以通过dice[top][face] 获得对应的右面,由此,就可以实现骰子的旋转。rotate()函数实现骰子的旋转,给它top, face, 和旋转的方向(前,后,左,右), 它会返回旋转后骰子的top, face.
记忆化搜索要多一个状态记录骰子的状态
说白了就是记录访问状态的数组vis要多两个维度分别记录top, face,因为top, face一旦固定,骰子就固定了
Note: 输出的格式也要稍微注意一下,锁进、末尾的逗号,换行要处理好。
#include <iostream> #include <cstdio> #include <vector> #include <utility> #include <cstring>using namespace std; const int MAXN = 10 + 2; int b[MAXN][MAXN], R, C, sx, sy, st, sf, len; int dx[] = {0, -1, 0, 1}, dy[] = {1, 0, -1, 0}, dice[7][7]; bool vis[MAXN][MAXN][7][7], ok, first; vector<pair<int, int> > path;void build_dice() { //dice[top][face] = rightdice[1][2] = 3;for (int i = 1; i < 7; i++) {for (int j = 1; j < 7; j++) {if (i == j) continue;if (dice[i][7 - j]) dice[i][j] = 7 - dice[i][7 - j];if (dice[i][j]) {dice[j][i] = 7 - dice[i][j];dice[i][dice[i][j]] = 7 - j;}}} }pair<int, int> rotate(int top, int face, int di) {pair<int, int> n_tf;if (di == 0) {n_tf.second = face;n_tf.first = 7 - dice[top][face];}else if (di == 1) {n_tf.first = face;n_tf.second = 7 - top;}else if (di == 2) {n_tf.second = face;n_tf.first = dice[top][face];}else {n_tf.second = top;n_tf.first = 7 - face;}return n_tf; }bool inside(int x, int y) {return x > 0 && y > 0 && x <= R && y <= C; }void dfs(int x, int y, int t, int f) {path.push_back(make_pair(x, y));if (x == sx && y == sy)if (first) first = 0;else { ok = 1; return;}vis[x][y][t][f] = 1;for (int i = 0; i < 4; i++) {pair<int, int> nextp = rotate(t, f, i);int nx = x + dx[i], ny = y + dy[i];int nt = nextp.first, nf = nextp.second;if (inside(nx, ny) && (!vis[nx][ny][nt][nf] || (nx == sx && ny == sy)) && (t == b[nx][ny] || b[nx][ny] == -1)) {dfs(nx, ny, nt, nf);}if (ok) return;}vis[x][y][t][f] = 0;path.pop_back(); }int main() {build_dice();char name[25];while (scanf("%s", name) == 1 && strcmp(name, "END")) {scanf("%d%d%d%d%d%d", &R, &C, &sx, &sy, &st, &sf);memset(vis, 0, sizeof(vis));memset(b, 0, sizeof(b));path.clear(); ok = 0; first = 1;for (int i = 1; i <= R; i++)for (int j = 1; j <= C; j++)scanf("%d", &b[i][j]);dfs(sx, sy, st, sf);len = path.size();printf("%s", name);if (ok)for (int i = 0; i < len; i++) {if (i % 9 == 0) {if (i >= 9) printf(",");printf("\n (%d,%d)", path[i].first, path[i].second);}else printf(",(%d,%d)", path[i].first, path[i].second);}else printf("\n No Solution Possible");printf("\n");}return 0; }
转载于:https://www.cnblogs.com/Bowen-/p/4935750.html
A Dicey Problem 骰子难题(Uva 810)相关推荐
- UVa810 A Dicey Problem 筛子难题
将一个筛子放在M*N的地图上,每次翻滚前翻滚后的位置的点数必须要与翻滚前筛子的上面的点数相同,给定起点,找出一条可循路径,返回起点. 这题好像没有强调最短路径,可以用dfs做,我TL了,用的bfs,可 ...
- poj 1872 A Dicey Problem (bfs+WordFinal题)
居然是一道WF题,虽然是很早的,但也超有成就感啊!! 4维判重,分别是x,y坐标和骰子上面和骰子前面的号码. 很水,拿个骰子或者纸盒比划下就好了. #include<cstdio> #in ...
- uva 11795 - Mega Man's Mission 洛克人的难题 基础集合动态规划
题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...
- UVA - 524:Prime Ring Problem
Prime Ring Problem 来源:UVA 题目 A ring is composed of n (even number) circles as shown in diagram. Put ...
- 【OI】WERTYU UVa 10082
题目: A common typing error is to place the hands on the keyboard one row to the right of the correct ...
- 使用HTML5的canvas元素和js实现一个超简单的随机骰子
这里共定义了四个函数,draw1,draw2,draw4,draw2mid,其中的3个点的面用draw1和draw2组合,五个点的面有draw1和draw4,六个点的面用draw4和draw2. 这个 ...
- 紫书《算法竞赛入门经典》
紫书<算法竞赛入门经典>题目一览 第3章 数组和字符串(例题) UVA 272 TEX Quotes UVA 10082 WERTYU UVA 401 Palindromes UVA 34 ...
- 算法竞赛入门经典(第二版)-刘汝佳-第六章 数据结构基础 习题(12/14)
文章目录 说明 习题 习6-1 UVA 673 平衡的括号 习6-2 UVA 712 S - 树 习6-3 UVA 536 二叉树重建 习6-4 UVA 439 骑士的移动 习6-5 UVA 1600 ...
- 算法竞赛入门经典 习题6-12
UVa810 A Dicey Problem 一个迷宫里有一骰子,骰子可以上下左右移动的条件是相邻格中的数字和骰子朝上面的数字相同,或者相邻格子中为*.求一条从起点出发,最终回到起点的路径. 深搜和广 ...
最新文章
- 接口隔离原则_设计模式六大原则
- webpack简单笔记
- 计算机技能测试题12答案,计算机基本技能考试选择题及答_计算机一级考试练习题及答案...
- linux查看app路径下文件卡死
- 【目标定位】基于matlab卡尔曼滤波UWB-IMU组合定位导航【含Matlab源码 1601期】
- jsp session 的状态保持, cookie的跨域访问(一)
- cplex java_【CPLEX教程02】配置Cplex的Java环境以及API说明
- MySQL基础学习特殊篇 入门限定
- android日历分析,kotlin - Android开发之日历篇(1)
- OpenCV角点检测—Harris,SIFT,ORB(7)
- 配置项目构建完成后文件移动---- Jenkins自动化部署学习笔记(三)
- 生产计划排产软件如何解决生产难题?
- CDay09 联合和枚举
- Kotlin sealed class 的使用
- mariadb Galera集群部署
- Revit二开 批量链接模型
- java中注解动态传参_Java自定义注解源码+原理解释(使用Java自定义注解校验bean传入参数合法性)...
- 第4章-Isochronous Streams
- 南邮Android实验报告三:基于高德地图的综合应用
- php商家入驻系统,商户入驻 · CRMEB 多商户系统 帮助文档 · 看云