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)\):
從一個點到另一個點橫向走的步數(shù) \(x\) 和縱向走的步數(shù) \(y\)
\(x-y\) 為定值
所以對于一個點,我們只要向左右任意一個方向建一條距離為 \(1\) 的邊,其余三個方向都建距離為 \(0\) 的邊,然后跑 \(Dijkstra\) 就好了,最后時候算一下要向左向右走幾步判一下就好了
解法二 \((deque)\):
考試的時候?qū)懥俗畋┝Φ?/strong> \(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\n",ans);return 0; }
作者:skl_win
出處:https://www.cnblogs.com/shaokele/
本文版權(quán)歸作者和博客園共有,歡迎轉(zhuǎn)載,但未經(jīng)作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文連接,否則保留追究法律責任的權(quán)利。
轉(zhuǎn)載于:https://www.cnblogs.com/shaokele/p/9791063.html
總結(jié)
以上是生活随笔為你收集整理的codeforces Labyrinth的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: c# TCP高性能通信
- 下一篇: 闲话杂谈—至曾经的自己