bfs+位压缩:

hdu 1429 http://acm.hdu.edu.cn/showproblem.php?pid=1429

中文题目:

思路:bfs+位压缩

#include <iostream>
#include <cstdio>
#include <cmath>
#include <vector>
#include <cstring>
#include <algorithm>
#include <string>
#include <set>
#include <functional>
#include <numeric>
#include <sstream>
#include <stack>
#include <map>
#include <queue>

#define CL(arr, val) memset(arr, val, sizeof(arr))

#define lc l,m,rt<<1
#define rc m + 1,r,rt<<1|1
#define pi acos(-1.0)
#define L(x)    (x) << 1
#define R(x)    (x) << 1 | 1
#define MID(l, r)   (l + r) >> 1
#define Min(x, y)   (x) < (y) ? (x) : (y)
#define Max(x, y)   (x) < (y) ? (y) : (x)
#define E(x)        (1 << (x))
#define iabs(x)     (x) < 0 ? -(x) : (x)
#define OUT(x)  printf("%I64d\n", x)
#define lowbit(x)   (x)&(-x)
#define Read()  freopen("din.txt", "r", stdin)
#define Write() freopen("dout.txt", "w", stdout)


#define M 137
#define N 22

using namespace std;
const double eps = 1e-10;

struct node
{
    int x,y;
    int val;
    int msk;//一共10为来表示到达某点时所拥有的钥匙种类
    bool operator < (const node &a) const
    {
        return val > a.val;
    }
}nd;

priority_queue<node>Q;
int sx,sy,ex,ey;

char mat[N][N];
int n,m,k;

int dx[4] = {-1,0,1,0};
int dy[4] = {0,1,0,-1};
bool vt[N][N][1028];//由于是优先队列处理所以只要到达该点就是最优的

int bfs(int x,int y)
{
    CL(vt,false);
    while (!Q.empty()) Q.pop();
    nd.x = x; nd.y = y;
    nd.val = 0; nd.msk = 0;
    Q.push(nd);
    vt[x][y][nd.msk] = true;
    while (!Q.empty())
    {
        node u = Q.top(); Q.pop();
        for (int i = 0; i < 4; ++i)
        {
            int tx = u.x + dx[i];
            int ty  =u.y + dy[i];
            int val = u.val + 1;
            int msk = u.msk;

            if (val >= k) continue;

            if (tx >= 0 && tx < n && ty >= 0 && ty < m && mat[tx][ty] != '*' && !vt[tx][ty][msk])
            {
                nd.x = tx; nd.y = ty;
                nd.val = val; nd.msk = msk;
                if (mat[tx][ty] == '.' || mat[tx][ty] == '@')
                {
                    vt[tx][ty][msk] = true;
                    Q.push(nd);
                }
                else if (mat[tx][ty] >= 'a' && mat[tx][ty] <= 'j')
                {
                    nd.msk = nd.msk | (1<<(mat[tx][ty] - 32 - 'A'));
                    vt[tx][ty][nd.msk] = true;
                    Q.push(nd);
                }
                else if (mat[tx][ty] >= 'A' && mat[tx][ty] <= 'J')
                {
                    if (u.msk & (1<<(mat[tx][ty] - 'A')))
                    {
                        vt[tx][ty][msk] = true;
                        Q.push(nd);
                    }
                }
                else if (mat[tx][ty] == '^')
                {
                    return nd.val;
                }
            }
        }
    }
    return -1;
}
int main()
{
//    Read();
    int i,j;
    while (~scanf("%d%d%d",&n,&m,&k))
    {
        for (i = 0; i < n; ++i)
        {
            scanf("%s",mat[i]);
            for (j = 0; j < m; ++j)
            {
                if (mat[i][j] == '@')
                {
                    sx = i; sy = j;
                }
            }
        }
        printf("%d\n", bfs(sx,sy));
    }
    return 0;
}
View Code

相关文章: