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 = + ;
int b[MAXN][MAXN], R, C, sx, sy, st, sf, len;
int dx[] = {, -, , }, dy[] = {, , -, }, dice[][];
bool vis[MAXN][MAXN][][], ok, first;
vector<pair<int, int> > path;
void build_dice() { //dice[top][face] = right
dice[][] = ;
for (int i = ; i < ; i++) {
for (int j = ; j < ; j++) {
if (i == j) continue;
if (dice[i][ - j]) dice[i][j] = - dice[i][ - j];
if (dice[i][j]) {
dice[j][i] = - dice[i][j];
dice[i][dice[i][j]] = - j;
}
}
}
}
pair<int, int> rotate(int top, int face, int di) {
pair<int, int> n_tf;
if (di == ) {
n_tf.second = face;
n_tf.first = - dice[top][face];
}
else if (di == ) {
n_tf.first = face;
n_tf.second = - top;
}
else if (di == ) {
n_tf.second = face;
n_tf.first = dice[top][face];
}
else {
n_tf.second = top;
n_tf.first = - face;
}
return n_tf;
}
bool inside(int x, int y) {
return x > && y > && 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 = ;
else { ok = ; return;}
vis[x][y][t][f] = ;
for (int i = ; i < ; 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] == -)) {
dfs(nx, ny, nt, nf);
}
if (ok) return;
}
vis[x][y][t][f] = ;
path.pop_back();
}
int main() {
build_dice();
char name[];
while (scanf("%s", name) == && strcmp(name, "END")) {
scanf("%d%d%d%d%d%d", &R, &C, &sx, &sy, &st, &sf);
memset(vis, , sizeof(vis));
memset(b, , sizeof(b));
path.clear(); ok = ; first = ;
for (int i = ; i <= R; i++)
for (int j = ; j <= C; j++)
scanf("%d", &b[i][j]);
dfs(sx, sy, st, sf);
len = path.size();
printf("%s", name);
if (ok)
for (int i = ; i < len; i++) {
if (i % == ) {
if (i >= ) 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 ;
}
A Dicey Problem 骰子难题(Uva 810)的更多相关文章
- UVA 810 - A Dicey Problem(BFS)
UVA 810 - A Dicey Problem 题目链接 题意:一个骰子,给你顶面和前面.在一个起点,每次能移动到周围4格,为-1,或顶面和该位置数字一样,那么问题来了,骰子能不能走一圈回到原地, ...
- poj 1872 A Dicey Problem WA的代码,望各位指教!!!
A Dicey Problem Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 832 Accepted: 278 Des ...
- UVA-810 A Dicey Problem (BFS)
题目大意:滚骰子游戏,骰子的上面的点数跟方格中的数相同时或格子中的数是-1时能把格子滚过去,找一条从起点滚到起点的路径. 题目大意:简单BFS,状态转移时细心一些即可. 代码如下; # include ...
- poj1872A Dicey Problem
Home Problems Status Contest 284:28:39 307:00:00 Overview Problem Status Rank A B C D E F G H ...
- Uva - 810 - A Dicey Problem
根据状态进行bfs,手动打表维护骰子滚动. AC代码: #include <iostream> #include <cstdio> #include <cstdlib&g ...
- UVA 810 A Dicey Promblem 筛子难题 (暴力BFS+状态处理)
读懂题意以后还很容易做的, 和AbbottsRevenge类似加一个维度,筛子的形态,可以用上方的点数u和前面的点数f来表示,相对的面点数之和为7,可以预先存储u和f的对应右边的点数,点数转化就很容易 ...
- UVA 11991 Easy Problem from Rujia Liu?【STL】
题目链接: option=com_onlinejudge&Itemid=8&page=show_problem&problem=3142">https://uv ...
- UVa 11375 - Matches
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...
- UVA 567 Risk【floyd】
题目链接: option=com_onlinejudge&Itemid=8&page=show_problem&problem=508">https://uva ...
随机推荐
- iOS崩溃报告获取一
在AppDelegate.m文件中实现函数 void UncaughtExceptionHandler(NSException *exception) { /** * 获取异常崩溃信息 */ NSAr ...
- Page.ClientScript.RegisterStartupScript不执行问题
c#后台使用Page.ClientScript.RegisterStartupScript在前台注册一段脚本提示,发现没有效果,寻寻觅觅,终于从度娘处找到了原因: 该页面多次使用到了Page.Clie ...
- Lucene初步搜索
Lucene在创立索引后,要进行搜索查询 搜索大概需要5部, 1,读取索引. 2,查询索引. 3,匹配数据. 4,封装匹配结果. 5,获取需要的值. 语言表达能力不好,大概就是分着几部吧. /** * ...
- 寒假ACM训练(二)
放了假的效率明显就低起来,最近也一直在学习Ubuntu,所以一直等到今天才写. 还是在用PC. 真的十分郁闷这个LC-Display,其实从思路上是有很多.不过我最后把他当成8字,分成了七笔. 一直W ...
- 深入了解jquery中的键盘事件
很多时候,我们需要获取用户的键盘事件,下面就一起来看看jquery是如何操作键盘事件的. 一.首先需要知道的是: 1.keydown() keydown事件会在键盘按下时触发. 2.keyup() k ...
- C# 数据结构 线性表(顺序表 链表 IList 数组)
线性表 线性表是最简单.最基本.最常用的数据结构.数据元素 1 对 1的关系,这种关系是位置关系. 特点 (1)第一个元素和最后一个元素前后是没有数据元素,线性表中剩下的元素是近邻的,前后都有元素. ...
- button 垂直分布
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];//button的类型 button.frame = CGRectMak ...
- SQL语句の循环添加数据
declare @i intset @i=1while @i<=1000begininsert into News_ITM(title,msg,subDateTime,author,imageP ...
- 关于PowerDesigner
1. PowerDesigner将所有的小写改为大写:Tools->Model Option->左侧菜单中“Naming conversion”->Column->Code – ...
- Uva_11762 Race to 1
题目链接 题意: 给一个数n, 每次从小于等于n的素数里选一个P, 如果能被n整除, 那么就n就变成n / P. 问: n 变成1的期望. 思路: 设小于等于n的素数有p 个, 其中是n的约数的有g个 ...