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;
}

  

时间: 2024-10-13 02:37:42

HDU 1429 胜利大逃亡(续) BFS+状压的相关文章

hdu 1429 胜利大逃亡(续)(bfs+位压缩)

胜利大逃亡(续) Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 7346    Accepted Submission(s): 2546 Problem Description Ignatius再次被魔王抓走了(搞不懂他咋这么讨魔王喜欢)…… 这次魔王汲取了上次的教训,把Ignatius关在一个n*m的地牢里,并在地牢的某些地方安装了带

hdu.1429.胜利大逃亡(续)(bfs + 0101011110)

胜利大逃亡(续) Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 6070    Accepted Submission(s): 2096 Problem Description Ignatius再次被魔王抓走了(搞不懂他咋这么讨魔王喜欢)……这次魔王汲取了上次的教训,把Ignatius关在一个n*m的地牢里,并在地牢的某些地方安装了带

hdu - 1429 胜利大逃亡(续) (bfs状态压缩)

http://acm.hdu.edu.cn/showproblem.php?pid=1429 终于开始能够做状态压缩的题了,虽然这只是状态压缩里面一道很简单的题. 状态压缩就是用二进制的思想来表示状态. 总共有10种钥匙,那么开一个(1<<10 的数组) 那么每次遇到一把钥匙我就用当前状态 |  钥匙转化为2进制的数值,然后遇到门的时候判断是否有对应的钥匙,只要用当前状态 & 门转化为2进制的值就可以.初始为0时,state=0,表示10个状态位都是0.那么每次遇到钥匙就改变相应的状态

HDU 1429 胜利大逃亡(续)(bfs+状态压缩,很经典)

传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1429 胜利大逃亡(续) Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 10648    Accepted Submission(s): 3860 Problem Description Ignatius再次被魔王抓走了(搞不懂他咋这么讨魔王

HDU 1429胜利大逃亡(续) (bfs+状态压缩)

胜利大逃亡(续) Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 6469 Accepted Submission(s): 2243 Problem Description Ignatius再次被魔王抓走了(搞不懂他咋这么讨魔王喜欢)-- 这次魔王汲取了上次的教训,把Ignatius关在一个n*m的地牢里,并在地牢的某些地方安装了带锁的门,钥

HDU 1429 胜利大逃亡(续)(bfs)

胜利大逃亡(续) Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 6270    Accepted Submission(s): 2177 Problem Description Ignatius再次被魔王抓走了(搞不懂他咋这么讨魔王喜欢)-- 这次魔王汲取了上次的教训,把Ignatius关在一个n*m的地牢里,并在地牢的某些地方安装了

hdu 1429 胜利大逃亡(续)【广度优先搜索+状态压缩】

胜利大逃亡(续) Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 5411    Accepted Submission(s): 1863 Problem Description Ignatius再次被魔王抓走了(搞不懂他咋这么讨魔王喜欢)-- 这次魔王汲取了上次的教训,把Ignatius关在一个n*m的地牢里,并在地牢的某些地方安装了

hdu 1429 胜利大逃亡(续) 搜索+状态压缩,,不错的题。

胜利大逃亡(续) Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 5860    Accepted Submission(s): 2046 Problem Description Ignatius再次被魔王抓走了(搞不懂他咋这么讨魔王喜欢)-- 这次魔王汲取了上次的教训,把Ignatius关在一个n*m的地牢里,并在地牢的某些地方安装了

hdu 1429 胜利大逃亡(续)(BFS+位压缩)

#include <stdio.h> #include <string.h> #include <iostream> #include <algorithm> #include <vector> #include <queue> #include <stack> #include <set> #include <map> #include <string> #include <ma