A Dicey Problem UVA - 810

题目的意思很简单,给你一个骰子以及骰子的正面以及顶面,同时给你一片区域,这个区域中每个小方格要么是一个数字,要么用0表示不存在,要么用-1表示五角星,骰子在区域中运动的规则是:可以在上下左右运动,不能出界限,同时要么可以运动到五角星小方格,要么运动到的小方格的数字和骰子目前顶面的数字是一样的。现在需要你根据题目中给出的信息判断骰子是否能够从起点开始运动一圈之后再次回到起点。那么这个题目考察的是BFS,首先使用二维数组Right[x][y]记录当目前骰子的正面是x顶面是y的时候右边的数字,这样便于后面骰子运动的时候更新骰子的顶面以及前面的信息,然后就是BFS,利用队列以及一个visit数组记录骰子的各种状态以及该状态是否已经如果队列了,从初始状态开始广度搜索,如果在搜索过程中发现能够回到起点,则递归找出这些节点的下标并且输出坐标信息,如果不能回到起点,当队列为空时跳出循环,输出相应的信息,源代码如下(话说printf、scanf真的比cin、cout快不少啊):

#include<iostream>
#include<vector>
#include<string>
#include<set>
#include<stack>
#include<queue>
#include<map>
#include<algorithm>
#include<cmath>
#include<iomanip>
#include<cstring>
#include<sstream>
#include<cstdio>
#include<deque>
using namespace std;

int dir[4][2] = {-1,0,1,0,0,-1,0,1};//上下左右
int Right[10][10];//前面、顶面---->右面
int R, C, stax, stay, Top, Face;
int visit[15][15][10][10];
int area[15][15];
vector<int> result;

struct node{
	int x, y, Top, Face, pre, index;
	node(int a = 0, int b = 0, int t = 0, int f = 0, int in = -1, int p = -1){
		x = a; y = b; Top = t; Face = f; index = in; pre = p;
	}
};

void overturn(int& t,int& f,int d){
	if (d == 0){//上
		int temp = t;
		t = f;
		f = 7 - temp;
	}
	else if (d == 1){//下
		int temp = f;
		f = t;
		t = 7 - temp;
	}
	else if (d == 2){//左
		t = Right[f][t];
	}
	else{//d==3,右
		t = 7 - Right[f][t];
	}
}

void getResult(int ind,node q[]){
	if (ind == -1) return;
	getResult(q[ind].pre,q);
	result.push_back(ind);
}

void BFS(){
	node q[20000];
	int head = 0, rear = 0;
	q[rear] = node(stax,stay,Top,Face,0,-1);
	rear++;
	while (head < rear){
		node temp = q[head];
		head++;
		for (int i = 0; i < 4; i++){
			int newx, newy;
			newx = temp.x + dir[i][0];
			newy = temp.y + dir[i][1];
			if (!(newx >= 1 && newx <= R&&newy >= 1 && newy <= C)) continue;
			if (area[newx][newy] != -1 && temp.Top != area[newx][newy]) continue;
			if (newx == stax&&newy == stay){
				getResult(temp.index, q);
				cout << "  ";
				for (int j = 0; j < result.size();){
					int ind = result[j];
					cout << "(" << q[ind].x << "," << q[ind].y << ")";
					j++;
					if (j % 9 == 0&&j!=0) cout << ",\n  ";
					else cout << ",";
				}
				cout << "(" << newx << "," << newy << ")" << endl;
				return;
			}
			node temp2 = temp;
			temp2.x = newx; temp2.y = newy;
			overturn(temp2.Top, temp2.Face, i);
			if (visit[temp2.x][temp2.y][temp2.Top][temp2.Face]) continue;
			visit[temp2.x][temp2.y][temp2.Top][temp2.Face] = 1;
			temp2.index = rear;
			temp2.pre = temp.index;
			q[rear] = temp2;
			rear++;
		}
	}
	cout << "  No Solution Possible" << endl;
}

int main(){

	string name;
	Right[1][2] = 4; Right[1][3] = 2; Right[1][5] = 3; Right[1][4] = 5;
	Right[2][1] = 3; Right[2][4] = 1; Right[2][6] = 4; Right[2][3] = 6;
	Right[3][1] = 5; Right[3][2] = 1; Right[3][6] = 2; Right[3][5] = 6;
	Right[4][1] = 2; Right[4][5] = 1; Right[4][6] = 5; Right[4][2] = 6;
	Right[5][6] = 3; Right[5][4] = 6; Right[5][1] = 4; Right[5][3] = 1;
	Right[6][2] = 3; Right[6][4] = 2; Right[6][5] = 4; Right[6][3] = 5;
	while (cin >> name){
		result.clear();
		if (name == "END") break;
		cin >> R >> C >> stax >> stay >> Top >> Face;
		memset(visit,0,sizeof(visit));
		memset(area,0,sizeof(area));
		for (int i = 1; i <= R; i++){
			for (int j = 1; j <= C; j++){
				cin >> area[i][j];
			}
		}
		cout << name << endl;
		BFS();
	}
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值