UVA 810 - A Dicey Problem(BFS)

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;
}
时间: 2024-07-30 03:47:37

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

UVA 810 - A Dicey Problem

这道题目题意理解起来好难....以至于不想做 抄题意:! 将筛子某在某一个初始位置. 知道他的初始状态(由顶部点数 和 最前面的点数来确定) 来往四个方向进行翻转. 能够翻转的条件:当且仅当如今的顶部的点数与下一个位置的点数同样. 假设能够翻转回来. 输出路径. 难点在于,how to know the die just rely on the front and top side>.... 能够打一个大~~~~表.. . 事实上不用所有打表~由于骰子相对的两面的和 = 7: 然后是路径输出.这

uva 10651 Pebble Solitaire (BFS)

uva 10651 Pebble Solitaire Pebble solitaire is an interesting game. This is a game where you are given a board with an arrangement of small cavities, initially all but one occupied by a pebble each. The aim of the game is to remove as many pebbles as

UVA - 439 - Knight Moves (BFS)

UVA - 439 Knight Moves Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description A friend of you is doing research on the Traveling Knight Problem (TKP) where you are to find the shortest closed tour of knigh

uva 11624 Fire!(BFS)

Fire! Time Limit: 1000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description Problem B: Fire! Joe works in a maze. Unfortunately, portions of the maze have caught on fire, and the owner of the maze neglected to create a f

uva 532 Dungeon Master(BFS)

uva 532 Dungeon Master You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is composed of unit cubes which may or may not be filled with rock. It takes one minute to move one unit north, south, east, west, up or down. Y

UVA 810 筛子难题(BFS)

The three-by-three array in Figure 1 is a maze. A standard six-sided die is needed to traverse the maze (the layout of a standard six--sided die is shown in Figure 2). Each maze has an initial position and an initial die configuration. In Figure 1, t

UVA 10047 - The Monocycle(BFS)

题目链接:点击打开链接 题意:从起点到终点,每秒可以选择前进.向左.向右转, 每前进一格轮子转到下一个颜色, 一共5中颜色, 开始的时候绿色接触地面,朝北, 要求最后也绿色接触地面,求能否到达目标点以及最短时间. 思路:和普通BFS相比,多了两个附加条件,所以要将状态表示全面,也要对应加两维. 水题. 细节参见代码: #include<cstdio> #include<cstring> #include<algorithm> #include<iostream&g

UVA - 10047 The Monocycle (BFS)

题目大意:有一个n*m的网格,网格上面有的地方有障碍物 现在有一个人,骑着独轮车,要求从一个地方到达另一个地方,骑独轮车时,只能直走,或者左拐,右拐,不能向后走 独轮车的轮子被分成了5部分,每部分都有对应的颜色,刚开始时是绿色向下,当经过一个格子时,颜色就会变换 问从起点出发到终点,到终点时独轮车的绿色颜色向下,需要多久 解题思路:暴力BFS #include <cstdio> #include <cstring> #include <algorithm> #inclu

UVA-810 A Dicey Problem (BFS)

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