Codeforces Round #516 (Div. 2)D. Labyrinth(BFS)
题目链接:http://codeforces.com/contest/1064/problem/D
题目大意:给你一个n*m的图,图中包含两种符号,'.'表示可以行走,'*'表示障碍物不能行走,规定最多只能向左走L个格子,向右R个格子,但是上下没有限制,现在给出出发点坐标(sx,sy),求能走的最大单元格数目。
Examples
4 5
3 2
1 2
.....
.***.
...**
*....
10
4 4
2 2
0 1
....
..*.
....
....
7
解题思路:直接用BFS模拟行走过程,不过需要多记录下向左和向右行走的步数。为了保证保留向左向右行走的次数,我们优先向上下行走,能向上下行走就先不往左右行走。直接用双向队列,如果是向上下行走就放在队首,如果是左右行走放在队尾。
附上代码:
#include<bits/stdc++.h>
using namespace std;
int n,m,vis[][],ans,sx,sy,L,R;
int dir[][]={{,},{-,},{,-},{,}};
char mp[][];
struct node{
int x,y,l,r;
};
bool check(node x)
{
if(x.x<=||x.y<=||x.x>n||x.y>m||vis[x.x][x.y]||mp[x.x][x.y]=='*')
return false;
return true;
}
void BFS()
{
deque<node> que;
vis[sx][sy]=;
ans++;
node s;
s.x=sx; s.y=sy; s.l=L; s.r=R;
que.push_back(s);
while(!que.empty())
{
node now=que.front();
que.pop_front();
node next;
for(int i=;i<;i++)
{
next.x=now.x+dir[i][];
next.y=now.y+dir[i][];
if(check(next))
{
if(i<)
{
next.l=now.l; next.r=now.r;
que.push_front(next);
vis[next.x][next.y]=;
ans++;
}
if(i==&&now.l>=)
{
next.l=now.l-;
next.r=now.r;
que.push_back(next);
vis[next.x][next.y]=;
ans++;
}
if(i==&&now.r>=)
{
next.l=now.l;
next.r=now.r-;
que.push_back(next);
vis[next.x][next.y]=;
ans++;
}
}
}
}
}
int main()
{
scanf("%d%d%d%d%d%d",&n,&m,&sx,&sy,&L,&R);
for(int i=;i<=n;i++)
for(int j=;j<=m;j++)
cin>>mp[i][j];
BFS();
cout<<ans<<endl;
return ;
}
Codeforces Round #516 (Div. 2)D. Labyrinth(BFS)的更多相关文章
- Codeforces Round #516 (Div. 2)D. Labyrinth
D. Labyrinth 题目链接:https://codeforces.com/contest/1064/problem/D 题意: 给出一个n*m的矩阵以及人物的起点,并且给出x,y,分别代表这个 ...
- Codeforces Round #433 (Div. 2)【A、B、C、D题】
题目链接:Codeforces Round #433 (Div. 2) codeforces 854 A. Fraction[水] 题意:已知分子与分母的和,求分子小于分母的 最大的最简分数. #in ...
- Codeforces Round #599 (Div. 2) D. 0-1 MST(bfs+set)
Codeforces Round #599 (Div. 2) D. 0-1 MST Description Ujan has a lot of useless stuff in his drawers ...
- Codeforces Round #516 (Div. 2) (A~E)
目录 Codeforces 1064 A.Make a triangle! B.Equations of Mathematical Magic C.Oh Those Palindromes D.Lab ...
- Codeforces Round #516 (Div. 2, by Moscow Team Olympiad) D. Labyrinth(重识搜索)
https://codeforces.com/contest/1064/problem/D 题意 给你一个有障碍的图,限制你向左向右走的次数,问你可以到达格子的个数 思路 可以定义状态为vi[x][y ...
- Codeforces Round #516 (Div. 2, by Moscow Team Olympiad) D. Labyrinth
http://codeforces.com/contest/1064/problem/D 向上/向下加0,向左/右加1, step = 0,1,…… 求的是最少的步数,所以使用bfs. step=k ...
- Codeforces Round #292 (Div. 1)A. Drazil and Factorial 构造
A. Drazil and Factorial 题目连接: http://codeforces.com/contest/516/problem/A Description Drazil is play ...
- Codeforces Round #516(Div 2)
比赛链接:传送门 A. Make a triangle!(简单思维) 题目大意: 给你三条边,问你最多加多少长度能使这三条边能构成三角形. 思路: 最大边小于答案加另外两条边的和. #include ...
- B. Ohana Cleans Up(Codeforces Round #309 (Div. 2))
B. Ohana Cleans Up Ohana Matsumae is trying to clean a room, which is divided up into an n by n gr ...
随机推荐
- 面象对象设计原则之七:合成复用原则(Composition/Aggregate Reuse Principle, CARP)
合成复用原则又称为组合/聚合复用原则(Composition/Aggregate Reuse Principle, CARP),其定义如下: 合成复用原则(Composite Reuse Princi ...
- [转帖]SAP一句话入门:Project System
SAP一句话入门:Project System http://blog.vsharing.com/MilesForce/A621279.html 这是SAP ERP入门的最后一篇了. 我们这些死跑龙套 ...
- C++常用宏
宏是由 #define 定义而来,在预处理阶段进行宏展开,它的格式是: #define N 2 + 2 // 仅仅是字符串替换 #define N (2 + 2) // 也是字符串 ,但是是(2 + ...
- php 删除一维数组中某一个值元素的操作方法
1. 自己写for循环 从array里去掉$tmp这个元素的值 ? 1 2 3 4 5 6 7 8 9 10 <?php $tmp = '324'; $arr = array( '0' => ...
- 在linux上安装Scala详细步骤
scala在linux安装很简单,就是下载,解压,配置环境变量,source一下成功. 提君博客原创 >>提君博客原创 http://www.cnblogs.com/tijun/ < ...
- Day 6-2简单的socket通信
什么是socket? Socket是应用层与TCP/IP协议族通信的中间软件抽象层,它是一组接口.在设计模式中,Socket其实就是一个门面模式,它把复杂的TCP/IP协议族隐藏在Socket接口后面 ...
- word的"bug"
发表博客发现,从word复制文本到chrome浏览器上的博客时, 如果复制完后立即关闭word,那么将无法粘贴到通过chrome浏览器访问的博客上,也无法粘贴到记事本上: 但是复制完立即关闭word后 ...
- SpringBoot 4.SpringBoot 整合 devtools 实现热部署
一.添加 devtools 依赖 <!-- Spring boot 热部署 : 此热部署会遇到 java.lang.ClassCastException 异常 --> <!-- op ...
- Hbase存储模式
以key.value的结构存储数据; (table,rowkey,family,colum,timestamp)构成数据的key,value存储数据
- linux之硬盘管理fdisk
1.首先我们用虚拟机模拟服务器加入一块新的硬盘,硬盘容量入下图5GB硬盘. 2.首先我们要对它进行分区,我们从上面知道需要分区格式化的是第二块硬盘. 3.输入n回车是新建分区,p是主分区(只能建立4个 ...