acw4481_方格探索

4481. 方格探索

/* 
1st" f[i][j][a][b]表示向右a步向左b步能否到达(i,j) => 
2nd" f[i][j][a]表示向右走a步能够到达(i,j)最少需要向左走多少步 => 
3rd" f[i][j]表示能够到达(i,j)最少要向右走多少步(j - c == a - b)
4th" 向上下左走的代价为0 向右走代价为1 => 0-1bfs最短路(双端队列)
*/
#include<bits/stdc++.h>
using namespace std;
using PII = pair<int,int>;
char g[2010][2010];
bool st[2010][2010];
int n, m, r, c, x, y, f[2010][2010];
const int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};
void bfs(){
    deque<PII>q;
    q.push_back({r,c});
    memset(f, 0x3f, sizeof f);
    f[r][c] = 0;
    while(q.size()) {
        auto t = q.front();
        q.pop_front();
        int x = t.first, y = t.second;
        if(st[x][y]) continue;
        st[x][y] = 1;
        for(int i = 0; i < 4; ++i) {
            int a = x + dx[i], b = y + dy[i];
            if(a >= 1 && a <= n && b >= 1 && b <= m && g[a][b] != '*' && !st[a][b]) {
                int w = 0;
                if(i == 1) w = 1;
                if(f[a][b] > f[x][y] + w) {
                    f[a][b] = f[x][y] + w;
                    if(w == 1) q.push_back({a, b});
                    else q.push_front({a, b});
                }
            }
        }
    }
}
int main()
{
    cin >> n >> m >> r >> c >> x >> y;
    for(int i = 1; i <= n; ++i) {
        for(int j = 1; j <= m; ++j) {
            cin >> g[i][j];
        }
    }
    
    bfs();
    
    int res = 0;
    
    for(int i = 1; i <= n; ++i) {
        for(int j = 1; j <= m; ++j) {
            int a = f[i][j], b = a - j + c;
            if(a <= y && b <= x) res ++;
        }
    }
    
    cout << res << endl;
    
    return 0;
}
posted @ 2022-06-13 18:02  Ivessas  阅读(18)  评论(0)    收藏  举报