D. Olya and Energy Drinks (状压BFS)

http://codeforces.com/problemset/problem/877/D

题意:就是一般的迷宫问题的模型,但不过有一个条件就是一次可以往一个方向最多走k步

做法:错得原因可能是vis标记上面,正确的做法应该是记录x,y这个点的四个方向来的情况,如果他在东方面的点来过了,下次就再从东方面来,就不需要了。

#include "bits/stdc++.h"

using namespace std;
const double eps = 1e-8;
#define lowbit(x) x&-x
#define pll pair<ll,ll>
#define pii pair<int,int>
#define fi first
#define se second
#define makp make_pair

int dcmp(double x) {
    if (fabs(x) < eps) return 0;
    return (x > 0) ? 1 : -1;
}

typedef long long ll;
typedef unsigned long long ull;
const ull hash1 = 201326611;
const ull hash2 = 50331653;
const int N = 1000000 + 10;
const int M = 1000 + 10;
const int inf = 0x3f3f3f3f;
const ll mod = 1000000000 + 7;
int n, m, k, sx, sy, ex, ey;
char mp[M][M];
int vis[M][M], dis[M][M];
int dx[4] = {0, 0, 1, -1}, dy[4] = {1, -1, 0, 0};

int check(int tx, int ty) {
    if (tx < 1 || tx > n || ty < 1 || ty > m)
        return 0;
    if (mp[tx][ty] == '#')
        return 0;
    return 1;
}

int bfs() {
    queue<pii > qu;
    qu.push(makp(sx, sy));
    vis[sx][sy] = (1 << 4) - 1;
    dis[sx][sy] = 0;
    while (!qu.empty()) {
        pii now = qu.front();
        qu.pop();
        if (now.fi == ex && now.se == ey) return dis[ex][ey];
        for (int i = 0; i < 4; i++) {
            int tmp = k, tx = now.fi, ty = now.se;
            while (tmp--) {
                tx += dx[i], ty += dy[i];
                if (!check(tx, ty)) break;
                if ((vis[tx][ty] >> i) & 1) break;
                vis[tx][ty] |= (1 << i);
                if (dis[tx][ty] != 0) dis[tx][ty] = min(dis[tx][ty], dis[now.fi][now.se] + 1);
                else dis[tx][ty] = dis[now.fi][now.se] + 1;
                qu.push(makp(tx, ty));
            }
        }
    }
    return -1;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);
    cin >> n >> m >> k;
    for (int i = 1; i <= n; i++) {
        cin >> mp[i] + 1;
    }
    cin >> sx >> sy >> ex >> ey;
    cout << bfs() << endl;
    return 0;
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值