链接:https://ac.nowcoder.com/acm/contest/332/J

题意:

你在一个 n 行 m 列的网格迷宫中,迷宫的每一格要么为空,要么有一个障碍。
你当前在第 r 行第 c 列(保证该格子为空)。每次移动你可以向上下左右任意一个方向移动一格,前提是不能走到障碍上,也不能超出迷宫的边界。
你向左移动的次数不能超过 x 次,向右不能超过 y 次。
问在这种情况下,对于每个格子,是否存在一种移动方案让你走到它。
输出有多少个格子存在移动方案让你走到它。

思路:

BFS 题目数据有点弱,普通bfs都过了。

看博客找到一份hack代码。

改了一下,感觉还是有点问题。

Vis数组记录到某个点的左右剩余次数,当新过去的点剩余次数大于旧的,就更新一下,解决因为bfs顺序引起的问题。

代码:

#include <bits/stdc++.h>
using namespace std; typedef long long LL; const int MAXN = 1e3 + 10;
int Next[4][2] = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}}; struct Node
{
int _x;
int _y;
int _l;
int _r;
Node(int x, int y, int l, int r):_x(x), _y(y), _l(l), _r(r){}
}; char Map[MAXN][MAXN];
int Vis[MAXN][MAXN][2];
int n, m, sx, sy;
int l, r; int main()
{
cin >> n >> m;
cin >> sx >> sy;
cin >> l >> r;
for (int i = 1;i <= n;i++)
{
for (int j = 1;j <= m;j++)
cin >> Map[i][j];
}
queue<Node> que;
que.emplace(sx,sy,l,r);
Map[sx][sy] = '+';
Vis[sx][sy][0] = l;
Vis[sx][sy][1] = r;
while (!que.empty())
{
Node now = que.front();
for (int i = 0;i < 4;i++)
{
int tx = now._x + Next[i][0];
int ty = now._y + Next[i][1];
if (tx < 1 || tx > n || ty < 1 || ty > m)
continue;
if (Map[tx][ty] == '*')
continue;
if (i == 1)
{
if (now._r == 0)
continue;
Map[tx][ty] = '+';
if (Vis[now._x][now._y][0] > Vis[tx][ty][0] || Vis[now._x][now._y][1] > Vis[tx][ty][1])
{
que.emplace(tx, ty, now._l, now._r - 1);
Vis[tx][ty][0] = Vis[now._x][now._y][0];
Vis[tx][ty][1] = Vis[now._x][now._y][1] - 1;
}
}
else if (i == 3)
{
if (now._l == 0)
continue;
Map[tx][ty] = '+';
if (Vis[now._x][now._y][0] > Vis[tx][ty][0] || Vis[now._x][now._y][1] > Vis[tx][ty][1])
{
que.emplace(tx, ty, now._l - 1, now._r);
Vis[tx][ty][0] = Vis[now._x][now._y][0] - 1;
Vis[tx][ty][1] = Vis[now._x][now._y][1];
}
}
else
{
Map[tx][ty] = '+';
if (Vis[now._x][now._y][0] > Vis[tx][ty][0] || Vis[now._x][now._y][1] > Vis[tx][ty][1])
{
que.emplace(tx, ty, now._l, now._r);
Vis[tx][ty][0] = Vis[now._x][now._y][0];
Vis[tx][ty][1] = Vis[now._x][now._y][1];
}
}
}
que.pop();
}
int res = 0;
for (int i = 1;i <= n;i++)
for (int j = 1;j <= m;j++)
if (Map[i][j] == '+')
res++;
/*
for (int i = 1;i <= n;i++)
{
for (int j = 1;j <= m;j++)
cout << Map[i][j];
cout << endl;
}
*/
cout << res << endl; return 0;
}

  

