学霸的迷宫
时间限制:1.0s 内存限制:256.0MB
问题描述
学霸抢走了大家的作业,班长为了帮同学们找回作业,决定去找学霸决斗。但学霸为了不要别人打扰,住在一个城堡里,城堡外面是一个二维的格子迷宫,要进城堡必须得先通过迷宫。因为班长还有妹子要陪,磨刀不误砍柴功,他为了节约时间,从线人那里搞到了迷宫的地图,准备提前计算最短的路线。可是他现在正向妹子解释这件事情,于是就委托你帮他找一条最短的路线。
输入格式
第一行两个整数n, m,为迷宫的长宽。
接下来n行,每行m个数,数之间没有间隔,为0或1中的一个。0表示这个格子可以通过,1表示不可以。假设你现在已经在迷宫坐标(1,1)的地方,即左上角,迷宫的出口在(n,m)。每次移动时只能向上下左右4个方向移动到另外一个可以通过的格子里,每次移动算一步。数据保证(1,1),(n,m)可以通过。
输出格式
第一行一个数为需要的最少步数K。
第二行K个字符,每个字符∈{U,D,L,R},分别表示上下左右。如果有多条长度相同的最短路径,选择在此表示方法下字典序最小的一个。
样例输入
Input Sample 1:
3 3
001
100
110
Input Sample 2:
3 3
000
000
000
样例输出
Output Sample 1:
4
RDRD
Output Sample 2:
4
DDRR
数据规模和约定
有20%的数据满足:1<=n,m<=10
有50%的数据满足:1<=n,m<=50
有100%的数据满足:1<=n,m<=500。
提交序号 2492113
作者 吴锦诚
提交时间 07-17 18:32:23
评测结果 正确
得分 100
CPU使用 31ms
内存使用 2.175MB
试题名称 学霸的迷宫
语言 C++
#include <iostream>
#include <queue>
#include <set>
#include <string>
#include <algorithm>
using namespace std;
struct Node
{
int x, y, step;
string approach;
};
int n, m, mix[500][500], dx[4] = {-1, 1, 0, 0}, dy[4] = {0, 0, 1, -1};
char ways[4] = {'U', 'D', 'R', 'L'};
string tt[4] = {"上", "下", "右", "左"};
bool qFlag[500][500];
set<Node> res;
bool operator<(const Node &a, const Node &b)
{
return a.approach < b.approach;
}
bool check(Node t)
{
int x = t.x;
int y = t.y;
// cout << " ";
if (x < 0 || x >= n || y < 0 || y >= m)
{
// cout << "越界" << endl;
return false;
}
if (qFlag[x][y])
{
// cout << "已入队过" << endl;
return false;
}
if (mix[x][y] == 1)
{
// cout << "此路不通" << endl;
return false;
}
return true;
}
void bfs()
{
int largeStep = 0;
queue<Node> q;
Node node, temp;
node.x = 0, node.y = 0, node.step = 0;
q.push(node);
while (!q.empty())
{
Node top = q.front();
q.pop();
if (top.x == n - 1 && top.y == m - 1)
{
qFlag[n - 1][m - 1] = false;
if (res.empty())
{
// cout << "产生第一条路径:" << top.approach << endl;
largeStep = top.step;
res.insert(top);
continue;
}
else
{
if (largeStep >= top.step)
{
// cout << "产生新路径:" << top.approach << endl;
res.insert(top);
continue;
}
else
{
// cout << "产生新路径:" << top.approach << "但路径距离过远" << endl;
return;
}
}
}
qFlag[top.x][top.y] = true;
// cout << "---top(" << top.x << "," << top.y << ")" << endl;
for (int i = 0; i < 4; i++)
{
temp.x = top.x + dx[i];
temp.y = top.y + dy[i];
// cout << " " << tt[i] << "方向,(" << temp.x << "," << temp.y << ") ";
if (check(temp))
{
// cout << "入队新位置"<< " ";
temp.step = top.step + 1;
temp.approach = top.approach + ways[i];
// cout << "前路径:" << top.approach << ",此路径为:" << temp.approach << endl;
q.push(temp);
qFlag[temp.x][temp.y] = true;
}
}
// cout << "***top(" << top.x << "," << top.y << ")探索完毕" << endl<< endl;
}
}
int main()
{
string s;
while (cin >> n >> m)
{
cin.ignore();
for (int i = 0; i < n; i++)
{
getline(cin, s);
for (int j = 0; j < m; j++)
{
qFlag[i][j] = false;
mix[i][j] = s[j] - '0';
}
}
res.clear();
bfs();
if (res.empty())
{
cout << -1 << endl;
}
else
{
cout << (*(res.begin())).step << endl;
cout << (*(res.begin())).approach << endl;
}
}
return 0;
}
详细记录
评测点序号 评测结果 得分 CPU使用 内存使用 下载评测数据
1 正确 10.00 0ms 2.101MB 输入 输出
2 正确 10.00 0ms 2.101MB VIP特权
3 正确 10.00 0ms 2.109MB VIP特权
4 正确 10.00 0ms 2.101MB VIP特权
5 正确 10.00 0ms 2.113MB VIP特权
6 正确 10.00 0ms 2.105MB VIP特权
7 正确 10.00 0ms 2.113MB VIP特权
8 正确 10.00 15ms 2.148MB VIP特权
9 正确 10.00 31ms 2.148MB VIP特权
10 正确 10.00 31ms 2.175MB VIP特权