UVA 810 - A Dicey Problem

题目链接

题意:一个骰子,给你顶面和前面。在一个起点,每次能移动到周围4格,为-1,或顶面和该位置数字一样,那么问题来了,骰子能不能走一圈回到原地,输出路径,要求最短,假设有多个最短,依照上下左右输出

思路:读懂题就是水题,就记忆化搜一下就可以,记录状态为位置和骰子顶面。正面(由于有两面就能确定骰子了)

代码:

#include <cstdio>
#include <cstring>
#include <vector>
using namespace std; const int D[4][2] = {-1, 0, 1, 0, 0, -1, 0, 1};
const int N = 15;
char str[25];
int n, m, sx, sy, u, f, to[7][7], g[N][N];
int vis[N][N][7][7]; struct State {
int x, y, u, f;
int pre;
State() {}
State(int x, int y, int u, int f, int pre) {
this->x = x;
this->y = y;
this->u = u;
this->f = f;
this->pre = pre;
}
} Q[10005]; void tra(int &vu, int &vf, int d) {
if (d == 0) {int tmp = vf; vf = 7 - vu; vu = tmp;}
if (d == 1) {int tmp = vu; vu = 7 - vf; vf = tmp;}
if (d == 2) vu = 7 - to[vu][vf];
if (d == 3) vu = to[vu][vf];
} #define MP(a,b) make_pair(a,b)
typedef pair<int, int> pii;
vector<pii> ans; void print(int u) {
if (u == -1) return;
print(Q[u].pre);
ans.push_back(MP(Q[u].x, Q[u].y));
} void bfs() {
ans.clear();
int head = 0, rear = 0;
Q[rear++] = State(sx, sy, u, f, -1);
memset(vis, 0, sizeof(vis));
vis[sx][sy][u][f] = 1;
while (head < rear) {
State u = Q[head++];
for (int i = 0; i < 4; i++) {
State v = u;
v.x += D[i][0];
v.y += D[i][1];
if (v.x <= 0 || v.x > n || v.y <= 0 || v.y > m) continue;
if (g[v.x][v.y] != -1 && u.u != g[v.x][v.y]) continue;
if (v.x == sx && v.y == sy) {
print(head - 1);
ans.push_back(MP(sx, sy));
int tot = ans.size();
for (int i = 0; i < tot; i++) {
if (i % 9 == 0) printf("\n ");
printf("(%d,%d)%c", ans[i].first, ans[i].second, i == tot - 1 ? '\n' : ',');
}
return;
}
tra(v.u, v.f, i);
if (vis[v.x][v.y][v.u][v.f]) continue;
vis[v.x][v.y][v.u][v.f] = 1;
v.pre = head - 1;
Q[rear++] = v;
}
}
printf("\n No Solution Possible\n");
} int main() {
to[1][2] = 4; to[1][3] = 2; to[1][4] = 5; to[1][5] = 3;
to[2][1] = 3; to[2][3] = 6; to[2][4] = 1; to[2][6] = 4;
to[3][1] = 5; to[3][2] = 1; to[3][5] = 6; to[3][6] = 2;
to[4][1] = 2; to[4][2] = 6; to[4][5] = 1; to[4][6] = 5;
to[5][1] = 4; to[5][3] = 1; to[5][4] = 6; to[5][6] = 3;
to[6][2] = 3; to[6][3] = 5; to[6][4] = 2; to[6][5] = 4;
while (~scanf("%s", str) && strcmp(str, "END")) {
printf("%s", str);
scanf("%d%d%d%d%d%d", &n, &m, &sx, &sy, &u, &f);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
scanf("%d", &g[i][j]);
bfs();
}
return 0;
}

版权声明:本文博客原创文章。博客,未经同意,不得转载。

