问题描述
学霸抢走了大家的作业,班长为了帮同学们找回作业,决定去找学霸决斗。但学霸为了不要别人打扰,住在一个城堡里,城堡外面是一个二维的格子迷宫,要进城堡必须得先通过迷宫。因为班长还有妹子要陪,磨刀不误砍柴功,他为了节约时间,从线人那里搞到了迷宫的地图,准备提前计算最短的路线。可是他现在正向妹子解释这件事情,于是就委托你帮他找一条最短的路线。
输入格式
第一行两个整数n, m,为迷宫的长宽。
接下来n行,每行m个数,数之间没有间隔,为0或1中的一个。0表示这个格子可以通过,1表示不可以。假设你现在已经在迷宫坐标(1,1)的地方,即左上角,迷宫的出口在(n,m)。每次移动时只能向上下左右4个方向移动到另外一个可以通过的格子里,每次移动算一步。数据保证(1,1),(n,m)可以通过。
接下来n行,每行m个数,数之间没有间隔,为0或1中的一个。0表示这个格子可以通过,1表示不可以。假设你现在已经在迷宫坐标(1,1)的地方,即左上角,迷宫的出口在(n,m)。每次移动时只能向上下左右4个方向移动到另外一个可以通过的格子里,每次移动算一步。数据保证(1,1),(n,m)可以通过。
输出格式
第一行一个数为需要的最少步数K。
第二行K个字符,每个字符∈{U,D,L,R},分别表示上下左右。如果有多条长度相同的最短路径,选择在此表示方法下字典序最小的一个。
第二行K个字符,每个字符∈{U,D,L,R},分别表示上下左右。如果有多条长度相同的最短路径,选择在此表示方法下字典序最小的一个。
样例输入
Input Sample 1:
3 3
001
100
110
Input Sample 2:
3 3
000
000
000
3 3
001
100
110
Input Sample 2:
3 3
000
000
000
样例输出
Output Sample 1:
4
RDRD
Output Sample 2:
4
DDRR
4
RDRD
Output Sample 2:
4
DDRR
数据规模和约定
有20%的数据满足:1<=n,m<=10
有50%的数据满足:1<=n,m<=50
有100%的数据满足:1<=n,m<=500。
有50%的数据满足:1<=n,m<=50
有100%的数据满足:1<=n,m<=500。
bfs求最短路,保持字典序只需要调整广搜的时候的方向优先顺序为D,L,R,U即可,然后打印路径用一个string不断的往下传,传到最后输出返回
#include <cstdio>
#include <cstring>
#include <queue>
#include <stack>
#include <algorithm>
#include <iostream>
using namespace std;
struct p {
int x, y, cou; //cou用来记录走到当前位置走的步数
string str; //str用来记录路径
p() {}
p(int x, int y, string str, int cou) : x(x), y(y), str(str), cou(cou) {}
};
char mapp[510][510];
bool vis[510][510];
queue<p> Q;
int n, m;
//dir按照D,L,R,U的顺序
int dir[4][2] = { {1, 0}, {0, -1}, {0, 1}, {-1, 0} };
bool check(int x, int y) {
if (x < 0 || x >= n || y < 0 || y >= m || vis[x][y])
return false;
return true;
}
void bfs() {
p tp = p(0, 0, "", 0);
while (!Q.empty()) Q.pop(); //初始化队列
Q.push(tp);
vis[0][0] = true;
while (!Q.empty()) {
p np = Q.front(); Q.pop();
for (int i = 0; i < 4; i++) {
int cx = dir[i][0], cy = dir[i][1];
int nx = np.x + cx, ny = np.y + cy;
string ts = "";
if (check(nx, ny)) {
switch(cx) {
case 1: ts = "D"; break;
case -1: ts = "U"; break;
default: break;
}
switch(cy) {
case 1: ts = "R"; break;
case -1: ts = "L"; break;
default: break;
}
if (nx == n - 1 && ny == m - 1) {
cout << np.cou + 1 << endl;
cout << np.str + ts << endl;
return ;
}
Q.push(p(nx, ny, np.str + ts, np.cou + 1));
vis[nx][ny] = true;
}
}
}
}
int main()
{
while (~scanf("%d%d", &n, &m)) {
memset(vis, false, sizeof(vis));
for (int i = 0; i < n; i++) {
scanf("%s", mapp[i]);
for (int j = 0; j < m; j++) {
if (mapp[i][j] == '1') vis[i][j] = true;
}
}
bfs();
}
return 0;
}
另外一种记录输出路径的方法:
用一个二维数组记录当前位置前一个点的位置信息,和转向信息,最后用一个栈把结果处理下输出
#include <cstdio>
#include <cstring>
#include <queue>
#include <stack>
#include <algorithm>
#include <iostream>
using namespace std;
struct p {
int x, y, cou; //cou用来记录走到当前位置走的步数
p() {}
p(int x, int y, int cou) : x(x), y(y), cou(cou) {}
};
//node结构体用来保存当前点的前驱位置信息和转向
struct node {
int x, y;
char d;
node() {}
node (int x, int y, char d) : x(x), y(y), d(d) {}
}pre[510][510];
//用于最后输出路径
stack<char> s;
char mapp[510][510];
bool vis[510][510];
queue<p> Q;
int n, m;
//dir按照D,L,R,U的顺序
int dir[4][2] = { {1, 0}, {0, -1}, {0, 1}, {-1, 0} };
bool check(int x, int y) {
if (x < 0 || x >= n || y < 0 || y >= m || vis[x][y])
return false;
return true;
}
void bfs() {
p tp = p(0, 0, 0);
while (!Q.empty()) Q.pop(); //初始化队列
while (!s.empty()) s.pop();
Q.push(tp);
vis[0][0] = true;
while (!Q.empty()) {
p np = Q.front(); Q.pop();
for (int i = 0; i < 4; i++) {
int cx = dir[i][0], cy = dir[i][1];
int nx = np.x + cx, ny = np.y + cy;
char ts;
if (check(nx, ny)) {
switch(cx) {
case 1: ts = 'D'; break;
case -1: ts = 'U'; break;
default: break;
}
switch(cy) {
case 1: ts = 'R'; break;
case -1: ts = 'L'; break;
default: break;
}
if (nx == n - 1 && ny == m - 1) {
pre[nx][ny] = node(np.x, np.y, ts);
cout << np.cou + 1 << endl;
for (int tx, ty; nx != 0 || ny != 0; nx = pre[tx][ty].x, ny = pre[tx][ty].y) {
tx = nx, ty = ny;
s.push(pre[nx][ny].d);
}
while (!s.empty()) {
printf("%c", s.top());
s.pop();
}
puts("");
return ;
}
Q.push(p(nx, ny, np.cou + 1));
pre[nx][ny] = node(np.x, np.y, ts);
vis[nx][ny] = true;
}
}
}
}
int main()
{
while (~scanf("%d%d", &n, &m)) {
memset(vis, false, sizeof(vis));
for (int i = 0; i < n; i++) {
scanf("%s", mapp[i]);
for (int j = 0; j < m; j++) {
if (mapp[i][j] == '1') vis[i][j] = true;
}
}
bfs();
}
return 0;
}