根据状态进行bfs,手动打表维护骰子滚动。

AC代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cctype>
#include <cstring>
#include <string>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <algorithm>
#include <stack>
#include <queue>
#include <bitset>
#include <cassert>
#include <cmath>

using namespace std;

const int maxn = 65536;

const int dx[] = { -1, 1, 0, 0 };
const int dy[] = { 0, 0, -1, 1 };

struct State {
	int x, y, top, front;
	State *prev;
} states[maxn];

int statesIdx;
int n, m, sx, sy, dtop, dfront;
int g[32][32];

State* getNewState(int x, int y, int dtop, int dfront, State *prev = NULL) {
	State *p = &states[statesIdx++];
	assert(statesIdx < maxn);
	p->x = x, p->y = y, p->top = dtop, p->front = dfront, p->prev = prev;
	return p;
}

int diceTable[7][7]; // [front][top] = right

void rotateDice(int dir, int dtop, int dfront, int &rtop, int &rfront) {
	rtop = rfront = 1;
	if (dir == 0) { // 向上滚动
		rtop = dfront, rfront = 7 - dtop;
	}
	else if (dir == 1) { // 向下滚动
		rtop = 7 - dfront, rfront = dtop;
	}
	else if (dir == 2) { // 向左滚动
		rfront = dfront;
		rtop = diceTable[dfront][dtop];
	}
	else if (dir == 3) { // 向右滚动
		rfront = dfront;
		rtop = 7 - diceTable[dfront][dtop];
	}
}

void bfs(int sx, int sy, int dtop, int dfront) {
	statesIdx = 0;
	int used[32][32][7][7] = {}; // used[x][y][dtop][dfront]
	int tx, ty, tdtop, tdfront;
	queue<State*> Q;
	State *u, *v;
	Q.push(getNewState(sx, sy, dtop, dfront));
	used[sx][sy][dtop][dfront] = 1;

	while (!Q.empty()) {
		u = Q.front(), Q.pop();
		for (int i = 0; i < 4; i++) {
			tx = u->x + dx[i], ty = u->y + dy[i];
			if (tx <= 0 || ty <= 0 || tx > n || ty > m) {
				continue;
			}
			if (g[tx][ty] != -1 && g[tx][ty] != u->top) {
				continue;
			}
			if (tx == sx && ty == sy) {
				vector< pair<int, int> > ret;
				State *p = u;
				ret.push_back(make_pair(sx, sy));
				while (p != NULL) {
					ret.push_back(make_pair(p->x, p->y));
					p = p->prev;
				}
				for (int i = ret.size() - 1, j = 0; i >= 0; i--, j++) {
					if (j % 9 == 0)	printf("  ");
					if (j % 9 != 0)	printf(",");
					printf("(%d,%d)", ret[i].first, ret[i].second);
					if (j % 9 == 8 || i == 0) {
						if (i) {
							printf(",\n");
						}
						else {
							puts("");
						}
					}
				}
				return;
			}
			rotateDice(i, u->top, u->front, tdtop, tdfront);
			if (used[tx][ty][tdtop][tdfront]) {
				continue;
			}
			used[tx][ty][tdtop][tdfront] = 1;
			v = getNewState(tx, ty, tdtop, tdfront, u);
			Q.push(v);
		}
	}
	printf("  No Solution Possible\n");
}

int main() {
	// 手动打表维护骰子滚动
	// diceface[front][top] = right
	diceTable[1][2] = 4, diceTable[1][3] = 2, diceTable[1][4] = 5, diceTable[1][5] = 3;
	diceTable[2][1] = 3, diceTable[2][3] = 6, diceTable[2][4] = 1, diceTable[2][6] = 4;
	diceTable[3][1] = 5, diceTable[3][2] = 1, diceTable[3][5] = 6, diceTable[3][6] = 2;
	diceTable[4][1] = 2, diceTable[4][2] = 6, diceTable[4][5] = 1, diceTable[4][6] = 5;
	diceTable[5][1] = 4, diceTable[5][3] = 1, diceTable[5][4] = 6, diceTable[5][6] = 3;
	diceTable[6][2] = 3, diceTable[6][3] = 5, diceTable[6][4] = 2, diceTable[6][5] = 4;

	char testcase[1024];
	while (scanf("%s", testcase) == 1) {
		if (!strcmp("END", testcase))
			break;
		scanf("%d %d %d %d %d %d", &n, &m, &sx, &sy, &dtop, &dfront);
		for (int i = 1; i <= n; i++) {
			for (int j = 1; j <= m; j++) {
				scanf("%d", &g[i][j]);
			}
		}
		printf("%s\n", testcase);
		bfs(sx, sy, dtop, dfront);
	}
	return 0;
}

