链接: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. (linux)struct inode 和 struct file

    转自:http://www.cnblogs.com/QJohnson/archive/2011/06/24/2089414.html 1.struct inode──字符设备驱动相关的重要结构介绍 内 ...

  2. poj2586 Y2K Accounting Bug —— 枚举

    链接:http://poj.org/problem?id=2586 题意:大意是一个公司在12个月中,或固定盈余s,或固定亏损d.但记不得哪些月盈余,哪些月亏损,只能记得连续5个月的代数和总是亏损(和 ...

  3. mediaxyz访谈录:ffmpeg的码率控制

    mediaxyz是一位研究ffmpeg有三年的高人了,这几天一直在折腾ffmpeg中的x264,就是不知道该如何控制码率,主要是参数太多,也不知道该如何设置,在 google上search了一下,这方 ...

  4. css控制字符长度超出变成点点点显示(单行,多行)

    css控制字符长度超出变成点点点显示 单行: width 必不可少 div{ width: 55%; text-overflow: ellipsis; overflow: hidden; white- ...

  5. H3C-L2TP

    l2tp enable #启用l2tp domain system authentication ppp local # 本地认证 access-limit disable state active ...

  6. 阻止Eclipse一直building workspace

    Eclipse 一直不停 building workspace完美解决总结 一.产生这个问题的原因多种 1.自动升级 2.未正确关闭 3.maven下载lib挂起 等.. 二.解决总结 (1).解决方 ...

  7. 在浏览器上直接输入 http://www.bookEstore.com就可以访问工程问题

    关于在浏览器上直接输入 http://www.bookEstore.com就可以访问工程问题 1.在tomcat/conf/server.xml文件中配置一个虚拟主机 <Host name=&q ...

  8. POJ3624(01背包:滚动 实现)

    Charm Bracelet Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 30417   Accepted: 13576 ...

  9. Windows下安装zip包解压版mysql

    Windows下安装zip包解压版mysql 虽然官方提供了非常好的安装文件,但是有的时候不想每次再重装系统之后都要安装一遍MySQL,需要使用zip包版本的MySQL.在安装时需如下三步: 1. 新 ...

  10. office2016出现 此功能看似已中断 并需要修复