codeforce 1064D bfs
D. 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 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 ≤ 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:
.++. .+*. .++. .++.
#include <iostream>
#include <algorithm>
#include <queue>
#include <cstdio>
#include <cstring>
#include <math.h>
#include <queue>
#include <vector>
#define sd(qwe) scanf("%d",&qwe)
#define ll long long
#define sdd(qwee) scanf("%I64d",&qwee)
using namespace std;
int a,b,c,d,e,f,ans;
char mp[2005][2005];
int vis[2005][2005];
int gox[]={0,0,0,1,-1};
int goy[]={0,1,-1,0,0};
struct NODE
{
int x, y, l, r;
bool operator <(const NODE b)const
{
if(l==b.l)
return r<b.r;
return l<b.l;
}
};
bool isgo(int x,int y)
{
if(x>=1&&y>=1&&x<=a&&y<=b&&mp[x][y]!='*')return true;
return false;
}
void bfs(int x,int y,int X,int Y)
{
priority_queue<NODE> que;
while(!que.empty())que.pop();
que.push((NODE){x,y,X,Y});
vis[x][y]=1;ans++;
while(!que.empty())
{
NODE tp=que.top();
que.pop();
for(int i=1;i<=4;i++)
{
int newx=tp.x+gox[i];int newy=tp.y+goy[i];
if(isgo(newx,newy)&&!vis[newx][newy]&&((tp.l-(i==2?1:0))>=0)&&((tp.r-(i==1?1:0))>=0))
{
ans++;
vis[newx][newy]=1;
que.push((NODE){newx,newy,tp.l-(i==2?1:0),tp.r-(i==1?1:0)});
}
}
}
}
int main()
{
ans=0;
memset(vis,0,sizeof(vis));
sd(a);sd(b);sd(c);sd(d);sd(e);sd(f);
for(int i=1;i<=a;i++)
{
scanf("%s",mp[i]+1);
}
bfs(c,d,e,f);
cout<<ans<<endl;
}
上一篇: 从源代码构建MYSQL
下一篇: 栈和队列的基本用法