牛客寒假6-J.迷宫的更多相关文章

  1. 2020牛客寒假算法基础集训营2 J题可以回顾回顾

    2020牛客寒假算法基础集训营2 A.做游戏 这是个签到题. #include <cstdio> #include <cstdlib> #include <cstring ...

  2. 2020牛客寒假算法基础集训营1 J题可以回顾回顾

    2020牛客寒假算法基础集训营1 这套题整体来说还是很简单的. A.honoka和格点三角形 这个题目不是很难,不过要考虑周全,面积是1,那么底边的长度可以是1也可以是2, 注意底边1和2会有重复的, ...

  3. 牛客寒假算法基础集训营4 C Applese 走迷宫

    链接:https://ac.nowcoder.com/acm/contest/330/C来源:牛客网 精通程序设计的 Applese 双写了一个游戏. 在这个游戏中,它被困在了一个 n×m迷宫 在迷宫 ...

  4. 牛客寒假算法基础集训营5 J 炫酷数学

    链接:https://ac.nowcoder.com/acm/contest/331/J来源:牛客网 小希最近想知道一个东西,就是A+B=A|B(其中|为按位或)的二元组有多少个. 当然,直接做这个式 ...

  5. 牛客寒假算法基础集训营4 I题 Applese 的回文串

    链接:https://ac.nowcoder.com/acm/contest/330/I 来源:牛客网 自从 Applese 学会了字符串之后,精通各种字符串算法,比如--判断一个字符串是不是回文串. ...

  6. 牛客寒假算法基础集训营4 I Applese 的回文串

    链接:https://ac.nowcoder.com/acm/contest/330/I来源:牛客网 自从 Applese 学会了字符串之后,精通各种字符串算法,比如……判断一个字符串是不是回文串. ...

  7. 牛客寒假算法基础集训营2 【处女座与复读机】DP最小编辑距离【模板题】

    链接:https://ac.nowcoder.com/acm/contest/327/G来源:牛客网 一天,处女座在牛客算法群里发了一句“我好强啊”,引起无数的复读,可是处女座发现复读之后变成了“处女 ...

  8. 牛客寒假6-F十字阵列

    链接:https://ac.nowcoder.com/acm/problem/201986来源:牛客网 题目描述 小 Q 新学会了一种魔法,可以对一个 N行M列 的网格上的敌人造成伤害 第 i 次使用 ...

  9. 牛客寒假6-I 导航系统

    链接:https://ac.nowcoder.com/acm/contest/3007/I来源:牛客网 题目描述 小 Q 所在的国家有 N 个城市,城市间由 N-1 条双向道路连接,任意一对城市都是互 ...

  10. 牛客寒假6-C汉诺塔

    链接:https://ac.nowcoder.com/acm/contest/3007/C来源:牛客网 题目描述 现在你有 N 块矩形木板,第 i 块木板的尺寸是 Xi*Yi,你想用这些木板来玩汉诺塔 ...

随机推荐

  1. Ext rowcontextmenu回调

    今天栽了大跟头,,,, EXT js右键可以获取到选中的那一列的索引值,只需在回调函数中取到那一列的数据就可以ok,不用再费尽心思的费用执行click的事件 grid.on("rowcont ...

  2. UVA11752 The Super Powers —— 数论、枚举技巧

    题目链接:https://vjudge.net/problem/UVA-11752 题意: 一个超级数是能够至少能表示为两个数的幂,求1~2^64-1内的超级数. 题解: 1.可知对于 n = a^b ...

  3. 如何强制ffmpeg编码时输出一个关键帧

    http://blog.csdn.net/ashlingr/article/details/7829429 如何强制ffmpeg编码时输出一个关键帧   如何强制ffmpeg编码时输出一个关键帧 AV ...

  4. CNN卷积神经网络_深度残差网络 ResNet——解决神经网络过深反而引起误差增加的根本问题,Highway NetWork 则允许保留一定比例的原始输入 x。(这种思想在inception模型也有,例如卷积是concat并行,而不是串行)这样前面一层的信息,有一定比例可以不经过矩阵乘法和非线性变换,直接传输到下一层,仿佛一条信息高速公路,因此得名Highway Network

    from:https://blog.csdn.net/diamonjoy_zone/article/details/70904212 环境:Win8.1 TensorFlow1.0.1 软件:Anac ...

  5. [调试AvantCourier的笔记]

    1.manifest里不能设置target sdk 不然会出现stale error. 2.manifest里要有Internet权限 3

  6. Relative atomic mass

    Relative atomic mass Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Oth ...

  7. js 常见的小数取整问题

    1.四舍五入取整 Math.round(5/2)   // 3 2.直接去掉小数,取整 parseInt(5/2);     //  2 3.向上取整,有小数整数部分就加1 Math.ceil(1/3 ...

  8. mongodb replica set 配置高性能多服务器详解

    mongodb的多服务器配置,以前写过一篇文章,是master-slave模式的,请参考:详解mongodb 主从配置.master-slave模式,不能自动实现故障转移和恢复.所以推荐大家使用mon ...

  9. 2019腾讯广告算法大赛 Rank23

    由于官方审核代码,代码将在2019年6月28号后开源 写在前面 这次腾讯的第三届广告算法大赛,是我第一次参加,取得了初赛与复赛均为23名的成绩,毕竟我只是初打比赛不久的小白.我想在此分享下我的基本解题 ...

  10. sublimelinter-php 错误代码提示

    先安装 SublimeLinter 如同其他插件一样使用 Package Control 来安装. 按下 Ctrl+Shift+p 进入 Command Palette 输入install进入 Pac ...