http://codeforces.com/contest/1064/problem/D
bfs
用双端队列做,向上走向下走的节点加入队首,左右走的加到队尾,其余的都是正常bfs
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxN=2005;
int N,M,X,Y,ans;
char ma[maxN][maxN];
int dir[4][2]={1,0,-1,0,0,1,0,-1};
struct DM
{
int x,y,l,r;
};
void bfs(int sx,int sy)
{
deque<DM>que;
que.push_back((DM){sx,sy,X,Y});
ma[sx][sy]='*';
while(!que.empty())
{
DM v=que.front();
que.pop_front();
ans++;
for(int i=0;i<4;i++)
{
int xx=v.x+dir[i][0];
int yy=v.y+dir[i][1];
if(xx<1||xx>N||yy<1||yy>M||ma[xx][yy]=='*')continue;
if(i<=1)que.push_front((DM){xx,yy,v.l,v.r}),ma[xx][yy]='*';
if(i==2&&v.r>=1)que.push_back((DM){xx,yy,v.l,v.r-1}),ma[xx][yy]='*';
if(i==3&&v.l>=1)que.push_back((DM){xx,yy,v.l-1,v.r}),ma[xx][yy]='*';
}
}
}
int main()
{
int sx,sy;
scanf("%d%d%d%d%d%d",&N,&M,&sx,&sy,&X,&Y);
for(int i=1;i<=N;i++)scanf("%s",ma[i]+1);
bfs(sx,sy);
printf("%d\n",ans);
return 0;
}