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 balls simultaneously,
and the movements of two balls are according to the rules described above independently. Both balls
will continue to move simultaneously if at least one of the balls 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 dou
题意:给n个地图,每相邻的两个地图求一下,两个球在两个地图的从各自起点到终点的最短路,两个求的移动方向是一致的。输出字符方向。
#include <bits/stdc++.h>
using namespace std;
char s[5]={"DLRU"};
const int N=6;
int nxt[4][2] = {{1,0},{0,-1},{0,1},{-1,0}};
struct node
{
int a[N][N];
int mp[N][N][4];
int bx,by,ex,ey;
void read()
{
for(int i = 0; i <= 5; i++)
for(int j = 0; j <= 5; j++)
{
scanf("%d", &a[i][j]);
for(int k = 0; k <= 1; k++) mp[i][j][k^1] = a[i][j] & (1<<k);
//先左再下 为了字典序最小
for(int k = 2; k <= 3; k++) mp[i][j][k] = a[i][j] & (1<<k);
if(a[i][j]&(1<<5)) bx = i, by = j;
if(a[i][j]&(1<<6)) ex = i, ey = j;
a[i][j] = a[i][j] & (1<<4);
}
}
bool check(int x,int y)
{
return x==ex&&y==ey;
}
bool Move(int t,int x,int y,int &xx,int &yy)
{
if(mp[x][y][t]) { xx=x,yy=y;return true;}
int nx=x+nxt[t][0],ny=y+nxt[t][1];
if(nx<0||nx>5||ny<0||ny>5||!a[nx][ny]) return false;
xx=nx,yy=ny; return true;
}
}pp[2];
struct point
{
int x1,y1,x2,y2;
point(){}
point(int _x1,int _y1,int _x2,int _y2):x1(_x1),y1(_y1),x2(_x2),y2(_y2){}
}pre[10][10][10][10];
int f[10][10][10][10];
void output(int x1,int y1,int x2,int y2)
{
if(f[x1][y1][x2][y2]==-2)
return ;
point p=pre[x1][y1][x2][y2];
output(p.x1,p.y1,p.x2,p.y2);
printf("%c",s[f[x1][y1][x2][y2]] );
}
bool bfs(node a,node b)
{
queue<point> q;
f[a.bx][a.by][b.bx][b.by]=-2;
q.push(point(a.bx,a.by,b.bx,b.by));
while(!q.empty())
{
point top=q.front();
q.pop();
if(a.check(top.x1,top.y1)&&b.check(top.x2,top.y2))
{
output(top.x1,top.y1,top.x2,top.y2);
puts("");
return true;
}
int x1,y1,x2,y2;
for(int i=0;i<=3;i++)
{
if(!a.Move(i, top.x1, top.y1, x1,y1))
continue;
if(!b.Move(i,top.x2, top.y2, x2, y2)) continue;
if(f[x1][y1][x2][y2]!=-1) continue;
f[x1][y1][x2][y2]=i;
pre[x1][y1][x2][y2]=top;
q.push(point(x1,y1,x2,y2));
}
}
return false;
}
int main()
{
int n;
int t;
scanf("%d",&n);
for(pp[t=0].read();--n;t^=1)
{
memset(f,-1,sizeof(f));
pp[t^1].read();
if(!bfs(pp[t],pp[t^1])) printf("-1\n");
}
}