Hdu3713 double maze

Source: Internet
Author: User
Tags bit set
Problem descriptionunlike single maze, double maze requires a common sequence of commands to solve both mazes. See the figure below for a quick understanding.



A maze is made up of 6*6 cells. A cell can be either a hole or a square. moreover, a cell may be surrounded by barriers. there is only one start cell (with a ball) and only one end cell (with a star) in a single maze. these two cells are both squares. it is possible that the Start Cell and the end cell are the same one. the goal of a single maze is to move the ball from the start cell to the end cell. there are four commands in total, 'l', 'D', 'R' and 'U' corresponding to moving the ball left, down, right and up one cell, respectively. the barriers may make the commands take no effect, I. E ., the ball does not move if there is a barrier on the way.
When the ball gets to a hole or outside of the maze, it fails. A double maze is made up of two single mazes. the commands control two bils simultaneously, and the movements of two bils are according to the rules described above independently. both bils will continue to move simultaneously if at least one of the bils has not got to the end cell.
So, a ball may move out of the end cell since the other ball has not been to the target. A double maze passes when both bils get to their end cells, or fails if either of the two mazes fails. the goal of double maze is to get the shortest sequence of commands to pass. if there are multiple solutions, get the lexical minimum one.
To simplify the input, a cell is encoded to an integer as follows. the lowest 4 BITs signal the existence of the barriers around a cell. the maximum th bit indicates whether a cell is a hole or not. the sixth and seventh bits are set for the start cell and end cell. details are listed in the following table with bits counted from lowest Bit. for a barrier, both of the two adjacent cells will have the corresponding barrier bit set. note that the first two mazes in the sample input is the encoding of two mazes in the figure above, make sure you understand the encoding right.
 

 

Inputthe first line of input gives the total number of mazes, t (1 <T ≤ 20 ). then follow T mazes. each maze is a 6*6 matrix, representing the encoding of the original maze. there is a blank line between mazes.

 

Outputfor every two consecutive mazes, you shoshould treat them as a double maze and output the answer. so there are actually t-1 answers. for each double maze, output the shortest sequence of commands to pass. if there are multiple solutions, output the lexicographically minimum one. if there is no way to pass, output-1 instead.

 

Sample input316 0 18 18 2420 19 24 16 28 118 28 17 0 22 1725 20 17 18 88 202 16 28 17 1624 16 16 20 23 116 0 18 18 2420 19 24 20 29 118 28 17 16 22 178 20 1 18 24 2019 80 48 24 16 024 16 16 16 16 22 1918 16 18 18 8024 18 24 16 24 1818 24 0 0 18 2424 18 0 0 24 1818 24 18 16 18 2456 18 24 18 24 18

 

Sample outputrrlulllrrdlurdrlllurdulurrrddu

 

Authorgao, Yuan

 

Source2010 Asia Chengdu Regional Contest

 

Recommendzhengfeng

Question: There are two maze tables. Each Maze has its own starting and ending points. Some grids may not work, and there may be guardrails between grids. At the same time, the two mazes are controlled to move in the same direction each time. The question is the number of steps that make the two Maxes reach the destination at the same time. There are multiple optimal solutions to output the smallest Lexicographic Order.

Idea: combine two graphs into one, create an edge, and use BFs, which is 6 to the power of 4. You can search by dlru to minimize the Lexicographic Order. Use an array to record its leading point. Find the answer and output it backwards.

#include<cstdio>#include<cstring>#include<queue> using namespace std;#define two(x) (1<<x)#define inf 200000000int  T;char c[4]={‘D‘,‘L‘,‘R‘,‘U‘};int d[2][37][4],st[2],en[2],f[37][37];bool can[2][37];int fa1[37][37],fa2[37][37],dir[37][37],ans[2000];void init(int cur){memset(d[cur],0,sizeof(d[cur]));memset(can[cur],1,sizeof(can[cur]));int x;for (int i=1;i<=6;++i)for (int j=1;j<=6;++j){int t=(i-1)*6+j;scanf("%d",&x);if (x & two(1)) d[cur][t][0]=t;else if (i<6) d[cur][t][0]=t+6;if (x & two(0)) d[cur][t][1]=t;else if (j>1) d[cur][t][1]=t-1;if (x & two(2)) d[cur][t][2]=t;else if (j<6) d[cur][t][2]=t+1;if (x & two(3)) d[cur][t][3]=t;else if (i>1) d[cur][t][3]=t-6;if ((x & two(4))==0) can[cur][t]=false;if (x & two(5)) st[cur]=t;if (x & two(6)) en[cur]=t;}}void bfs(){queue<int> q1,q2;bool p[37][37];memset(p,1,sizeof(p));q1.push(st[0]);q2.push(st[1]);p[st[0]][st[1]]=false;while (!q1.empty()){int x=q1.front(),y=q2.front();q1.pop();q2.pop();for (int i=0;i<=3;++i){int tx=d[0][x][i],ty=d[1][y][i];if (tx && ty && can[0][tx] && can[1][ty])if (p[tx][ty]) {q1.push(tx);q2.push(ty);p[tx][ty]=false;fa1[tx][ty]=x;fa2[tx][ty]=y;dir[tx][ty]=i;}}}}void solve(){memset(dir,-1,sizeof(dir));dir[st[0]][st[1]]=5;bfs();int x=en[0],y=en[1];if (dir[x][y]==-1){printf("-1\n");return;}int tot=0,tx,ty;while (!(x==st[0] && y==st[1])){ans[++tot]=dir[x][y];tx=fa1[x][y];ty=fa2[x][y];x=tx;y=ty;}for (int i=tot;i>0;--i)printf("%c",c[ans[i]]);printf("\n");}int main(){scanf("%d",&T);init(1);for (int i=2;i<=T;++i){init(1 & i);solve();}return 0;}

  

Hdu3713 double maze

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.