UVA 810 - A Dicey Problem(BFS)的更多相关文章

  1. Uva - 810 - A Dicey Problem

    根据状态进行bfs,手动打表维护骰子滚动. AC代码: #include <iostream> #include <cstdio> #include <cstdlib&g ...

  2. UVA 810 A Dicey Promblem 筛子难题 (暴力BFS+状态处理)

    读懂题意以后还很容易做的, 和AbbottsRevenge类似加一个维度,筛子的形态,可以用上方的点数u和前面的点数f来表示,相对的面点数之和为7,可以预先存储u和f的对应右边的点数,点数转化就很容易 ...

  3. Fire! UVA - 11624 (两步bfs)

    题目链接 题意 人要从迷宫走出去,火会向四个方向同时扩散 分析 两步bfs,先出火到达各地时的时间(设初始时间为0,人每走一步为1s,在着一步内火可以向四周可触及的方向同时扩散),然后在bfs人,人能 ...

  4. UVA - 11624 J - Fire! (BFS)

    题目传送门 J - Fire! Joe works in a maze. Unfortunately, portions of the maze have caught on fire, and the ...

  5. uva 10026 Shoemaker's Problem(排序)

    题目连接:10026 Shoemaker's Problem 题目大意:有一个鞋匠接了n双要修的鞋子, 修每双鞋需要d天,每推迟一天修将亏损val元,问按什么样的顺序修鞋可以保证损失最少,如果有多种情 ...

  6. UVA-810 A Dicey Problem (BFS)

    题目大意:滚骰子游戏,骰子的上面的点数跟方格中的数相同时或格子中的数是-1时能把格子滚过去,找一条从起点滚到起点的路径. 题目大意:简单BFS,状态转移时细心一些即可. 代码如下; # include ...

  7. UVA - 524 Prime Ring Problem(dfs回溯法)

    UVA - 524 Prime Ring Problem Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & % ...

  8. 【暑假】[深入动态规划]UVa 1380 A Scheduling Problem

     UVa 1380 A Scheduling Problem 题目: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=41557 ...

  9. 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 ...

随机推荐

  1. linux grep命令详解(转)

    简介 grep (global search regular expression(RE) and print out the line,全面搜索正则表达式并把行打印出来)是一种强大的文本搜索工具,它 ...

  2. 软件測试系统文章(文件夹&amp;链接在此)

    前言 我会在此账号上写一系列关于软件測试的文章,故在此置顶软件測试系列文章的文件夹和链接,以方便大家阅读! 文件夹 软件測试系列之入门篇(一) 软件測试系列之了解篇(二) 软件測试系列之黑白盒(三) ...

  3. codeforces #256 A. Rewards

    A. Rewards time limit per test 1 second memory limit per test 256 megabytes input standard input out ...

  4. IOS设计模式学习(21)享元

    1 前言 在面向对象软件设计中,利用公共对象不仅能节省资源还能提高性能.共享的对象只能提供某些内在的信息,而不能用来识别对象.专门用于设计可共享对象的一种设计模式叫做享元模式(Flyweight pa ...

  5. mysqldump: Couldn't execute 'show events': Cannot proceed because system tables used by Event Schedu

    最近将老版本的mysql 实例倒入 percona 5.5.30,使用的是线上的全备,结果将mysql 库下的表也倒入了,这下可悲剧了,备份报错. 没办法,将mysql库下的数据倒出来,清空,再倒入p ...

  6. Web Socket rfc6455 握 (C++)

    std::string data((const char*)buf->data(),bytes_transferred); recycle_buffer(buf); std::string ke ...

  7. 泛泰A860 Andorid4.4.3 KTU84M (Omni) 图赏

    Omni4.4.3 For Pantech A860L/K/S watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvc3lob3N0/font/5a6L5L2T/ ...

  8. C#读取excel等表格常用方法

    0. 利用NPOI. 请查阅此插件的相关文档. 1.方法一:采用OleDB读取EXCEL文件: 把EXCEL文件当做一个数据源来进行数据的读取操作,实例如下: 1 2 3 4 5 6 7 8 9 10 ...

  9. 【原创】leetCodeOj --- Majority Element 解题报告(脍炙人口的找n个元素数组中最少重复n/2次的元素)

    题目地址: https://oj.leetcode.com/problems/majority-element/ 题目内容: Given an array of size n, find the ma ...

  10. Java应用中使用ShutdownHook友好地清理现场(转)

    在线上Java程序中经常遇到进程程挂掉,一些状态没有正确的保存下来,这时候就需要在JVM关掉的时候执行一些清理现场的代码.Java中得ShutdownHook提供了比较好的方案. JDK在1.3之后提 ...