NOJ [1320] Hole of Black

链接地址:http://ac.nbutoj.com/Problem/view.xhtml?id=1320

此题就是一个最短路径BFS求解问题。
但是由于黑洞的干扰,当Wodex每走一步,黑洞就会向空格区域蔓延一步。
那么我们可以定义一个tmp[][]数组来存储黑洞的情况,对黑洞蔓延BFS一下。上面的值存储的意思表示黑洞在Wodex走那么多步时蔓延到的格子。
标记完黑洞的情况以后,我们再对Wodex走迷宫BFS一下,当Wodex此步的步数与tmp[][]比较大小,步数比tmp[][]小的时候才可以走。

出题代码:

#include <set>
#include <map>
#include <list>
#include <stack>
#include <queue>
#include <cmath>
#include <cstdio>
#include <vector>
#include <iomanip>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;

#define N 110

int maze[N][N];
int tmp[N][N];
int dir[4][2] = {{-1, 0}, {0, -1}, {0, 1}, {1, 0}};

int m;

int main()
{
//freopen("data.in", "r", stdin);
//freopen("data.out", "w", stdout);
while (scanf("%d", &m) && m)
{
getchar();
memset(tmp, -1, sizeof(tmp));
for (int i = 0; i < m; i++)
{
for (int j = 0; j < m; j++)
{
if (getchar() == 'O') maze[i][j] = 0;
else maze[i][j] = -1;
}
getchar();
}
int a, b;
scanf("%d%d", &a, &b);
tmp[a][b] = 0;
queue<int> q1, q2;
while (!q1.empty()) q1.pop();
while (!q2.empty()) q2.pop();
q1.push(a);
q2.push(b);
while (!q1.empty())
{
int x1 = q1.front();
int y1 = q2.front();
q1.pop();
q2.pop();
for (int i = 0; i < 4; i++)
{
int xx = x1 + dir[i][0];
int yy = y1 + dir[i][1];
if (xx >= 0 && yy >= 0 && xx < m && yy < m)
{
if (maze[xx][yy] == 0 && tmp[xx][yy] == -1)
{
tmp[xx][yy] = tmp[x1][y1] + 1;
q1.push(xx);
q2.push(yy);
}
}
}
}
q1.push(0);
q2.push(0);
maze[0][0] = 1;
while (!q1.empty())
{
int x1 = q1.front();
int y1 = q2.front();
q1.pop();
q2.pop();
for (int i = 0; i < 4; i++)
{
int xx = x1 + dir[i][0];
int yy = y1 + dir[i][1];
if (xx >= 0 && yy >= 0 && xx < m && yy < m)
{
if (maze[xx][yy] == 0 && (maze[x1][y1] < tmp[xx][yy] || tmp[xx][yy] == -1))
{
maze[xx][yy] = maze[x1][y1] + 1;
q1.push(xx);
q2.push(yy);
}
}
}
}
if (maze[m - 1][m - 1]) printf("%d\n", maze[m - 1][m - 1] - 1);
else printf("Wodex lost the game!\n");
}

return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值