Problem UVA1601-The Morning after Halloween
Accept: 289 Submit: 3136
Time Limit: 12000 mSec
Problem Description
Input
Output
For each dataset in the input, one line containing the smallest number of steps to restore ghosts into the positions where they are supposed to be should be output. An output line should not contain extra characters such as spaces.
Sample Input
Sample Ouput
7
36
77
题解:双向BFS经典题目。
抛开双向BFS,这个题的预处理也很值得学习。节点个数太多16*16=256,并且有三个鬼,所以状态总数几乎是256^3,不管是时间还是空间,肯定都要炸,所以就要把状态算的精确一些,省去一些不可能的情况,首先最外层都是'#',这样这个图就变成了最大14*14 = 196,还是有点大,注意题目条件,每2*2中至少有一个障碍,所以这196个点至多有75%是空格,196*0.75 = 147。这就差不多了,由于后面可能有加点的操作,不过最多加2个点,也就是说总数不会超过150,这个数字就比较理想了,时间上和空间上都不错。判重的问题,刚才分析过了,每个鬼能够出现的格子数目不超过150,八位二进制数就能表示,因此三个就可以用24位二进制数表示,和int还有一段距离,所以转换成一个int型整数判重是再好不过的了。将所有空格连接起来用的是类似邻接表的操作,不过lrj没有用vector,可能还是效率原因吧。
之后就是双向BFS的模板了,两个队列,分别入队出发点和终点,vis数组肯定是三维的,只是原来的0、1变成了现在的0、1、2,因为要区分是在哪一边扩展的过程中遍历到的这个点。当一个点在两个队列中都出现了,就说明找到了,返回dis的和,别忘了+1.还有一个需要注意的是双向BFS每次扩展一层而不是扩展一个节点。原因参考下面的链接:
http://www.cppblog.com/Yuan/archive/2011/02/23/140553.aspx
先来一个单向BFS,680ms
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <queue>
using namespace std; const int maxs = ;
const int maxn = ; int n,m,cnt,sum;
int deg[maxn],G[maxn][maxn];
int x[maxn],y[maxn],id[maxn][maxn];
int d[maxn][maxn][maxn];
int s[],t[];
int dx[] = {,,,-,};
int dy[] = {,,-,,}; int find_ID(int a,int b,int c){
return (a<<)|(b<<)|c;
} bool conflict(int a,int b,int a2,int b2){
if((a2==b && a==b2) || a2==b2) return true;
return false;
} int bfs(){
queue<int> que;
d[s[]][s[]][s[]] = ;
que.push(find_ID(s[],s[],s[]));
while(!que.empty()){
int top = que.front();que.pop();
int a = (top>>)&0xff,b = (top>>)&0xff,c = (top&0xff);
if(a==t[] && b==t[] && c==t[]) return d[a][b][c];
for(int i = ;i < deg[a];i++){
int a2 = G[a][i];
for(int j = ;j < deg[b];j++){
int b2 = G[b][j];
if(conflict(a,b,a2,b2)) continue;
for(int k = ;k < deg[c];k++){
int c2 = G[c][k];
if(d[a2][b2][c2] != -) continue;
if(conflict(a,c,a2,c2)) continue;
if(conflict(b,c,b2,c2)) continue;
d[a2][b2][c2] = d[a][b][c]+;
que.push(find_ID(a2,b2,c2));
}
}
}
}
return -;
} int main()
{
//freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);
while(~scanf("%d%d%d\n",&m,&n,&sum) && (m||n||sum)){
char gra[maxs][maxs];
cnt = ;
memset(d,-,sizeof(d));
for(int i = ;i < n;i++) fgets(gra[i],maxs,stdin); for(int i = ;i < n;i++){
for(int j = ;j < m;j++){
if(gra[i][j] != '#'){
x[cnt] = i,y[cnt] = j,id[i][j] = cnt;
if(islower(gra[i][j])) s[gra[i][j]-'a'] = cnt;
else if(isupper(gra[i][j])) t[gra[i][j]-'A'] = cnt;
cnt++;
}
}
} for(int i = ;i < cnt;i++){
int X = x[i],Y = y[i];
deg[i] = ;
for(int k = ;k < ;k++){
int xx = X+dx[k],yy = Y+dy[k];
if(gra[xx][yy] != '#') G[i][deg[i]++] = id[xx][yy];
}
} if(sum <= ){
s[] = t[] = G[cnt][] = cnt;
deg[cnt++] = ;
}
if(sum <= ){
s[] = t[] = G[cnt][] = cnt;
deg[cnt++] = ;
} printf("%d\n",bfs());
}
return ;
}
再来一个双向BFS,530ms
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <queue>
using namespace std; const int maxs = ;
const int maxn = ; int n,m,cnt,sum;
int deg[maxn],G[maxn][maxn];
int x[maxn],y[maxn],id[maxn][maxn];
int d[maxn][maxn][maxn];
int vis[maxn][maxn][maxn];
int s[],t[];
int dx[] = {,,,-,};
int dy[] = {,,-,,}; int find_ID(int a,int b,int c){
return (a<<)|(b<<)|c;
} bool conflict(int a,int b,int a2,int b2){
if((a2==b && a==b2) || a2==b2) return true;
return false;
} int bfs(){
queue<int> que,anti_que;
d[s[]][s[]][s[]] = ;
d[t[]][t[]][t[]] = ; que.push(find_ID(s[],s[],s[]));
anti_que.push(find_ID(t[],t[],t[])); vis[s[]][s[]][s[]] = ;
vis[t[]][t[]][t[]] = ; while(!que.empty() && !anti_que.empty()){
int num = que.size(),anti_num = anti_que.size();
while(num--){
int top = que.front();que.pop();
int a = (top>>)&0xff,b = (top>>)&0xff,c = (top&0xff); for(int i = ;i < deg[a];i++){
int a2 = G[a][i];
for(int j = ;j < deg[b];j++){
int b2 = G[b][j];
if(conflict(a,b,a2,b2)) continue;
for(int k = ;k < deg[c];k++){
int c2 = G[c][k];
if(conflict(a,c,a2,c2)) continue;
if(conflict(b,c,b2,c2)) continue; if(vis[a2][b2][c2] == ){
d[a2][b2][c2] = d[a][b][c]+;
vis[a2][b2][c2] = ;
que.push(find_ID(a2,b2,c2));
}
else if(vis[a2][b2][c2] == ){
return d[a][b][c]+d[a2][b2][c2]+;
}
}
}
}
} while(anti_num--){
int top = anti_que.front();anti_que.pop();
int a = (top>>)&0xff,b = (top>>)&0xff,c = (top&0xff); for(int i = ;i < deg[a];i++){
int a2 = G[a][i];
for(int j = ;j < deg[b];j++){
int b2 = G[b][j];
if(conflict(a,b,a2,b2)) continue;
for(int k = ;k < deg[c];k++){
int c2 = G[c][k];
if(conflict(a,c,a2,c2)) continue;
if(conflict(b,c,b2,c2)) continue; if(vis[a2][b2][c2] == ){
d[a2][b2][c2] = d[a][b][c]+;
vis[a2][b2][c2] = ;
anti_que.push(find_ID(a2,b2,c2));
}
else if(vis[a2][b2][c2] == ){
return d[a][b][c]+d[a2][b2][c2]+;
}
}
}
}
}
}
return -;
} int main()
{
//freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);
while(~scanf("%d%d%d\n",&m,&n,&sum) && (m||n||sum)){
char gra[maxs][maxs];
cnt = ;
memset(d,-,sizeof(d));
memset(vis,,sizeof(vis));
for(int i = ;i < n;i++) fgets(gra[i],maxs,stdin); for(int i = ;i < n;i++){
for(int j = ;j < m;j++){
if(gra[i][j] != '#'){
x[cnt] = i,y[cnt] = j,id[i][j] = cnt;
if(islower(gra[i][j])) s[gra[i][j]-'a'] = cnt;
else if(isupper(gra[i][j])) t[gra[i][j]-'A'] = cnt;
cnt++;
}
}
} for(int i = ;i < cnt;i++){
int X = x[i],Y = y[i];
deg[i] = ;
for(int k = ;k < ;k++){
int xx = X+dx[k],yy = Y+dy[k];
if(gra[xx][yy] != '#') G[i][deg[i]++] = id[xx][yy];
}
} if(sum <= ){
s[] = t[] = G[cnt][] = cnt;
deg[cnt++] = ;
}
if(sum <= ){
s[] = t[] = G[cnt][] = cnt;
deg[cnt++] = ;
} printf("%d\n",bfs());
}
return ;
}