题目链接:http://codeforces.com/contest/1064/problem/D

题目大意:给你一个n*m的图,图中包含两种符号,'.'表示可以行走,'*'表示障碍物不能行走,规定最多只能向左走L个格子,向右R个格子,但是上下没有限制,现在给出出发点坐标(sx,sy),求能走的最大单元格数目。

Examples

Input

Copy
4 5
3 2
1 2
.....
.***.
...**
*....
Output

Copy
10
Input

Copy
4 4
2 2
0 1
....
..*.
....
....
Output

Copy
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)的更多相关文章

  1. Codeforces Round #516 (Div. 2)D. Labyrinth

    D. Labyrinth 题目链接:https://codeforces.com/contest/1064/problem/D 题意: 给出一个n*m的矩阵以及人物的起点,并且给出x,y,分别代表这个 ...

  2. Codeforces Round #433 (Div. 2)【A、B、C、D题】

    题目链接:Codeforces Round #433 (Div. 2) codeforces 854 A. Fraction[水] 题意:已知分子与分母的和,求分子小于分母的 最大的最简分数. #in ...

  3. 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 ...

  4. Codeforces Round #516 (Div. 2) (A~E)

    目录 Codeforces 1064 A.Make a triangle! B.Equations of Mathematical Magic C.Oh Those Palindromes D.Lab ...

  5. Codeforces Round #516 (Div. 2, by Moscow Team Olympiad) D. Labyrinth(重识搜索)

    https://codeforces.com/contest/1064/problem/D 题意 给你一个有障碍的图,限制你向左向右走的次数,问你可以到达格子的个数 思路 可以定义状态为vi[x][y ...

  6. 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 ...

  7. Codeforces Round #292 (Div. 1)A. Drazil and Factorial 构造

    A. Drazil and Factorial 题目连接: http://codeforces.com/contest/516/problem/A Description Drazil is play ...

  8. Codeforces Round #516(Div 2)

    比赛链接:传送门 A. Make a triangle!(简单思维) 题目大意: 给你三条边,问你最多加多少长度能使这三条边能构成三角形. 思路: 最大边小于答案加另外两条边的和. #include ...

  9. 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 ...

随机推荐

  1. 3 Asking for more information

    1 Could you tell me more about your idea? 2 Could I hear more about your idea? 3 Could you expain yo ...

  2. 过滤数组中的空数组array_filter()

    手册查询array_filter()的使用细节时,看到了:If no callback is supplied, all entries of input equal to FALSE (see co ...

  3. Django Rest framework 框架之解析器

    解析器 序列化***** 请求数据进行校验 对queryset进行序列化处理 分页 路由 视图 渲染器

  4. Git使用:Linux(Ubuntu 14.04 x64)下安装Git并配置连接GitHub

    github是一个非常好的网络代码托管仓库,知晓许久,但是一直没有用起来,最近才开始使用git管理自己的文档和代码. Git是非常强大的版本管理工具,今天就告诉大家,如何在Linux下安装GIt,并且 ...

  5. python爬虫之scrapy模拟登录

    背景: 初来乍到的pythoner,刚开始的时候觉得所有的网站无非就是分析HTML.json数据,但是忽略了很多的一个问题,有很多的网站为了反爬虫,除了需要高可用代理IP地址池外,还需要登录.例如知乎 ...

  6. sonar安装

    ##jdk不要用yum下载的 一.下载sonar源码 cd /usr/local/src wget https://sonarsource.bintray.com/Distribution/sonar ...

  7. PHPStorm从入门到精通

    1. 使用phpstorm+xdebug进行调试 首先,安装php的xdebug扩展 查看phpinfo中php的版本,php的安装位数,php的是否线程安全:根据这些下载对应的xdebug.dll ...

  8. Java权限访问修饰符

    私有的,以 private 修饰符指定,在同一类内可见. 默认的,也称为 default,在同一包内可见,不使用任何修饰符. 受保护的,以 protected 修饰符指定,对同一包内的类和所有子类可见 ...

  9. [转]Java 的强引用、弱引用、软引用、虚引用

    1.强引用(StrongReference) 强引用是使用最普遍的引用.如果一个对象具有强引用,那垃圾回收器绝不会回收它.如下: Object o=new Object(); // 强引用 当内存空间 ...

  10. jvm相关参数

    官方地址:https://docs.oracle.com/javase/8/docs/technotes/tools/unix/jstat.html#BEHHGFAE 一.查看jvm运行参数 1.查看 ...