poj1872A Dicey Problem
Home | Problems | Status | Contest |
A
B
C
D
E
F
G
H
I
J
K
L
M
N
O
Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u
Status
Description

To move through the maze you must tip the die over on an edge to land on an adjacent square, effecting horizontal or vertical movement from one square to another. However, you can only move onto a square that contains the same number as the number displayed on the top of the die before the move, or onto a "wild" square which contains a star. Movement onto a wild square is always allowed regardless of the number currently displayed on the top of the die. The goal of the maze is to move the die off the starting square and to then find a way back to that same square.
For example, at the beginning of the maze there are two possible moves. Since the 5 is on top of the die, it is possible to move down one square, and since the square to the left of the starting position is wild it is also possible to move left. If the first move chosen is to move down, this brings the 6 to the top of the die and moves are now possible both to the right and down. If the first move chosen is instead to the left, this brings the 3 to the top of the die and no further moves are possible.
If we consider maze locations as ordered pairs of row and column numbers (row, column) with row indexes starting at 1 for the top row and increasing toward the bottom, and column indexes starting at 1 for the left column and increasing to the right, the solution to this simple example maze can be specified as: (1,2), (2,2), (2,3), (3,3), (3,2), (3,1), (2,1), (1,1), (1,2). A bit more challenging example maze is shown in Figure 3.
The goal of this problem is to write a program to solve dice mazes. The input file will contain several mazes for which the program should search for solutions. Each maze will have either a unique solution or no solution at all. That is, each maze in the input may or may not have a solution, but those with a solution are guaranteed to have only one unique solution. For each input maze, either a solution or a message indicating no solution is possible will be sent to the output.
Input
Output
Sample Input
DICEMAZE1
3 3 1 2 5 1
-1 2 4
5 5 6
6 -1 -1
DICEMAZE2
4 7 2 6 3 6
6 4 6 0 2 6 4
1 2 -1 5 3 6 1
5 3 4 5 6 4 2
4 1 2 0 3 -1 6
DICEMAZE3
3 3 1 1 2 4
2 2 3
4 5 6
-1 -1 -1
END
Sample Output
DICEMAZE1
(1,2),(2,2),(2,3),(3,3),(3,2),(3,1),(2,1),(1,1),(1,2)
DICEMAZE2
(2,6),(2,5),(2,4),(2,3),(2,2),(3,2),(4,2),(4,1),(3,1),
(2,1),(2,2),(2,3),(2,4),(2,5),(1,5),(1,6),(1,7),(2,7),
(3,7),(4,7),(4,6),(3,6),(2,6)
DICEMAZE3
No Solution Possible
用了个bfs就过了,水题,在最后格式输出的时候错了几次,做题的时候,不防做个小正方形,这样省去了大量的想象,速度也快的多!
#include <iostream>
#include <stdio.h>
#include<string.h>
#include<string>
#include <queue>
using namespace std;
int row,column,up,down,n,m,left,right,front,back,startx,starty;
struct mazntree{int x[7];string re;int row,colomn;};
mazntree start,p,temp;
int visit[7][7][50][50];
int map[100][100];
int dir[4][2]={{0,-1},{1,0},{0,1},{-1,0}};
int init()
{
scanf("%d%d%d%d%d%d",&n,&m,&startx,&starty,&up,&front);
if(up==1)
{
if(front==2)
{
start.x[1]=1,start.x[2]=6,start.x[3]=4,start.x[4]=3,start.x[5]=2,start.x[6]=5;
}
else if(front==3)
{
start.x[1]=1,start.x[2]=6,start.x[3]=2,start.x[4]=5,start.x[5]=3,start.x[6]=4;
}
else if(front==4)
{
start.x[1]=1,start.x[2]=6,start.x[3]=5,start.x[4]=2,start.x[5]=4,start.x[6]=3;
}
else if( front==5)
{
start.x[1]=1,start.x[2]=6,start.x[3]=3,start.x[4]=4,start.x[5]=5,start.x[6]=2;
}
}
else if(up==2)
{
if(front==1)
{
start.x[1]=2,start.x[2]=5,start.x[3]=3,start.x[4]=4,start.x[5]=1,start.x[6]=6;
}
else if(front==3)
{
start.x[1]=2,start.x[2]=5,start.x[3]=6,start.x[4]=1,start.x[5]=3,start.x[6]=4;
}
else if(front==4)
{
start.x[1]=2,start.x[2]=5,start.x[3]=1,start.x[4]=6,start.x[5]=4,start.x[6]=3;
}
else if( front==6)
{
start.x[1]=2,start.x[2]=5,start.x[3]=4,start.x[4]=3,start.x[5]=6,start.x[6]=1;
}
}
else if(up==3)
{
if(front==1)
{
start.x[1]=3,start.x[2]=4,start.x[3]=5,start.x[4]=2,start.x[5]=1,start.x[6]=6;
}
else if(front==2)
{
start.x[1]=3,start.x[2]=4,start.x[3]=1,start.x[4]=6,start.x[5]=2,start.x[6]=5;
}
else if(front==5)
{
start.x[1]=3,start.x[2]=4,start.x[3]=6,start.x[4]=1,start.x[5]=5,start.x[6]=2;
}
else if( front==6)
{
start.x[1]=3,start.x[2]=4,start.x[3]=2,start.x[4]=5,start.x[5]=6,start.x[6]=1;
}
}
else if(up==4)
{
if(front==1)
{
start.x[1]=4,start.x[2]=3,start.x[3]=2,start.x[4]=5,start.x[5]=1,start.x[6]=6;
}
else if(front==2)
{
start.x[1]=4,start.x[2]=3,start.x[3]=6,start.x[4]=1,start.x[5]=2,start.x[6]=5;
}
else if(front==5)
{
start.x[1]=4,start.x[2]=3,start.x[3]=1,start.x[4]=6,start.x[5]=5,start.x[6]=2;
}
else if( front==6)
{
start.x[1]=4,start.x[2]=3,start.x[3]=5,start.x[4]=2,start.x[5]=6,start.x[6]=1;
}
}
else if(up==5)
{
if(front==1)
{
start.x[1]=5,start.x[2]=2,start.x[3]=4,start.x[4]=3,start.x[5]=1,start.x[6]=6;
}
else if(front==3)
{
start.x[1]=5,start.x[2]=2,start.x[3]=1,start.x[4]=6,start.x[5]=3,start.x[6]=4;
}
else if(front==4)
{
start.x[1]=5,start.x[2]=2,start.x[3]=6,start.x[4]=1,start.x[5]=4,start.x[6]=3;
}
else if( front==6)
{
start.x[1]=5,start.x[2]=2,start.x[3]=3,start.x[4]=4,start.x[5]=6,start.x[6]=1;
}
}
else if(up==6)
{
if(front==2)
{
start.x[1]=6,start.x[2]=1,start.x[3]=3,start.x[4]=4,start.x[5]=2,start.x[6]=5;
}
else if(front==3)
{
start.x[1]=6,start.x[2]=1,start.x[3]=5,start.x[4]=2,start.x[5]=3,start.x[6]=4;
}
else if(front==4)
{
start.x[1]=6,start.x[2]=1,start.x[3]=2,start.x[4]=5,start.x[5]=4,start.x[6]=3;
}
else if( front==5)
{
start.x[1]=6,start.x[2]=1,start.x[3]=4,start.x[4]=3,start.x[5]=5,start.x[6]=2;
}
}
int i,j;
for(i=1;i<=n;i++)
{
for(j=1;j<=m;j++)
{
scanf("%d",&map[i][j]);
}
}
return 1;
}
bool changecan(int i)//判定能否走
{
int x[7];
for( int j=1;j<7;j++)
{
x[j]=p.x[j];
}
p.row=p.row+dir[i][0];
p.colomn=p.colomn+dir[i][1];
if((map[p.row][p.colomn]!=0)&&p.row>=1&&p.row<=n&&p.colomn>=1&&p.colomn<=m&&((p.x[1]==map[p.row][p.colomn])||(map[p.row][p.colomn]==-1)))//不越界
{ if( i==0)//左移
{
p.x[3]=x[1];
p.x[4]=x[2];
p.x[2]=x[3];
p.x[1]=x[4];
p.re=temp.re+'0';
}
else if(i==1)//下称
{
p.x[5]=x[1];
p.x[6]=x[2];
p.x[2]=x[5];
p.x[1]=x[6];
p.re=temp.re+'1'; }
else if ( i==2)//右移
{
p.x[4]=x[1];
p.x[3]=x[2];
p.x[1]=x[3];
p.x[2]=x[4];
p.re=temp.re+'2'; }
else if (i==3)//上移
{
p.x[6]=x[1];
p.x[5]=x[2];
p.x[1]=x[5];
p.x[2]=x[6];
p.re=temp.re+'3';
}
if(visit[p.x[1]][p.x[5]][p.row][p.colomn]==0)//没有访问过-1能走,0不能走
{
visit[p.x[1]][p.x[5]][p.row][p.colomn]=1;//标记为已访问
return true;
}
else
return false;
}
else
{
return false;
}
return false;
}
int bfs()
{
queue<mazntree> q;
int i,xx,yy;
memset(visit ,0,sizeof(visit));
while(!q.empty())//先清空
{
q.pop();
}
start.re="";
start.colomn=starty;
start.row=startx;
q.push(start); while(!q.empty())
{
temp=q.front();
q.pop();
for( i=0;i<4;i++)
{
p=temp;
if(changecan(i))
{ q.push(p);
if(visit[p.x[1]][p.x[5]][p.row][p.colomn]&&(p.row==startx)&&(p.colomn==starty))//找到
{ int count=0;
xx=startx,yy=starty;
count=1;
printf(" (%d,%d),",startx,starty); for( i=0;i<p.re.length()-1;i++,count++)
{
xx=xx+dir[p.re[i]-'0'][0];
yy=yy+dir[p.re[i]-'0'][1];
if(count==0) printf(" "); printf("(%d,%d),",xx,yy); if(count==8)
{
printf("\n");
count=-1;
}
}
xx=xx+dir[p.re[i]-'0'][0];
yy=yy+dir[p.re[i]-'0'][1];
printf("(%d,%d)\n",xx,yy); return 1;
}
}
}
}
printf(" No Solution Possible\n");
return -1;
}
int main()
{
char str[200];
while(1)
{ gets(str);
if(str[0]=='E'&&str[1]=='N'&&str[2]=='D')
break;
init();
printf("%s\n",str);
gets(str);//把最后的回车吸收
bfs();
}
return 0;
}
FAQ | About Virtual Judge | Forum | Discuss | Open Source Project
All Copyright Reserved ©2010-2012
HUST ACM/ICPC TEAM
Anything about the OJ, please ask in the
forum, or contact author:
Isun
Server Time:
poj1872A Dicey Problem的更多相关文章
- 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)
UVA 810 - A Dicey Problem 题目链接 题意:一个骰子,给你顶面和前面.在一个起点,每次能移动到周围4格,为-1,或顶面和该位置数字一样,那么问题来了,骰子能不能走一圈回到原地, ...
- UVA-810 A Dicey Problem (BFS)
题目大意:滚骰子游戏,骰子的上面的点数跟方格中的数相同时或格子中的数是-1时能把格子滚过去,找一条从起点滚到起点的路径. 题目大意:简单BFS,状态转移时细心一些即可. 代码如下; # include ...
- A Dicey Problem 骰子难题(Uva 810)
题目描述:https://uva.onlinejudge.org/external/8/810.pdf 把一个骰子放在一个M x N的地图上,让他按照规定滚动,求滚回原点的最短路径. 思路: 记忆化 ...
- Uva - 810 - A Dicey Problem
根据状态进行bfs,手动打表维护骰子滚动. AC代码: #include <iostream> #include <cstdio> #include <cstdlib&g ...
- BFS广搜题目(转载)
BFS广搜题目有时间一个个做下来 2009-12-29 15:09 1574人阅读 评论(1) 收藏 举报 图形graphc优化存储游戏 有时间要去做做这些题目,所以从他人空间copy过来了,谢谢那位 ...
- 1199 Problem B: 大小关系
求有限集传递闭包的 Floyd Warshall 算法(矩阵实现) 其实就三重循环.zzuoj 1199 题 链接 http://acm.zzu.edu.cn:8000/problem.php?id= ...
- No-args constructor for class X does not exist. Register an InstanceCreator with Gson for this type to fix this problem.
Gson解析JSON字符串时出现了下面的错误: No-args constructor for class X does not exist. Register an InstanceCreator ...
- C - NP-Hard Problem(二分图判定-染色法)
C - NP-Hard Problem Crawling in process... Crawling failed Time Limit:2000MS Memory Limit:262144 ...
随机推荐
- JS链表
链表 我们可以看到在javascript概念中的队列与栈都是一种特殊的线性表的结构,也是一种比较简单的基于数组的顺序存储结构.由于javascript的解释器针对数组都做了直接的优化,不会存在在很多编 ...
- Android EventBus现实 听说你out该
转载请注明出处:http://blog.csdn.net/lmj623565791/article/details/40794879.本文出自:[张鸿洋的博客] 1.概述 近期大家面试说常常被问到Ev ...
- HDU 3094 A tree game 树删边游戏
叶节点SG值至0 非叶节点SG值至于它的所有子节点SG值添加1 XOR和后 #include <cstdio> #include <cstring> #include < ...
- 探讨css中repaint和reflow
(个人blog迁移文章.) 前言: 页面设计中,不可避免的需要浏览器进行repaint和reflow.那到底什么是repaint和reflow呢.下面谈谈自己对repaint和reflow的理解,以及 ...
- C++ Primer 学习笔记_63_重载运算符和转换 --转换和类类型【上】
重载运算符和转换 --转换与类类型[上] 引言: 在前面我们提到过:能够用一个实參调用的位 unsignedchar 相同范围的值,即:0到255. 这个类能够捕获下溢和上溢错误,因此使用起来比内置u ...
- 快速构建Windows 8风格应用30-应用生命周期管理
原文:快速构建Windows 8风格应用30-应用生命周期管理 引言 Windows 8 中可以启动多个应用并在其中切换,我们没有必要担心降低系统速度或消耗电池电量. 因为系统会自动挂起(有时会终止) ...
- leetcode第33题--Search for a Range
Given a sorted array of integers, find the starting and ending position of a given target value. You ...
- leetcode第22题--Merge k Sorted Lists
problem:Merge k sorted linked lists and return it as one sorted list. Analyze and describe its compl ...
- Oracle入门4-REF Cursor
Oracle入门4-REF Cursor 转自:http://blog.sina.com.cn/s/blog_55dbebb00100gxsc.html 自:http://blog.csdn.net/ ...
- 【推荐分享】大量JavaScript/jQuery电子书籍教程pdf合集下载
不收藏是你的错^_^. 经证实,均可免费下载. 资源名称 资源大小 15天学会jQuery(完整版).pdf 274.79 KB 21天学通JavaScript(第2版)-顾宁燕扫描版.pdf ...