Codeforces Round #516 (Div. 2, by Moscow Team Olympiad) D Labyrinth 【双端队列deque+BFS】
程序员文章站
2022-05-09 22:51:26
...
题目链接:http://codeforces.com/contest/1064/problem/D
用bfs扩展的时候有一点需要注意:如果左右两边的扩展范围受限,那么扩展的时候如果不优先处理不受限的扩展,会出现受限的优先扩展后,不但无法继续扩展,还会标记途中的点,导致不受限的扩展无法向前扩展;
所以可以用deque,把最优先的放在前面,其次放在后面;
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <cmath>
#include <queue>
#include <map>
#include <stack>
#include <sstream>
#include <vector>
#include <string>
#include <set>
#include <deque>
using namespace std;
//using namespace __gnu_pbds;
#define IOS ios::sync_with_stdio(false); cin.tie(0);
#define REP(i,n) for(int i=0;i<n;++i)
int read(){
int r=0,f=1;char p=getchar();
while(p>'9'||p<'0'){if(p=='-')f=-1;p=getchar();}
while(p>='0'&&p<='9'){r=r*10+p-48;p=getchar();}return r*f;
}
//typedef tree<pair<long long,int>,null_type,less< pair<long long,int> >,rb_tree_tag,tree_order_statistics_node_update> rbtree;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<long long,long long> pll;
const int Maxn = 2e3+10;
const long long LINF = 1e18;
const int INF = 0x3f3f3f3f;
const int Mod = 10001;
const double PI = acos(-1.0);
struct Node {
int x, y, L, R;
Node (int t1, int t2, int t3, int t4):x(t1), y(t2), L(t3), R(t4) {}
};
char G[Maxn][Maxn];
int n, m, r, c, x, y, cnt, dir[4][2] = {{-1, 0}, {0, 1}, {1, 0}, {0, -1} };
bool vis[Maxn][Maxn];
bool check (int xx, int yy) {
if (xx > n || xx < 1) return false;
else if (yy > m || yy < 1) return false;
else return true;
}
void bfs () {
deque <Node> qu;
qu.push_front(Node (r, c, 0 ,0));
while (!qu.empty()) {
Node tmp = qu.front();
qu.pop_front();
if (vis[tmp.x][tmp.y]) continue;
vis[tmp.x][tmp.y] = 1;
G[tmp.x][tmp.y] = '+';
cnt++;
for (int i = 0; i < 4; ++i) {
int xx = tmp.x+dir[i][0];
int yy = tmp.y+dir[i][1];
if (check (xx, yy) && G[xx][yy] != '*' && !vis[xx][yy]) {
if (tmp.R < y && i == 1) {
qu.push_back(Node (xx, yy, tmp.L, tmp.R+1));
}
if (tmp.L < x && i == 3) {
qu.push_back(Node (xx, yy, tmp.L+1, tmp.R));
}
else if (i == 0 || i == 2)
qu.push_front(Node (xx, yy, tmp.L, tmp.R));
}
}
}
}
int main (void)
{
cin >> n >> m >> r >> c >> x >> y;
for (int i = 1; i <= n; ++i) cin >> (G[i]+1);
cnt = 0;
bfs();
cout << cnt << endl;
return 0;
}