UVa1601-The Morning after Halloween

          题意:给出一个最大为16×16的迷宫图和至多3个ghost的起始位置和目标位置,求最少经过几轮移动可以使三个ghost都到达目标位置。每轮移动中,每个ghost可以走一步,也可以原地不动,需要注意的是任意两个ghost不能在相同的位置,因此也不能出现任意两个ghost对穿,也就是原来是ab,移动之后是ba。每个迷宫图'#'表示墙,' '表示空地,小写字母表示ghost的起始位置,大写字母表示对应ghost的目标位置,比如'a'应该走到'A'。保证任意2×2的空间内都有一个'#'。 

  分析:以当前3个小写字母的位置为状态,则问题转化为图的最短路问题。状态总数为256^3,每次转移需要5^3枚举每个小写字母下一步的走法,但是容易超时。

//暴力
#include<cstdio>
#include<cstring>
#include<cctype>
#include<queue>
using namespace std;
const int maxs = 20;
const int maxn = 150;
const int dx[]={1,-1,0,0,0};
const int dy[]={0,0,1,-1,0};
int ID(int a, int b, int c) {
  return (a<<16)|(b<<8)|c;//将状态压缩成一个整数,方便储存
}
int s[3], t[3];//起始状态,目标状态
int A[256];//记录是否为添加的鬼
bool conflict(int a, int b, int a2, int b2) {
  return a2 == b2 || (a2 == b && b2 == a);//判断是否冲突
}
int d[maxn][maxn][maxn];//步数
char maze[20][20];//原图
int cnt, x[maxn], y[maxn], id[maxs][maxs];//图与ID号的映射
int w, h, n;
int bfs() {
    queue<int> q;
    memset(d, -1, sizeof(d));
    q.push(ID(s[0], s[1], s[2]));
    d[s[0]][s[1]][s[2]] = 0;
    while(!q.empty()) {
      int u = q.front();
      q.pop();
      int a = (u>>16)&0xff, b = (u>>8)&0xff, c = u&0xff;
      if(a == t[0] && b == t[1] && c == t[2]){
        return d[a][b][c];
      }
      for(int i = 0; i < 5; i++) {//第一个鬼
        if(x[a]+dx[i]<0||x[a]+dx[i]>=h||y[a]+dy[i]<0||y[a]+dy[i]>=w){//出界
            continue;
        }
        char a3=maze[x[a]+dx[i]][y[a]+dy[i]];
        if(a3=='#'){//有墙
            continue;
        }
        int a2=id[x[a]+dx[i]][y[a]+dy[i]];//下一步的ID号
        for(int j = 0; j < 5; j++) {//第二个鬼
          int b2;
          if(A[b]==1){//判断是否为添加的鬼
            j=5;
            b2=b;
          }
          else{
          if(x[b]+dx[j]<0||x[b]+dx[j]>=h||y[b]+dy[j]<0||y[b]+dy[j]>=w){
                continue;
          }
          char b3=maze[x[b]+dx[j]][y[b]+dy[j]];
          if(b3=='#'){
                continue;
          }
          b2=id[x[b]+dx[j]][y[b]+dy[j]];
          }
          if(conflict(a, b, a2, b2)){//判断是否冲突
            continue;
          }
          for(int k = 0; k < 5; k++) {//第三个鬼
            int c2;
            if(A[c]==1){
                k=5;
                c2=c;
            }
            else{
            if(x[c]+dx[k]<0||x[c]+dx[k]>=h||y[c]+dy[k]<0||y[c]+dy[k]>=w){
                continue;
            }
            char c3=maze[x[c]+dx[k]][y[c]+dy[k]];
            printf("%c\n",c3);
            if(c3=='#'){
                continue;
            }
            c2=id[x[c]+dx[k]][y[c]+dy[k]];
            }
            if(conflict(a, c, a2, c2)){
                continue;
            }
            if(conflict(b, c, b2, c2)){
                continue;
            }
            if(d[a2][b2][c2] != -1){//检查是否已经访问过
                continue;
            }
            d[a2][b2][c2] = d[a][b][c]+1;
            q.push(ID(a2, b2, c2));
          }
        }
      }
    }
  return -1;
}
int main() {
  while(scanf("%d%d%d\n", &w, &h, &n) == 3 && n) {
    memset(A,0,sizeof(A));
    for(int i = 0; i < h; i++){
        fgets(maze[i], 20, stdin);
    }
    cnt = 0;
    //将空地编上ID号
    for(int i = 0; i < h; i++)
      for(int j = 0; j < w; j++)
        if(maze[i][j] != '#') {
          x[cnt] = i; y[cnt] = j;
          id[i][j] = cnt;
          if(islower(maze[i][j])){
            s[maze[i][j] - 'a'] = cnt;
          }
          else if(isupper(maze[i][j])){
            t[maze[i][j] - 'A'] = cnt;
          }
          cnt++;
        }
    //不足三个鬼时添加鬼,方便统一处理
    if(n <= 2){
        A[cnt]=1;
        s[2] = t[2] = cnt++;


    }
    if(n <= 1){
        A[cnt]=1;
        s[1] = t[1] = cnt++;
    }
    printf("%d\n",bfs());
  }
  return 0;
}


