uva 1601(万圣节的早晨,经典搜索)

题目大意:

    有n只小鬼(n <= 3) 分别移动的对应的大写字母网格中, 每次可以有多只小鬼同时移动,移动方向为上下左右或者选择不移动。 注意的是,移动后两任意两只小鬼不能占据同一个位置,也不能在一步之内交换位置;

题目分析:

   这道题做到我好累,最后还是把刘汝佳的代码搬过来吧。

  直接枚举需要付出的时间代价很大,一次枚举最多需要n= 5*5*5次,状态的总数为 m = 256*256*256; 时间复杂度为O(n*m);  会超时,需要优化。 因为障碍很多,许多枚举都是不必要的,可以自己建立一张地图;


#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <cctype>

using namespace std;

const int maxn = 200;
int w, h, n, cnt;
char maze[20][20];

int s[3], t[3]; // starting/ending position of each ghost
int deg[maxn], G[maxn][5]; // target cells for each move (including "no move")
int dir[5][2] = {{0, 0}, {-1, 0}, {1, 0}, {0, -1}, {0, 1}};

int readInput(){
    if(scanf("%d%d%d", &w, &h, &n) == EOF) return 0;
    if(!n) return 0;
    getchar();
    for(int i = 0; i < h; ++i)
        fgets(maze[i], 20, stdin);
    return 1;
}
int bfs();

int main()
{
    while(readInput()){
        // extract empty cells
        cnt = 0;
        int x[maxn], y[maxn], id[20][20];
        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++;
                }
            }

        // build a graph of empty cells
        for(int i = 0; i < cnt; ++i){
            deg[i] = 0;
            for(int d = 0; d < 5; ++d){
                int nx = x[i]+dir[d][0], ny = y[i]+dir[d][1];
                if(maze[nx][ny] != '#'){
                    G[i][deg[i]++] = id[nx][ny];
                }
            }
        }
        // add fakes nodes so that in each case we have 3 ghosts. this makes the code shorter
        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++; }
        int ans = bfs();
        printf("%d\n", ans);
    }
    return 0;
}

int d[maxn][maxn][maxn]; // distance from starting state

int ID(int a, int b, int c){
    return (a<<16) | (b<<8) | c;
}
bool conflict(int a, int b, int a2, int b2){
    if(a2 == b2) return true;
    else if(a2 == b && b2 == a) return true;
    return false;
}

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];
            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;
}

 

     

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值