Uva - 810 - A Dicey Problem的更多相关文章

  1. UVA 810 - A Dicey Problem(BFS)

    UVA 810 - A Dicey Problem 题目链接 题意:一个骰子,给你顶面和前面.在一个起点,每次能移动到周围4格,为-1,或顶面和该位置数字一样,那么问题来了,骰子能不能走一圈回到原地, ...

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

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

  3. UVa 101 The Blocks Problem Vector基本操作

    UVa 101 The Blocks Problem 一道纯模拟题 The Problem The problem is to parse a series of commands that inst ...

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

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

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

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

  6. uva 10837 - A Research Problem(欧拉功能+暴力)

    题目链接:uva 10837 - A Research Problem 题目大意:给定一个phin.要求一个最小的n.欧拉函数n等于phin 解题思路:欧拉函数性质有,p为素数的话有phip=p−1; ...

  7. poj 1872 A Dicey Problem WA的代码,望各位指教!!!

    A Dicey Problem Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 832   Accepted: 278 Des ...

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

  9. UVA 10026 Shoemaker's Problem 鞋匠的难题 贪心+排序

    题意:鞋匠一口气接到了不少生意,但是做鞋需要时间,鞋匠只能一双一双地做,根据协议每笔生意如果拖延了要罚钱. 给出每笔生意需要的天数和每天的罚钱数,求出最小罚钱的排列顺序. 只要按罚款/天数去从大到小排 ...

随机推荐

  1. ionic3-ng4学习见闻--(多环境方案)

    搜了很久,很难找到一个详细入微,开箱即用的方案. 于是我 百折不挠的,搞出来一个,也不知道是不是最完美的方案,有什么可以优化的地方可以指出,谢谢. 首先, 1.项目目录下(与src平级),新增conf ...

  2. 全局变量&局部变量,global&nonlocal

    ###全局变量与局部变量 1.函数内部的变量名如果第一次出现,且出现在=前面,即被视为定义一个局部变量,不管全局域中有没有用到该变量名,函数中使用的将是局部变量 2.函数内部的变量名如果第一次出现,且 ...

  3. 关于Allele(等位基因)的理解

    高中学生物的时候关于遗传学的部分,记得当时的教材上为了简化处理一般将基因型定义为AA, Aa, aa.其实这种抽象的理解对应付高考是很有用的,但是实际应用中如果还这样理解那么便会产生一些疑问.之所以会 ...

  4. 关于bedtools merge 功能中sort 命令的解释

    Bedtools 是一个很好的用来处理区间的工具,很多时候用这个底层语言编写的小工具比自己写的脚本运行快很多,但是这个工具中的某些功能对输入文件有一定的要求,比如说里面的一个merge函数,这是里面的 ...

  5. MySQL 连接的使用

    MySQL 连接的使用 在前几章节中,我们已经学会了如果在一张表中读取数据,这是相对简单的,但是在真正的应用中经常需要从多个数据表中读取数据. 本章节我们将向大家介绍如何使用 MySQL 的 JOIN ...

  6. (二)ROS系统架构及概念 ROS Architecture and Concepts 以Kinetic为主更新 附课件PPT

    第2章 ROS系统架构及概念 ROS Architecture and Concepts PPT说明: 正文用白色,命令或代码用黄色,右下角为对应中文译著页码. 这一章需要掌握ROS文件系统,运行图级 ...

  7. Openstack: MP-BIOS bug: 8254 timer not connected to IO-APIC

    Issue: After you import an linux image into openstack and run an instance of it, you may find that t ...

  8. Android Lollipop 5.0 经典新特性回顾

    *Tamic 专注移动开发! 更多文章请关注 http://blog.csdn.net/sk719887916 虽然Android已到了7.0 ,但是我们还是不能忘怀视觉革命性改变的5.0,今天回顾下 ...

  9. Python 文本转语音

    文本转语音,一般会用在无障碍开发.下面介绍如何使用Python实现将文本文件转换成语音输出. 准备 使用Speech API 原理 示例代码 小总结 pyttsx方式 原理 示例代码 小总结 pytt ...

  10. 用Python递归解决阿拉伯数字转为中文财务数字格式的问题(2)--打开思路的一种方法

    几天前自己写了个将阿拉伯数字转为中文财务数字的程序.用的递归,不幸的是它是树形递归. 虽然实际过程中不太可能出现金额数字大到让Python递归栈溢出,但是始终是一块心病,这玩意终究在理论上是受限制的. ...