HDU 1429 胜利大逃亡(续) BFS+状压

状压一下然后随意写,注意如果你被魔王抓了一次钥匙就全丢了哦,这样第二个样例就可以解释为什么是-1而不是20了

#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <set>
#include <vector>
#include <string>
#include <queue>
#include <deque>
#include <bitset>
#include <list>
#include <cstdlib>
#include <climits>
#include <cmath>
#include <ctime>
#include <algorithm>
#include <stack>
#include <sstream>
#include <numeric>
#include <fstream>
#include <functional>

using namespace std;

#define MP make_pair
#define PB push_back
typedef long long LL;
typedef unsigned long long ULL;
typedef vector<int> VI;
typedef pair<int,int> pii;
const int INF = INT_MAX / 3;
const double eps = 1e-8;
const LL LINF = 1e17;
const double DINF = 1e60;
const int maxn = 25;
const int maxk = 10;
const int dx[] = {0,0,1,-1};
const int dy[] = {1,-1,0,0};
char mp[maxn][maxn];
int n,m,t,sx,sy,ex,ey;
int dist[maxn][maxn][1 << maxk];

struct Node {
    int x,y,state;
    Node(int x,int y,int state): x(x), y(y), state(state) {}
};


int solve() {
    queue<Node> q;
    q.push(Node(sx,sy,0));
    dist[sx][sy][0] = 0;
    int x,y,nx,ny,s,ns;
    while(!q.empty()) {
        Node now = q.front(); q.pop();
        x = now.x; y = now.y; s = now.state;
        if(dist[x][y][s] == t) continue;
        if(x == ex && y == ey) return dist[x][y][s];
        for(int i = 0;i < 4;i++) {
            nx = x + dx[i];
            ny = y + dy[i];
            ns = s;
            char nc = mp[nx][ny];
            if(nc == '*') continue;
            if(nc >= 'A' && nc <= 'J' && !(s & (1 << (nc - 'A')))) continue;
            if(nc >= 'a' && nc <= 'j') ns |= (1 << (nc - 'a'));
            if(dist[nx][ny][ns] == -1 || dist[nx][ny][ns] > dist[x][y][s] + 1) {
                dist[nx][ny][ns] = dist[x][y][s] + 1;
                q.push(Node(nx,ny,ns));
            }
        }
    }
    return -1;
}

int main() {
    while(scanf("%d%d%d",&n,&m,&t) != EOF) {
        memset(mp,'*',sizeof(mp));
        memset(dist,-1,sizeof(dist));
        for(int i = 1;i <= n;i++) {
            for(int j = 1;j <= m;j++) {
                scanf(" %c",&mp[i][j]);
                if(mp[i][j] == '@') {
                    sx = i; sy = j; mp[i][j] = '.';
                }
                else if(mp[i][j] == '^') {
                    ex = i; ey = j; mp[i][j] = '.';
                }
            }
        }
        printf("%d\n",solve());
    }
    return 0;
}

  

转载于:https://www.cnblogs.com/rolight/p/3936715.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值