由条件“任何一个2×2子网格中至少有一个障碍格”暗示着很多格子都是障碍,并且大部分空地都和障碍相邻,因此不是所有4个方向都能移动,因此可以把所有空格提出来建一个图。这里将原图中的空白点映射为编号,再将三个点的编号合一。预处理可以把每个空白格子的5个方向的下个格子的编号算出来。这样就可以AC了。

//暴力+优化,单向BFS
#include<cstdio>
#include<cstring>
#include<cctype>
#include<queue>
using namespace std;
const int dx[]={1,-1,0,0,0};
const int dy[]={0,0,1,-1,0};
int s[3],t[3];//起始状态,目标状态
int deg[256], G[256][5];
int ID(int a,int b,int c)
{
    return (a<<16)|(b<<8)|c;//将状态压缩成一个整数,方便储存
}
bool conflict(int a, int b, int a2, int b2)
{
  return a2 == b2 || (a2 == b && b2 == a);//判断是否冲突
}
int d[256][256][256];//步数
int bfs() {
    queue<int> q;
    memset(d, -1, sizeof(d));
    q.push(ID(s[0], s[1], s[2]));
    d[s[0]][s[1]][s[2]] = 0;
    while(!q.empty()){
      int u = q.front();
      q.pop();
      int a = (u>>16)&0xff, b = (u>>8)&0xff, c = u&0xff;
      if(a == t[0] && b == t[1] && c == t[2]){
        return d[a][b][c];
      }
      for(int i = 0; i < deg[a]; i++) {//第一个鬼
        int a2 = G[a][i];//下一步的ID号
        for(int j = 0; j < deg[b]; j++) {//第二个鬼
          int b2 = G[b][j];
          if(conflict(a, b, a2, b2)){//判断是否冲突
            continue;
          }
          for(int k = 0; k < deg[c]; k++) {//第三个鬼
            int c2 = G[c][k];
            if(conflict(a, c, a2, c2)){
                continue;
            }
            if(conflict(b, c, b2, c2)){
                continue;
            }
            if(d[a2][b2][c2] != -1){//检查是否已经访问过
                continue;
            }
            d[a2][b2][c2] = d[a][b][c]+1;
            q.push(ID(a2, b2, c2));
          }
        }
      }
    }
  return -1;
}
int main()
{
    int w,h,n;
    while(scanf("%d%d%d\n",&w,&h,&n)==3&&n!=0){
        char maze[20][20];
        for(int i=0;i<h;i++){
            fgets(maze[i],20,stdin);
        }
        int cnt=0,x[256],y[256],id[20][20];
        //将空地编上ID号
        for(int i=0;i<h;i++){
            for(int j=0;j<w;j++){
                if(maze[i][j]!='#'){
                    x[cnt]=i;
                    y[cnt]=j;
                    id[i][j]=cnt;
                    if(islower(maze[i][j])){
                        s[maze[i][j] - 'a'] = cnt;
                    }
                    else if(isupper(maze[i][j])){
                        t[maze[i][j] - 'A'] = cnt;
                    }
                    cnt++;
                }
            }
        }
        //建图
        for(int i=0;i<cnt;i++){
            deg[i]=0;
            for(int j=0;j<5;j++){
                int nx = x[i]+dx[j], ny = y[i]+dy[j];
                if(maze[nx][ny]!='#'){
                    G[i][deg[i]++]=id[nx][ny];
                }
            }
        }
        //不足三个鬼时添加鬼,方便统一处理
        if(n<=2){
            deg[cnt]=1;
            G[cnt][0]=cnt;
            s[2]=t[2]=cnt++;
        }
        if(n<=1){
            deg[cnt]=1;
            G[cnt][0]=cnt;
            s[1]=t[1]=cnt++;
        }
        printf("%d\n",bfs());
    }
    return 0;
}
当然,也可以用 双向BFS。 双向BFS的好处,就是避免了单向BFS步数太多产生的组合性爆炸的情况,从而降低时间复杂度。
//双向BFS
#include<cstdio>
#include<cstring>
#include<cctype>
#include<queue>
#include <iostream>
using namespace std;
const int N    = 20;
const int maxn = 200;
const int dx[] = {1,0,-1,0,0};
const int dy[] = {0,-1,0,1,0};
int n,m,k;
char maze[N][N];
int x[maxn],y[maxn],cnt,G[maxn][5],deg[maxn],id[maxn][maxn];
int s[3],t[3];
int d[maxn][maxn][maxn],bd[maxn][maxn][maxn];
int ID(int a,int b,int c)
{
    return (a<<16)|(b<<8)|c;//将状态压缩成一个整数,方便储存
}
void Memset(){
    memset(d,-1,sizeof(d));
    memset(bd,-1,sizeof(bd));
}
bool conflict(int a, int b, int a2, int b2) {
  return a2 == b2 || (a2 == b && b2 == a);//判断是否冲突
}
queue<int> q;
bool bfs()//正向BFS
{
    int value;
    int uu=q.front();
    int aa=(uu>>16)&0xff,bb=(uu>>8)&0xff,cc=uu&0xff;
    value=d[aa][bb][cc];
    while(!q.empty())
    {
        int u=q.front();
        int a=(u>>16)&0xff,b=(u>>8)&0xff,c=u&0xff;
        if(d[a][b][c]!=value) return false;
        if(bd[a][b][c]!=-1) return true;
        for(int i=0; i<deg[a]; i++)
        {
            int na=G[a][i];
            for(int j=0; j<deg[b]; j++)
            {
                int nb=G[b][j];
                for(int k=0; k<deg[c]; k++)
                {
                    int nc=G[c][k];
                    if(conflict(a,b,na,nb)) continue;
                    if(conflict(a,c,na,nc)) continue;
                    if(conflict(b,c,nb,nc)) continue;
                    if(d[na][nb][nc]==-1)
                    {
                        d[na][nb][nc]=d[a][b][c]+1;
                        q.push(ID(na,nb,nc));
                    }
                }
            }
        }
        q.pop();
    }
    return false;
}
queue<int> p;
int back_bfs(){//反向BFS
    int value;
    int uu=p.front();
    int aa=(uu>>16)&0xff,bb=(uu>>8)&0xff,cc=uu&0xff;
    value=bd[aa][bb][cc];
    while(!p.empty())
    {
        int u=p.front();
        int a=(u>>16)&0xff,b=(u>>8)&0xff,c=u&0xff;
        if(bd[a][b][c]!=value) return false;
        if(d[a][b][c]!=-1) return true;
        for(int i=0; i<deg[a]; i++)
        {
            int na=G[a][i];
            for(int j=0; j<deg[b]; j++)
            {
                int nb=G[b][j];
                for(int k=0; k<deg[c]; k++)
                {
                    int nc=G[c][k];
                    if(conflict(a,b,na,nb)) continue;
                    if(conflict(a,c,na,nc)) continue;
                    if(conflict(b,c,nb,nc)) continue;
                    if(bd[na][nb][nc]==-1)
                    {
                        bd[na][nb][nc]=bd[a][b][c]+1;
                        p.push(ID(na,nb,nc));
                    }
                }
            }
        }
        p.pop();
    }
    return false;
}
int BFS(){
while(!q.empty()) q.pop();
while(!p.empty()) p.pop();
q.push(ID(s[0],s[1],s[2]));
p.push(ID(t[0],t[1],t[2]));
Memset();
d[s[0]][s[1]][s[2]]=0; bd[t[0]][t[1]][t[2]]=0;
int step=0,ok=0;
while(!p.empty()&&!q.empty()){//正向BFS与反向BFS交替进行,同时统计步数
        if(bfs()){
            ok=1;
            break;
        }
        else step++;
        if(back_bfs()){
            ok=1;
            break;
        }
        else step++;
}
return ok ? step:-1;
}
int main()
{
    int w,h,n;
    while(scanf("%d%d%d\n",&w,&h,&n)==3&&n!=0){
        char maze[20][20];
        for(int i=0;i<h;i++){
            fgets(maze[i],20,stdin);
        }
        int cnt=0,x[256],y[256],id[20][20];
        //将空地编上ID号
        for(int i=0;i<h;i++){
            for(int j=0;j<w;j++){
                if(maze[i][j]!='#'){
                    x[cnt]=i;
                    y[cnt]=j;
                    id[i][j]=cnt;
                    if(islower(maze[i][j])){
                        s[maze[i][j] - 'a'] = cnt;
                    }
                    else if(isupper(maze[i][j])){
                        t[maze[i][j] - 'A'] = cnt;
                    }
                    cnt++;
                }
            }
        }
        //建图
        for(int i=0;i<cnt;i++){
            deg[i]=0;
            for(int j=0;j<5;j++){
                int nx = x[i]+dx[j], ny = y[i]+dy[j];
                if(maze[nx][ny]!='#'){
                    G[i][deg[i]++]=id[nx][ny];
                }
            }
        }
        //不足三个鬼时添加鬼,方便统一处理
        if(n<=2){
            deg[cnt]=1;
            G[cnt][0]=cnt;
            s[2]=t[2]=cnt++;
        }
        if(n<=1){
            deg[cnt]=1;
            G[cnt][0]=cnt;
            s[1]=t[1]=cnt++;
        }
        printf("%d\n",BFS());
    }
    return 0;
}




  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值