zoukankan      html  css  js  c++  java
  • Labyrinth(记忆化BFS)

    Labyrinth

    http://codeforces.com/problemset/problem/1064/D

    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
    Copy
    4 5
    3 2
    1 2
    .....
    .***.
    ...**
    *....
    output
    Copy
    10
    input
    Copy
    4 4
    2 2
    0 1
    ....
    ..*.
    ....
    ....
    output
    Copy
    7
    Note

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

    First example:


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

    Second example:


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

     加个记忆化,判断L和R剩余多少就行

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<cmath>
     5 #include<string>
     6 #include<algorithm>
     7 #include<queue>
     8 #include<vector>
     9 #pragma GCC optimize(2)
    10 using namespace std;
    11 
    12 int n,m;
    13 char map[2005][2005];
    14 struct Num{
    15     int L,R;
    16 }book[2005][2005];
    17 int r,c,L,R;
    18 struct sair{
    19     int x,y,L,R;
    20 };
    21 int dir[4][2]={0,-1,-1,0,0,1,1,0};//R,D,L,U
    22 
    23 void bfs(){
    24     queue<sair>Q;
    25     sair s,e;
    26     s.x=r,s.y=c,s.L=L,s.R=R;
    27     Q.push(s);
    28     book[s.x][s.y].L=L;
    29     book[s.x][s.y].R=R;
    30     while(!Q.empty()){
    31         s=Q.front();
    32         Q.pop();
    33         for(int i=0;i<4;i++){
    34             e.x=s.x+dir[i][0];
    35             e.y=s.y+dir[i][1];
    36             if(e.x>=0&&e.x<n&&e.y>=0&&e.y<m&&map[e.x][e.y]!='*'){
    37                 e.L=s.L;
    38                 e.R=s.R;
    39                 if(i==2){
    40                     e.R--;
    41                     if(e.R<0) continue;
    42                 }
    43                 else if(i==0){
    44                     e.L--;
    45                     if(e.L<0) continue;
    46                 }
    47                 if(book[e.x][e.y].L<e.L||book[e.x][e.y].R<e.R){
    48                     book[e.x][e.y].L=max(book[e.x][e.y].L,e.L);
    49                     book[e.x][e.y].R=max(book[e.x][e.y].R,e.R);
    50                     Q.push(e);
    51                 }
    52 
    53             }
    54         }
    55     }
    56 }
    57 
    58 int main(){
    59     cin>>n>>m;
    60     cin>>r>>c;
    61     cin>>L>>R;
    62     for(int i=0;i<=2000;i++){
    63         for(int j=0;j<=2000;j++){
    64             book[i][j].L=book[i][j].R=-1;
    65         }
    66     }
    67     for(int i=0;i<n;i++){
    68         cin>>map[i];
    69     }
    70     r--,c--;
    71     bfs();
    72     int ans=0;
    73     for(int i=0;i<n;i++){
    74         for(int j=0;j<m;j++){
    75             if(book[i][j].L!=-1||book[i][j].R!=-1){
    76                 ans++;
    77             }
    78         }
    79     }
    80     cout<<ans<<endl;
    81 }
    View Code
  • 相关阅读:
    Linux 桌面玩家指南:18. 使用 Docker 隔离自己的开发环境和部署环境
    Linux 桌面玩家指南:17. 在 Ubuntu 中使用 deepin-wine,解决一些依赖 Windows 的痛点问题
    Linux 桌面玩家指南:16. 使用 CUDA 发挥显卡的计算性能
    Linux 桌面玩家指南:15. 深度学习可以这样玩
    Linux 桌面玩家指南:14. 数值计算和符号计算
    Linux 桌面玩家指南:13. 使用 Git 及其和 Eclipse 的集成
    Linux 桌面玩家指南:12. 优秀的文本化编辑思想大碰撞(Markdown、LaTeX、MathJax)
    Linux 桌面玩家指南:11. 在同一个硬盘上安装多个 Linux 发行版以及为 Linux 安装 Nvidia 显卡驱动
    Linux 桌面玩家指南:10. 没有 GUI 的时候应该怎么玩
    Linux 桌面玩家指南:09. X Window 的奥秘
  • 原文地址:https://www.cnblogs.com/Fighting-sh/p/9811620.html
Copyright © 2011-2022 走看看