试题描述:
已经连续下了几天雨,却还是没有停的样子。土豪CCY刚从外地赚完1e元回来,知道不久除了自己别墅,其他的地方都将会被洪水淹没。
CCY所在的城市可以用一个N*M(N,M<=50)的地图表示,地图上有五种符号:“. * X D S”。其中“X”表示石头,水和人都不能从上面经过。“.”表示平原,CCY和洪水都可以经过。“*”表示洪水开始地方(可能有多个地方开始发生洪水)。“D”表示CCY的别墅。“S”表示CCY现在的位置。
CCY每分钟可以向相邻位置移动,而洪水将会在CCY移动之后把相邻的没有的土地淹没(从已淹没的土地)。
求CCY回到别墅的最少时间。如果聪哥回不了家,就很可能会被淹死,那么他就要膜拜黄金大神涨RP来呼叫直升飞机,所以输出“ORZ hzwer!!!”。
输入:
第一行:N和M
接下来N行,每行M个字符,表示地图。
输出:
若能回家,输出一个整数,表示最少回家步数;否则输出“ORZ hzwer!!!”。
输入示例:
3 3
D.*
...
.S.
输出示例:
3
良心输入示例:
...SXX...XX...X...XX.XX....X.......X....X.X....*X.
.XX.X..XX.X.X..X.X.X...XX.X.....X....X...X.XX....X
..X..X....X.X.X.X.XX....XX...X..XX....X...XXX...XX
X.....X..XXX.XX..X.X.X........XXX....X.X..XX..X...
X..X.....X......X.......X..X......X.....X.X..X....
......XXX..X..XX.X.......X..X......X.X.XXX.X..X...
...X...XX.X..X.........X..XX..XX.X..X.X....X..X..X
XX.........XX.XXX.XXX...X..X..XXX.XX.X..X...X.X.X.
.....X...X..X.X.X.....X.X.X...XX........XX..X..X..
XXXX..XX..X..X..X..XXX..X...X..X.X...X..XX.XX.X...
......X..X....X.........XX..X..X........X.XX...X.X
X..XX...X........X........XXXX.XX.........X.X.XXX.
.....XXX...X.XXX....X...XX........X...XXXX...X....
.X.XX....XXX...X...X...X.X...XXX...X...X..XX.XX...
...XXX....XXX.....X.X...XX...X.X.XXX.X.XX..X......
.......X..XX.X.X..........X.XX..X..X.XX.XXXX...XX.
X...X.X.X.XXX.X..X.XXX..X...X....X.X..X...X..XX..X
..X..X.XX..X.XXX..X...X..X..X..X.X....X..........X
XX....X.X.X.XX.....X..XXXX.XX..X....X.....X.....X.
.XX.....X.X..X..X.X...X......X....X..X.XX.XX.XX.X.
.....X.........X..X.X...X....XX.X...X..X..XX.XX...
.X........X..XX..X...X....XX.XXX.XX.X..X...X...XXX
...X.X.XX...X......XX.X.XXX..X.X.XX....XXX.X...X..
....X.....XX..XX.X.X...X..X.X....X.....XX.XX..XXX.
....X........XXXXXX.........XX..X....XX.XXXX......
..XX.XXX.X...X.X.X.X..XXXX....X.X.X...XX.X..X..X..
XX..XX.X...X....XX....X.......X..X.X......XXXXX...
XX..XX.XX.X..X...XX....XX.X..X...X.X....X.........
..X.....X...X..X.........X......X...XX.XX...X.....
X.X..XX.X.X.X.XXXX.X...X..X...XX..X.....X.X.XXX...
..X....X.X.X......X..X...XX..X...X.X.XXX.X....XX.X
X.....X.X...XXXXX.....X..XX...X....X...XXX....X...
.XXX...XXXX.X....X...XX.XX..X.X........X...X....X.
..XXX.XX....X.XX.X..X...X.X.X.....X...X....X.XXXXX
.XX...X.X....XXX......X.....X....X.X..X..X....XX.X
XX..XX.X....X...XXX....X..XXX.XXX.X.X....X.X......
XX....X.X....X..X.XX...X..X.X....X..XX...XX...X...
X.X...X...X..X....X.X.........X...X...X....X..X...
.X...X...X.X....X.....XX......XXX..XX.XX.X.....X..
..X.X..XX.X..X..........X.X.XX..X..X.X.XXXX.......
..X...XX.X.X..XX..XXXXX...X........XX...X.X..X..XX
.....X..XXX....X.XX....XX.XX..XX.X....XX.XX..X.XXX
X..X.XX.X.X.......X.X.X..X......X..XX....X.XXX.XXX
X..XXXX.........X..X..X............X.X..X..X...X..
X...XX.XX...X........X....X....X.X...X..XD...X..X.
......X.......XXXX..XXX.....X.X.X.X..........X....
X.....XX.XX.XXX.XX.X.X..X.X.......XXXX.....X...XXX
.X..XX..X.....XX...XX..X.....XX...X..X....X..X..XX
X....XX....XX.....XX.X.XXX....X.......X..X....X..X
X.....XX..XX.X.....XX....X..X..X.X.XX.......X..X..
良心输出示例:
解题思路:
这是一道神奇的题目,我用了一天。。。。。
首先,我写了一个DFS+BFS,然后结果错误。
接着,改成预处理,创建一个step数组,step[i][j]表示洪水第几步漫道这里。
如:
3 3
D.*
...
.S.
的step是:
-1 1 0
3 2 1
4 3 2
这需要一个BFS
读入·时每遇到一个*就把它丢到队列里面,
然后正常BFS。
预处理以后,就再正常地BFS
然后就好了。
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <queue>
using namespace std;
int map[][];
int step[][];
int book[][];
int startx, starty;
int endx,endy;
queue <int> Qx;
queue <int> Qy;
int n, m;
int ans = ;
int wnum = ;
void getstep()
{
while(!Qx.empty())
{
int kx = Qx.front();
int ky = Qy.front();
Qx.pop();
Qy.pop();
if(map[kx-][ky]== && step[kx-][ky]>step[kx][ky]+ && kx->)
{
Qx.push(kx-);
Qy.push(ky);
step[kx-][ky]=step[kx][ky]+;
}
if(map[kx+][ky]== && step[kx+][ky]>step[kx][ky]+ && kx+<=n)
{
Qx.push(kx+);
Qy.push(ky);
step[kx+][ky]=step[kx][ky]+;
}
if(map[kx][ky-]== && step[kx][ky-]>step[kx][ky]+ && ky->)
{
Qx.push(kx);
Qy.push(ky-);
step[kx][ky-]=step[kx][ky]+;
}
if(map[kx][ky+]== && step[kx][ky+]>step[kx][ky]+ && ky+<=m)
{
Qx.push(kx);
Qy.push(ky+);
step[kx][ky+]=step[kx][ky]+;
}
}
}
int main()
{
memset(book,,sizeof(book));
memset(step,,sizeof(step));
cin>>n>>m;
for (int i = ; i <= n; i++)
for (int j = ; j <= m; j++)
{
char c;
cin >> c;
if (c == 'S')
{
startx = i;
starty = j;
}
if (c == '*')
{
Qx.push(i);
Qy.push(j);
step[i][j]=;
}
if (c == 'D')
{
endx=i;
endy=j;
map[i][j]=;
}
if (c == 'X')
map[i][j] = -;
}
getstep();
queue <int> qx;
queue <int> qy;
qx.push(startx);
qy.push(starty);
book[startx][starty]=;
while(!qx.empty())
{
int kx=qx.front();
int ky=qy.front();
qx.pop();
qy.pop();
if(map[kx][ky]==)
break;
if((map[kx-][ky]== || map[kx-][ky]==) && book[kx-][ky]>book[kx][ky]+ && step[kx-][ky]>book[kx][ky]+ && kx->=)
{
qx.push(kx-);
qy.push(ky);
book[kx-][ky]=book[kx][ky]+;
}
if((map[kx+][ky]== || map[kx+][ky]==) && book[kx+][ky]>book[kx][ky]+ && step[kx+][ky]>book[kx][ky]+ && kx+<=n)
{
qx.push(kx+);
qy.push(ky);
book[kx+][ky]=book[kx][ky]+;
}
if((map[kx][ky+]== || map[kx][ky+]==) && book[kx][ky+]>book[kx][ky]+ && step[kx][ky+]>book[kx][ky]+ && ky+<=m)
{
qx.push(kx);
qy.push(ky+);
book[kx][ky+]=book[kx][ky]+;
}
if((map[kx][ky-]== || map[kx][ky-]==) && book[kx][ky-]>book[kx][ky]+ && step[kx][ky-]>book[kx][ky]+ && ky->=)
{
qx.push(kx);
qy.push(ky-);
book[kx][ky-]=book[kx][ky]+;
}
}
if(book[endx][endy]>=)
cout<<"ORZ hzwer!!!";
else
cout<<book[endx][endy];
}