codeforces Labyrinth


codeforces Labyrinth##

  Time Limit: 2 Sec
  Memory Limit: 512 MB

Description###

   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 (n, m (1 ≤ n, m ≤ 2000)) — the number of rows and the number columns in the labyrinth respectively.
   The second line contains two integers (r, c (1 ≤ r ≤ n, 1 ≤ c ≤ m)) — index of the row and index of the column that define the starting cell.
   The third line contains two integers (x, y (0 ≤ x, y ≤ 10^9)) — 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.
 

Sample Input 1###

   (4) (5)
   (3) (2)
   (1) (2)
   (.....)
   (.***.)
   (...**)
   (*....)
 

Sample Output 1###

  (10)
    

Sample Input 2###

   (4) (4)
   (2) (2)
   (0) (1)
   (....)
   (..*.)
   (....)
   (....)
  

Sample Output 2###

  (7)
    

HINT

  Cells, reachable in the corresponding example, are marked with '+'.
  
  First example:
  (+++..)
  (+***.)
  (+++**)
  (*+++.)
  Second example:
  (.++.)
  (.+*.)
  (.++.)
  (.++.)
  

题目地址:codeforces Labyrinth

题目大意:

  读入一个 (n*m) 的图起点为 ((r,c))
  最多可以向左走 (x) 步,向右走 (y)
  问可以到那些点

题解:

  解法一 ((Dijkstra))
  从一个点到另一个点横向走的步数 (x) 和纵向走的步数 (y)
  (x-y) 为定值
  所以对于一个点,我们只要向左右任意一个方向建一条距离为 (1) 的边,其余三个方向都建距离为 (0) 的边,然后跑 (Dijkstra) 就好了,最后时候算一下要向左向右走几步判一下就好了
  解法二 ((deque))
  考试的时候写了最暴力的 (bfs ......)
  因为可能会先做花一些代价的方案到某一个点而把不用花代价的方案给排掉了
  所以我们可以每次把不用花代价的方案放到队首,把花代价的放到队尾
  每次从队首取点,就写完了
  orz大神们


AC代码

#include <cstdio>
#include <deque>
using namespace std;
const int N=2005;
int n,m,R,C,x,y,ans;
char ch[N][N];
bool vis[N][N];
struct note{
    int x,y,a,b;
};
deque<note> q;
int main(){
    scanf("%d%d",&n,&m);
    scanf("%d%d",&R,&C);
    scanf("%d%d",&x,&y);
    for(int i=1;i<=n;i++)
        scanf("%s",ch[i]+1);
    q.push_back((note){R,C,x,y});
    while(!q.empty()){
        note tmp=q.front();q.pop_front();
        int x=tmp.x,y=tmp.y;
        if(vis[x][y])continue;
        vis[x][y]=1;ans++;
        if(1<=x-1 && ch[x-1][y]=='.')q.push_front((note){x-1,y,tmp.a,tmp.b});
        if(x+1<=n && ch[x+1][y]=='.')q.push_front((note){x+1,y,tmp.a,tmp.b});
        if(1<=y-1 && ch[x][y-1]=='.' && tmp.a)q.push_back((note){x,y-1,tmp.a-1,tmp.b});
        if(y+1<=m && ch[x][y+1]=='.' && tmp.b)q.push_back((note){x,y+1,tmp.a,tmp.b-1});
    }
    printf("%d
",ans);
    return 0;
}


  作者:skl_win
  出处:https://www.cnblogs.com/shaokele/
  本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

原文地址:https://www.cnblogs.com/shaokele/p/9791063.html