链接: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. [RK3288][Android6.0] 调试笔记 --- 移除uboot和kernel开机logo【转】

    本文转载自:http://blog.csdn.net/kris_fei/article/details/71600690 Platform: RockchipOS: Android 6.0Kernel ...

  2. dhcpcd守护进程分析【转】

    本文转载自;http://blog.csdn.net/lishanmin11/article/details/37930073 最近在调android ethernet功能,android本身不带 e ...

  3. 6个基本screen命令

    screen -S  <name>    直接建立并进入<name>窗口 control+a    d    暂时退出窗口 (在会话内) screen -r <name& ...

  4. C++打印变量地址

    %p专门用来打印变量的以十六进制表示的地址: #include<iostream> using namespace std; int main() { ; printf("a的地 ...

  5. 恶心的struts标签,等我毕业设计弄完了,瞧我怎么收拾你。

    1.从java action中到页面中获取变量值的struts标签 获取从bean中定义的对象中属性的值: <s:property value="#request.cardTo.acc ...

  6. Object.prototype.constructor

    Returns a reference to the Object function that created the instance's prototype. 注意这个属性的值是函数本省的引用,而 ...

  7. Linux-正则表达式与三剑客

    1 固化命令文件 登录时执行文件的顺序 /etc/profile /etc/profile.d ~/.bash_profile ~/.bashrc /etc/bashrc 非登录shell ~/.ba ...

  8. 更换ubuntu apt-get源

    原文地址:http://www.cnblogs.com/zhangpengshou/p/3591387.html 为了优化ubuntu软件安装/更新速度,我测试了国内几家apt源的速度,发现北京交大的 ...

  9. HDMI 8193 配置

    1, User space:ProjectConfig.mkMTK_HDMI_SUPPORT = yes MTK_MULTIBRIDGE_SUPPORT = yesMTK_INTERNAL_HDMI_ ...

  10. HTML head元素

    head标签中可以包含的标签元素有: <title>:定义html页面的标题 <meta>: <meta> 标签提供了元数据.元数据也不显示在页面上,但会被浏览器解 ...