zoukankan      html  css  js  c++  java
  • codeforces1063/B(水中有坑,难受)

     B. Labyrinth

    time limit per test
    2 seconds
    memory limit per test
    512 megabytes
    input
    standard input
    output
    standard output

    You are playing some computer game. One of its levels puts you in a maze consisting of n lines, each of which contains m cells. Each cell either is free or is occupied by an obstacle. The starting cell is in the row r and column c. In one step you can move one square up, left, down or right, if the target cell is not occupied by an obstacle. You can't move beyond the boundaries of the labyrinth.

    Unfortunately, your keyboard is about to break, so you can move left no more than x times and move right no more than y times. There are no restrictions on the number of moves up and down since the keys used to move up and down are in perfect condition.

    Now you would like to determine for each cell whether there exists a sequence of moves that will put you from the starting cell to this particular one. How many cells of the board have this property?

    Input

    The first line contains two integers nm (1 ≤ n, m ≤ 2000) — the number of rows and the number columns in the labyrinth respectively.

    The second line contains two integers rc (1 ≤ r ≤ n1 ≤ c ≤ m) — index of the row and index of the column that define the starting cell.

    The third line contains two integers xy (0 ≤ x, y ≤ 109) — the maximum allowed number of movements to the left and to the right respectively.

    The next n lines describe the labyrinth. Each of them has length of m and consists only of symbols '.' and '*'. The j-th character of the i-th line corresponds to the cell of labyrinth at row i and column j. Symbol '.' denotes the free cell, while symbol '*' denotes the cell with an obstacle.

    It is guaranteed, that the starting cell contains no obstacles.

    Output

    Print exactly one integer — the number of cells in the labyrinth, which are reachable from starting cell, including the starting cell itself.

    Examples
    input
    4 5
    3 2
    1 2
    .....
    .***.
    ...**
    *....
    output
    10
    input
    4 4
    2 2
    0 1
    ....
    ..*.
    ....
    ....
    output
    7
    Note

    Cells, reachable in the corresponding example, are marked with '+'.

    First example:


    +++..
    +***.
    +++**
    *+++.

    Second example:


    .++.
    .+*.
    .++.
    .++.

    题意:给出图,给定左转右转的次数限制,问你从起点最多可到达几个点(包括起点)。

     

    思路:直接bfs(有左右次数的限制的bfs)但是!!!,要先走左右转弯花费次数小的(可惜没想到,太菜了),因此可以用优先队列。

    剩下的看代码吧。

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<string.h>
     4 #include<queue>
     5 #define LL long long
     6 using namespace std;
     7 char map[2010][2010];
     8 bool vis[2010][2010];
     9 int dir[][2]={-1,0,1,0,0,-1,0,1};//上下左右
    10 LL n,m,r,c,x,y,cnt;
    11 struct node{
    12     LL x,y;
    13     LL l,r;//左右
    14     friend bool operator < (node a,node b){
    15         return a.l+a.r<b.l+b.r;//剩余左右转次数大的先走 
    16     }
    17 }n1,n2;
    18 void bfs(){
    19     priority_queue<node>Q;
    20     n1.x=r,n1.y=c;
    21     n1.l=x,n1.r=y;
    22     Q.push(n1);
    23     cnt=1;
    24     vis[r][c]=true;
    25     while(!Q.empty()){
    26         n2=Q.top();
    27         Q.pop();
    28         for(int i=0;i<4;i++){
    29             n1.x=n2.x+dir[i][0],n1.y=n2.y+dir[i][1];
    30             if(n1.x>=1&&n1.y>=1&&n1.x<=n&&n1.y<=m&&map[n1.x][n1.y]=='.'){
    31                 if(!vis[n1.x][n1.y]){
    32                     if(i==2){//
    33                         if(i==2&&n2.l){
    34                             n1.l=n2.l-1;
    35                             n1.r=n2.r;//这别忘写,我就忘了,找半天 
    36                             vis[n1.x][n1.y]=true;
    37                             cnt++;//加答案 
    38                             Q.push(n1);
    39                         }
    40                     }
    41                     else if(i==3){//
    42                         if(i==3&&n2.r){
    43                             n1.r=n2.r-1;
    44                             n1.l=n2.l;// 还有这,唉,写代码要细心 
    45                             vis[n1.x][n1.y]=true;
    46                             cnt++;
    47                             Q.push(n1);
    48                         }
    49                     }else{
    50                         n1.l=n2.l;
    51                         n1.r=n2.r;
    52                         vis[n1.x][n1.y]=true;
    53                         cnt++;
    54                         Q.push(n1);
    55                     }
    56                 }
    57             }
    58         }
    59     }
    60 }
    61 int main(){
    62     scanf("%lld%lld%lld%lld%lld%lld",&n,&m,&r,&c,&x,&y);
    63     for(int i=1;i<=n;i++){
    64         cin>>map[i]+1;
    65     }
    66     bfs();
    67     printf("%lld
    ",cnt);
    68     return 0;
    69 }

    有错请指正。

  • 相关阅读:
    在包a中新建一个类A,在类A中有一个int add(int m)方法,用来求1+2+…+m 的和。在包b中新建一个类B,在类B中有一个int cheng(int n)方法,用来求n! 的结果。在包c中新建一个主类C,调用A、B中的方法输出1+2+…+30的和, 以及5!的计算结果。
    在包a中编写一个类Father,具有属性:年龄(私有)、姓名(公有); 具有功能:工作(公有)、开车(公有)。 在包a中编写一个子类Son,具有属性:年龄(受保护的)、姓名; 具有功能:玩(私有)、学习(公有)。 最后在包b中编写主类Test,在主类的main方法中测试类Father与类Son。
    简单且线程安全的两个单例模式java程序
    Condition的优点
    Java设计模式—生产者消费者模式(阻塞队列实现)
    Java NIO使用及原理分析 (一)
    一步步优化JVM五:优化延迟或者响应时间(1)
    UML 类图
    Eclipse中构建Fluent风格到Formatter
    Memcache 问题集锦
  • 原文地址:https://www.cnblogs.com/liuzuolin/p/10484695.html
Copyright © 2011-2022 走看看