【POJ】3523 The Morning after Halloween

时间:2022-05-21 22:20:14

1. 题目描述
$m \times n$的迷宫(最大为$16 \times 16$)包含最多3个点,这些点可以同时向相邻方向移动或保持停止,使用小写字母表示起始位置,使用大写字母表示中止位置。求最少经过多少时间,这些点可以从起始位置移动到对应的终止位置。

2. 基本思路
这是很经典的路径搜索问题,一般采用BFS。因为题目说每个$2 \times 2$个子矩阵,都至少有一个点为#,那么起始空白可走的点一定很少,因此,可以预处理这些点可以通过1个时间单位到达的有效位置。这样可以降低$5^3$的总排列。显然,同时对三个点组成的三元组进行状态压缩,这里采用移位。这些做完了,普通的BFS+map就已经可以解出正确解了。但是太慢了。因此,使用双向BFS+map,发现还是超时,原因是map太慢了(而且会随着状态的增加越来越慢)。那么,直接用数组存(注意不要MLE)。直接过了。双向BFS明显地提高了性能。

3. 代码

 /* 3523 */
#include <iostream>
#include <sstream>
#include <string>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#include <deque>
#include <bitset>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <cstring>
#include <climits>
#include <cctype>
#include <cassert>
#include <functional>
#include <iterator>
#include <iomanip>
using namespace std;
//#pragma comment(linker,"/STACK:102400000,1024000") #define sti set<int>
#define stpii set<pair<int, int> >
#define mpii map<int,int>
#define vi vector<int>
#define pii pair<int,int>
#define vpii vector<pair<int,int> >
#define rep(i, a, n) for (int i=a;i<n;++i)
#define per(i, a, n) for (int i=n-1;i>=a;--i)
#define clr clear
#define pb push_back
#define mp make_pair
#define fir first
#define sec second
#define all(x) (x).begin(),(x).end()
#define SZ(x) ((int)(x).size())
#define lson l, mid, rt<<1
#define rson mid+1, r, rt<<1|1
#define INF 0x3f3f3f3f
#define mset(a, val) memset(a, (val), sizeof(a)) const int maxn = ;
const int maxm = ;
int ID[maxn][maxn];
int sz[maxm];
int Move[maxm][];
int visit[][maxm][maxm][maxm];
char s[maxn][maxn];
map<char,int> ptb;
queue<int> Q[];
int dir[][] = {
-, , , , , -, , , ,
};
int n, m, gn;
int st, ed; inline bool judge(int x, int y) {
return x< || x>=n || y< || y>=m || s[x][y]=='#';
} void init() {
int cnt = ;
map<char,int>::iterator iter; ptb.clr(); rep(i, , n) {
rep(j, , m) {
if (s[i][j] == '#')
continue; ID[i][j] = cnt++;
if (s[i][j] != ' ')
ptb[s[i][j]] = ID[i][j];
}
} rep(i, , n) {
rep(j, , m) {
if (s[i][j] == '#')
continue; const int& id = ID[i][j];
sz[id] = ;
Move[id][] = id;
rep(k, , ) {
int x = i + dir[k][];
int y = j + dir[k][];
if (judge(x, y))
continue; Move[id][sz[id]++] = ID[x][y];
}
}
} st = ed = ; for (char ch='a'; ch<='z'; ++ch) {
iter = ptb.find(ch);
if (iter != ptb.end()) {
st = (st << ) | iter->sec;
iter = ptb.find(ch-'a'+'A');
#ifndef ONLINE_JUDGE
assert(iter != ptb.end());
#endif
ed = (ed << ) | iter->sec;
}
}
} int bfs1(const int qid) {
int cst, nst;
int qsz = SZ(Q[qid]); while (qsz--) {
cst = Q[qid].front();
Q[qid].pop(); int step = visit[qid][][][cst] + ;
rep(i, , sz[cst]) {
nst = Move[cst][i];
if (visit[qid][][][nst] == -) {
if (visit[qid^][][][nst] >= )
return step + visit[qid^][][][nst];
visit[qid][][][nst] = step;
Q[qid].push(nst);
}
}
} return -;
} int bfs2(const int qid) {
int cst[], nst[], tmp;
int qsz = SZ(Q[qid]); while (qsz--) {
st = Q[qid].front();
Q[qid].pop(); per(i, , ) {
cst[i] = st & 0xff;
st >>= ;
} int step = visit[qid][][cst[]][cst[]] + ; rep(i, , sz[cst[]]) {
nst[] = Move[cst[]][i];
rep(j, , sz[cst[]]) {
nst[] = Move[cst[]][j];
if (nst[]==nst[] || (nst[]==cst[]&&nst[]==cst[]))
continue; tmp = nst[]<< | nst[];
if (visit[qid][][nst[]][nst[]] == -) {
if (visit[qid^][][nst[]][nst[]] != -)
return step + visit[qid^][][nst[]][nst[]];
visit[qid][][nst[]][nst[]] = step;
Q[qid].push(tmp);
}
}
}
} return -;
} inline bool check(int *nst, int *cst) {
return (nst[]==cst[] && nst[]==cst[]) || (nst[]==cst[] && nst[]==cst[]) || (nst[]==cst[] && nst[]==cst[]);
} int bfs3(const int qid) {
int cst[], nst[], tmp;
int qsz = SZ(Q[qid]); while (qsz--) {
st = Q[qid].front();
Q[qid].pop(); per(i, , ) {
cst[i] = st & 0xff;
st >>= ;
} int step = visit[qid][cst[]][cst[]][cst[]] + ; rep(i, , sz[cst[]]) {
nst[] = Move[cst[]][i];
rep(j, , sz[cst[]]) {
nst[] = Move[cst[]][j];
rep(k, , sz[cst[]]) {
nst[] = Move[cst[]][k];
if (nst[]==nst[] || nst[]==nst[] || nst[]==nst[] || check(cst, nst))
continue; tmp = (nst[]<<) | (nst[]<<) | (nst[]); if (visit[qid][nst[]][nst[]][nst[]] == -) {
if (visit[qid^][nst[]][nst[]][nst[]] != -)
return step + visit[qid^][nst[]][nst[]][nst[]];
visit[qid][nst[]][nst[]][nst[]] = step;
Q[qid].push(tmp);
}
}
}
}
} return -;
} #define bibfs(n)\
int bibfs##n() {\
int tmp; \
\
while (!Q[].empty() || !Q[].empty()) {\
tmp = bfs##n();\
if (tmp >= ) return tmp;\
tmp = bfs##n();\
if (tmp >= ) return tmp;\
}\
\
return -;\
} #define callbibfs(n) bibfs##n() bibfs()
bibfs()
bibfs() void solve() {
init();
int ans; memset(visit, -, sizeof(visit));
rep(i, , )
while (!Q[i].empty()) Q[i].pop();
visit[][st>>][(st>>)&0xff][st&0xff] = ;
visit[][ed>>][(ed>>)&0xff][ed&0xff] = ;
Q[].push(st);
Q[].push(ed); if (gn == ) {
ans = callbibfs();
} else if (gn == ) {
ans = callbibfs();
} else {
ans = callbibfs();
} printf("%d\n", ans);
} int main() {
ios::sync_with_stdio(false);
#ifndef ONLINE_JUDGE
freopen("data.in", "r", stdin);
freopen("data.out", "w", stdout);
#endif while (scanf("%d%d%d%*c",&m,&n,&gn)!=EOF && (n||m||gn)) {
rep(i, , n)
gets(s[i]);
solve();
} #ifndef ONLINE_JUDGE
printf("time = %d.\n", (int)clock());
#endif return ;
}