题目描述 一个迷宫里,有两个点,你的任务是给出尽可能短的一系列的命令,让这两个点一起执行这些命令能走到一起。如果某个点按某个命令走会走出迷宫或走到障碍上,则忽略这条命令。 输入 输入不超过1000个样例,每个样例第一行有两个整数n,m(2≤n,m≤11),之后n行每行m个字符,'.'表示能走的地方,'#'表示障碍,‘*'表示其中一个点。每个样例之间有一个空行。 输出 每个样例输出一行,包含'U','D','L','R'。'U'表示向上走的命令,'D'表示向下,'L'表示向左,'R'表示向右。要是有多个答案,输出字典序最小的序列。如果无法达到目标,输出“Sorry”(引号不要输出)。 样例输入 4 11
*.##..#..#*
...#..#.#..
.##.#.#.##.
##..###....
4 4
.*..
.###
...*
....
10 10
*.........
#########.
..........
.#########
..........
#########.
..........
.#########
.........*
##########
样例输出 Sorry
LLLUU
LLLLLLLLLUURRRRRRRRRUULLLLLLLLLUURRRRRRRRRDD
Source XTU OnlineJudge 解题思路:和HDU的Double Maze 基本一样,也是用四维数组存状态,但放在一个迷宫中,比HDU的还是要简单些。 #include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
const int N = 15;
struct Point{
int x, y;
int xx, yy;
int x1, y1, x2, y2;
}fr, next, start, prev[N][N][N][N];
char mp[N][N];
int vis[N][N][N][N];
int dir[4][2] = {1, 0, 0, -1, 0, 1, -1, 0};
char s[4] = {'D', 'L', 'R', 'U'};
int n, m;
void print(int x, int y, int xx, int yy){
if(x == start.x && y == start.y && xx == start.xx && yy == start.yy)
return;
int tx = x, ty = y, txx = xx, tyy = yy;
x = prev[tx][ty][txx][tyy].x1;
y = prev[tx][ty][txx][tyy].y1;
xx = prev[tx][ty][txx][tyy].x2;
yy = prev[tx][ty][txx][tyy].y2;
print(x, y, xx, yy);
for(int i = 0; i < 4; i++){
if((x + dir[i][0] == tx && y + dir[i][1] == ty) || (xx + dir[i][0] == txx && yy + dir[i][1] == tyy)){
putchar(s[i]);
break;
}
}
return;
}
void bfs(){
queue<Point> Q;
Q.push(start);
vis[start.x][start.y][start.xx][start.yy] = 1;
while(!Q.empty()){
fr = Q.front();
// cout << fr.x << " " << fr.y << " " << fr.xx << " " << fr.yy << endl;
Q.pop();
if(fr.x == fr.xx && fr.y == fr.yy){
// printf("%d %d %d %d\n", fr.x, fr.y, fr.xx, fr.yy);
print(fr.x, fr.y, fr.xx, fr.yy);
puts("");
return;
}
for(int i = 0; i < 4; i++){
int tx = fr.x + dir[i][0], ty = fr.y + dir[i][1];
int txx = fr.xx + dir[i][0], tyy = fr.yy + dir[i][1];
// cout << tx << " " << ty << " " << txx << " " << tyy << " " << i << endl;
if(mp[tx][ty] == '.' || mp[tx][ty] == '*'){
next.x = tx;
next.y = ty;
}
else{
next.x = fr.x;
next.y = fr.y;
}
if(mp[txx][tyy] == '.' || mp[txx][tyy] == '*'){
next.xx = txx;
next.yy = tyy;
}
else{
next.xx = fr.xx;
next.yy = fr.yy;
}
// cout << next.x << " " << next.y << " " << next.xx << " " << next.yy << endl;
if(!vis[next.x][next.y][next.xx][next.yy]){
vis[next.x][next.y][next.xx][next.yy] = 1;
// cout << next.x << " " << next.y << " " << next.xx << " " << next.yy << endl;
Q.push(next);
prev[next.x][next.y][next.xx][next.yy].x1 = fr.x;
prev[next.x][next.y][next.xx][next.yy].y1 = fr.y;
prev[next.x][next.y][next.xx][next.yy].x2 = fr.xx;
prev[next.x][next.y][next.xx][next.yy].y2 = fr.yy;
}
}
}
puts("Sorry");
return;
}
int main(){
while(scanf("%d%d", &n, &m) != EOF){
memset(mp, '\0', sizeof(mp));
int flag = 0;
for(int i = 1; i <= n; i++){
scanf("%s", mp[i] + 1);
for(int j = 1; j <= m; j++){
if(mp[i][j] == '*'){
if(!flag){
start.x = i;
start.y = j;
flag = 1;
}
else{
start.xx = i;
start.yy = j;
break;
}
}
}
}
memset(vis, 0, sizeof(vis));
bfs();
}
return 0;
}
|