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

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

Examples

Input

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

Copy
  1. 10
Input

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

Copy
  1. 7

解题思路:直接用BFS模拟行走过程,不过需要多记录下向左和向右行走的步数。为了保证保留向左向右行走的次数,我们优先向上下行走,能向上下行走就先不往左右行走。直接用双向队列,如果是向上下行走就放在队首,如果是左右行走放在队尾。

附上代码:

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. int n,m,vis[][],ans,sx,sy,L,R;
  4. int dir[][]={{,},{-,},{,-},{,}};
  5. char mp[][];
  6. struct node{
  7. int x,y,l,r;
  8. };
  9. bool check(node x)
  10. {
  11. if(x.x<=||x.y<=||x.x>n||x.y>m||vis[x.x][x.y]||mp[x.x][x.y]=='*')
  12. return false;
  13. return true;
  14. }
  15. void BFS()
  16. {
  17. deque<node> que;
  18. vis[sx][sy]=;
  19. ans++;
  20. node s;
  21. s.x=sx; s.y=sy; s.l=L; s.r=R;
  22. que.push_back(s);
  23. while(!que.empty())
  24. {
  25. node now=que.front();
  26. que.pop_front();
  27. node next;
  28. for(int i=;i<;i++)
  29. {
  30. next.x=now.x+dir[i][];
  31. next.y=now.y+dir[i][];
  32. if(check(next))
  33. {
  34. if(i<)
  35. {
  36. next.l=now.l; next.r=now.r;
  37. que.push_front(next);
  38. vis[next.x][next.y]=;
  39. ans++;
  40. }
  41. if(i==&&now.l>=)
  42. {
  43. next.l=now.l-;
  44. next.r=now.r;
  45. que.push_back(next);
  46. vis[next.x][next.y]=;
  47. ans++;
  48. }
  49. if(i==&&now.r>=)
  50. {
  51. next.l=now.l;
  52. next.r=now.r-;
  53. que.push_back(next);
  54. vis[next.x][next.y]=;
  55. ans++;
  56. }
  57. }
  58. }
  59. }
  60. }
  61. int main()
  62. {
  63. scanf("%d%d%d%d%d%d",&n,&m,&sx,&sy,&L,&R);
  64. for(int i=;i<=n;i++)
  65. for(int j=;j<=m;j++)
  66. cin>>mp[i][j];
  67. BFS();
  68. cout<<ans<<endl;
  69. return ;
  70. }

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. day 7-2 multiprocessing开启多进程

    一. multiprocessing模块介绍 python中的多线程无法利用多核优势,如果想要充分地使用多核CPU的资源(os.cpu\_count\(\)查看),在python中大部分情况需要使用多 ...

  2. Navicat 远程连接Docker容器中的mysql 报错:1251 - Client does not support authentication protocol 解决办法。

    出现这个问题 首先进入 1.docker exec -it mysql02 bash      //mysql02是mysql容器的别名 2.mysql -uroot -p 3.输入密码 4.进入my ...

  3. 建议2---编写pythonic代码

    (1)要避免劣化代码 1)避免只用大小写来区分不同的对象.如a是一个数值类型变量,A是String类型,虽在编码过程容易区分二者的含义,但这样做毫无益处,它不会给其他阅读代码的人带来多少便利. 2)避 ...

  4. NIO和经典IO

    NIO未必更快,在Linux上使用Java6完成的测试中,多线程经典I/O设计胜出NIO30%左右 异步I/O强于经典I/O:服务器需要支持超大量的长期连接,比如10000个连接以上,不过各个客户端并 ...

  5. 和docket的第一次亲密接触

    很久很久以前,第一次听说docker时,感觉很高大上,同时自我感觉会很难.所以一直没有详细了解.前一段时间偶尔看到关于docker的详细介绍,于是乎来了兴趣.自已折腾了一下,发现不是想象中的那么难. ...

  6. SSM+shiro,所有配置文件,详细注释版,自用

    spring配置文件applicationContext.xml,放在resources下 <?xml version="1.0" encoding="UTF-8& ...

  7. flask 保存文件到 七牛云

    上篇文章队长讲述了如何把前端上传的文件保存到本地项目目录 本篇 讲述一下把前端上传的文件保存到 第三方存储(七牛云) 七牛云相关步骤思路: 首先 进去七牛云官网,注册并实名认证来获取一个七牛云账号和存 ...

  8. ES 6 系列 - 变量声明

    let 和 const let 声明 (一)基本用法 let 声明的变量只在块级作用域内有效,出了该块则报错,最常见且适合的地方在 for 循环中: var a = []; for (var i = ...

  9. codeforces545C

    Woodcutters CodeForces - 545C Little Susie listens to fairy tales before bed every day. Today's fair ...

  10. Web.xml中Filter过滤器标签几个说明

    在研究liferay框架中看到Web.xml中加入了过滤器的标签,可以根据页面提交的URL地址进行过滤,发现有几个新标签没用过,下面就介绍以下几个过滤器的标签用法: <!-- 定义Filter ...