【HDOJ】1429 胜利大逃亡(续)

BFS+状态压缩,做了很多状态压缩了。今晚把八数码问题给搞定了。

 #include <iostream>
#include <queue>
#include <cstring>
#include <cstdio>
using namespace std; typedef struct node_st {
int x, y, t;
int key;
node_st() {}
node_st(int xx, int yy, int tt, int kk) {
x = xx; y = yy; t = tt; key = kk;
}
} node_st; char map[][];
char visit[<<][][];
int direct[][] = {{-,},{,},{,-},{,}};
int n, m, time; int bfs(int bx, int by) {
int x, y, key, t;
int i, tmp;
queue<node_st> que;
node_st node; memset(visit, , sizeof(visit));
visit[bx][by][] = ;
que.push(node_st(bx,by,,)); while ( !que.empty() ) {
node = que.front();
if (node.t >= time)
return -;
if (map[node.x][node.y] == '^')
return node.t;
que.pop();
t = node.t + ;
for (i=; i<; ++i) {
x = node.x + direct[i][];
y = node.y + direct[i][];
if (x< || x>=n || y< || y>=m)
continue;
if (map[x][y]>='a' && map[x][y]<='j')
key = node.key | (<<(map[x][y]-'a'));
else
key = node.key;
if (visit[key][x][y] || map[x][y]=='*')
continue;
if (map[x][y]>='A' && map[x][y]<='J') {
tmp = (<<(map[x][y]-'A')) & key;
if ( !tmp )
continue;
}
que.push(node_st(x,y,t,key));
visit[key][x][y] = ;
}
} return -;
} int main() {
int bx, by;
int i, j; while (scanf("%d %d %d", &n, &m, &time) != EOF) {
for (i=; i<n; ++i) {
scanf("%s", map[i]);
for (j=; j<m; ++j) {
if (map[i][j] == '@') {
bx = i;
by = j;
}
}
}
i = bfs(bx, by);
printf("%d\n", i);
} return ;
}
上一篇:c语言学习笔记(10)——结构体


下一篇:它们的定义View