题目描述
这是一个关于二维迷宫的题目。我们要从迷宫的起点 'S' 走到终点 'E',每一步我们只能选择上下左右四个方向中的一个前进一格。 'W' 代表墙壁,是不能进入的位置,除了墙壁以外的地方都可以走。迷宫内的 'D' 代表一道上锁的门,只有在持有钥匙的时候才能进入。而 'K' 则代表了钥匙,只要进入这一格,就会自动地拿到钥匙。最后 '.' 则是代表空无一物的地方,欢迎自在的游荡。
本题的迷宫中,起点、终点、门跟钥匙这四个特殊物件,每一个恰好会出现一次。而且,此迷宫的四周 (最上面的一行、最下面的一行、最左边的一列以及最右边的一列) 都会是墙壁。
请问,从起点到终点,最少要走几步呢?
输入描述:
输入的第一行有两个正整数H, W,分别代表迷宫的长跟宽。
接下来的H行代表迷宫,每行有一个长度恰为W的字串,此字串只包含`'S'`, `'E'`, `'W'`, `'D '`, `'K'`, `'.'`这几种字元。
输出描述:
请在一行中输出一个整数代表答案,如果无法从起点走到终点,请输出-1。
示例1
输入
4 12
WWWWWWWWWWWW
WE.W.S..W.KW
W..D..W....W
WWWWWWWWWWWW
输出
20
示例2
输入
6 6
WWWWWW
WEWS.W
W.WK.W
W.WD.W
W.W..W
WWWWWW
输出
-1
备注:
4 ≤ H, W≤ 500
'S', 'E', 'K', 'D'各出现恰好一次
迷宫的四周(最上面的一行、最下面的一行、最左边的一列以及最右边的一列) 都会是 'W'
这一题其实也是相当简单的,只要广度优先遍历S到钥匙的距离,钥匙到门的距离,门到终点的距离,和钥匙到终点的距离,然后比较起点钥匙加上钥匙到门加上门到终点的距离和起点到终点的距离哪个小就能够得到最终的结果,这里需要注意的是以上所说的距离中任意一条都有可能不存在,当两条都不存在的时候,输出-1!
这一题我把一个i写成了j,然后找bug找了一个小时,时间都浪费了,把心态也搞蹦了,主要还是太紧张了,以后做题的时候首先是不要紧张,然后一定要注意,字母不要写错!
这一题我为了缩短程序的运行时间加了很多的条件判断,这样是没有必要的,因为运行时间缩短的分数并不会上升,尽快的ac题目才是王道!
在c++里面调试的时候要学会使用f10!能够查看执行的下一步在当前程序的哪一行代码!
#include <iostream>
#include <vector>
#include <queue>
#include<string>
struct point
{
int x, y;
point(int x1, int y1) : x(x1), y(y1) {}
void setP(int x1, int y1) { x = x1; y = y1; }
};
using namespace std;
point theYaoShi(0, 0);
point theMen(0, 0);
point theS(0, 0);
point theE(0, 0);
int H, W;
void BFS(point start, int theStartKind, vector<vector<int>> & theResult, vector<string> &theWalls)
{
int theX[4] = { -1,1,0,0 };
int theY[4] = { 0,0,-1,1 };
int tmpX, tmpY;
queue<point> theS;
point tmp(0, 0);
theS.push(start);
theResult[start.x][start.y] = 0;
while (!theS.empty())
{
tmp = theS.front(); theS.pop();
if (tmp.x == 1 && tmp.y == 10)
{
int j_make = 1;
}
for (int i = 0; i < 4; i++)
{
tmpX = tmp.x + theX[i];
tmpY = tmp.y + theY[i];
if (theWalls[tmpX][tmpY] != 'W'&&theResult[tmpX][tmpY] == -1)
{
theResult[tmpX][tmpY] = theResult[tmp.x][tmp.y] + 1;
if (theWalls[tmpX][tmpY] != 'D')
theS.push(point(tmpX, tmpY));
}
}
if (theStartKind == 0)
{
if (theResult[theE.x][theE.y] != -1 && theResult[theYaoShi.x][theYaoShi.y] != -1)
return;
}
else if (theStartKind == 1)
{
if (theResult[theMen.x][theMen.y] != -1) return;
}
else
{
if (theResult[theE.x][theE.y] != -1) return;
}
}
}
void clearthe(vector<vector<int>> & theResult)
{
int w = theResult.size();
int h = theResult[0].size();
for (int i = 0; i<w; i++)
for (int j = 0; j < h; j++)
{
theResult[i][j] = -1;
}
}
int main()
{
cin >> H >> W;
vector<string> theWalls(H, string(W, 'W'));
vector<vector<int>> theResult(H, vector<int>(W, -1));
string theNum;
for (int i = 0; i < H; i++)
{
cin >> theNum;
for (int j = 0; j < theNum.size(); j++)
{
if (theNum[j] == 'S') theS.setP(i, j);
if (theNum[j] == 'E') theE.setP(i, j);
if (theNum[j] == 'D') theMen.setP(i, j);
if (theNum[j] == 'K') theYaoShi.setP(i, j);
theWalls[i][j] = theNum[j];
}
}
BFS(theS, 0, theResult, theWalls);
int theSToK = theResult[theYaoShi.x][theYaoShi.y];
int theSToE = theResult[theE.x][theE.y];
if (theSToE == -1 && theSToK == -1) { cout << -1; return 0; }
if (theSToE != -1 && theSToK == -1) { cout << theSToE; return 0; }
clearthe(theResult);
BFS(theYaoShi, 1, theResult, theWalls);
int theKtoM = theResult[theMen.x][theMen.y];
if (theSToE == -1 && theKtoM == -1) { cout << -1; return 0; }
if (theKtoM == -1 && theSToE != -1) { cout << theSToE; return 0; }
clearthe(theResult);
BFS(theMen, 2, theResult, theWalls);
int theMtoE = theResult[theE.x][theE.y];
if (theSToE == -1 && theMtoE != -1)
{
cout << theSToK + theKtoM + theMtoE; return 0;
}
if (theMtoE == -1 && theSToE != -1) { cout << theSToE; return 0; }
if (theMtoE != -1 && theSToE != -1)
{
cout << min(theSToE, theSToK + theKtoM + theMtoE);
return 0;
}
else
{
cout << -1;
return 0;
}
return 0;
}