【POJ 1324】Holedox Moving A*寬搜

Holedox Moving

Time Limit:5000MS Memory Limit:65536KB

Description

During winter, the most hungry and severe time, Holedox sleeps in its lair. When spring comes, Holedox wakes up, moves to the exit of its lair, comes out, and begins its new life.
Holedox is a special snake, but its body is not very long. Its lair is like a maze and can be imagined as a rectangle with n*m squares. Each square is either a stone or a vacant place, and only vacant places allow Holedox to move in. Using ordered pair of row and column number of the lair, the square of exit located at (1,1).

Holedox’s body, whose length is L, can be represented block by block. And let B1(r1,c1) B2(r2,c2) .. BL(rL,cL) denote its L length body, where Bi is adjacent to Bi+1 in the lair for 1 <= i <=L-1, and B1 is its head, BL is its tail.

To move in the lair, Holedox chooses an adjacent vacant square of its head, which is neither a stone nor occupied by its body. Then it moves the head into the vacant square, and at the same time, each other block of its body is moved into the square occupied by the corresponding previous block.

For example, in the Figure 2, at the beginning the body of Holedox can be represented as B1(4,1) B2(4,2) B3(3,2)B4(3,1). During the next step, observing that B1’(5,1) is the only square that the head can be moved into, Holedox moves its head into B1’(5,1), then moves B2 into B1, B3 into B2, and B4 into B3. Thus after one step, the body of Holedox locates in B1(5,1)B2(4,1)B3(4,2) B4(3,2) (see the Figure 3).

Given the map of the lair and the original location of each block of Holedox’s body, your task is to write a program to tell the minimal number of steps that Holedox has to take to move its head to reach the square of exit (1,1).

Input

The input consists of several test cases. The first line of each case contains three integers n, m (1<=n, m<=20) and L (2<=L<=8), representing the number of rows in the lair, the number of columns in the lair and the body length of Holedox, respectively. The next L lines contain a pair of row and column number each, indicating the original position of each block of Holedox’s body, from B1(r1,c1) to BL(rL,cL) orderly, where 1<=ri<=n, and 1<=ci<=m,1<=i<=L. The next line contains an integer K, representing the number of squares of stones in the lair. The following K lines contain a pair of row and column number each, indicating the location of each square of stone. Then a blank line follows to separate the cases.

The input is terminated by a line with three zeros.

Note: Bi is always adjacent to Bi+1 (1<=i<=L-1) and exit square (1,1) will never be a stone.

Output

For each test case output one line containing the test case number followed by the minimal number of steps Holedox has to take. “-1” means no solution for that case.

Sample Input

5 6 4
4 1
4 2
3 2
3 1
3
2 3
3 3
3 4

4 4 4
2 3
1 3
1 4
2 4
4

2 1
2 2
3 4
4 2

0 0 0

Sample Output

Case 1: 9
Case 2: -1
Hint
In the above sample case, the head of Holedox can follows (4,1)->(5,1)->(5,2)->(5,3)->(4,3)->(4,2)->(4,1)->(3,1)->(2,1)->(1,1) to reach the square of exit with minimal number of step, which is nine.

題解

這是一道搜索題(還用你說

本題難點爲狀壓蛇的座標
暴力狀壓會2^400次方直接爆掉
考慮通過蛇之前的行徑來狀壓
設蛇頭座標爲x,y
蛇在之前L次行動中分別選擇了第ai個移動方式(ai<=4)
由於L<=8所以可以用一個7位的四進制整數表示之前的移動方式
總狀態數爲 202047
可以用一個三維數組vis[x][y][S]保存下來
就可以解決去重的問題啦

這道題直接暴力寬搜也可以過
用A*會更快一些。
以每個點到終點的距離作爲A*算法估值
這個距離可以一開始bfs出來
速度快很多

下面貼代碼

#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
int n,m,L;
int maxs,start;
const int mov[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
bool vis[22][22][18000];
int dis[22][22];
int a1[1000][2],a2[1000][2];
bool g[22][22],now[22][22];
struct St{
    int x,y,s,c;
    St(){x=y=s=c=0;}
    St(int a,int b,int cc,int d)
    {x=a,y=b,s=cc,c=d;}
};
bool operator<(const St&a,const St&b)
{return dis[a.x][a.y]+a.c>dis[b.x][b.y]+b.c;}

void init(){
    memset(vis,0,sizeof vis);
    memset(g,0,sizeof g);
    memset(dis,0,sizeof dis);
    maxs=(1<<((L-1)*2))-1;
    start=0;
}

int buf[10][2];
void read(){
    for(int i=1;i<=L;i++)
        scanf("%d%d",&buf[i][0],&buf[i][1]);
    for(int i=L-1;i>=1;i--){
        start<<=2;
        if(buf[i][0]==buf[i+1][0]){
            if(buf[i][1]<buf[i+1][1])//up
                start+=3;
            else start+=2;
        }else if(buf[i][0]<buf[i+1][0])//left
                start+=1;
    }
    int k,ag1,ag2;
    scanf("%d",&k);
    for(int i=1;i<=k;i++){
        scanf("%d%d",&ag1,&ag2);
        g[ag1][ag2]=true;
    }
}
void initivebfs(){
    int tot1=2,tot2=1;
    int level=1;
    a1[1][0]=a1[1][1]=1;
    dis[1][1]=level++;
    while(tot1!=1){
        tot2=1;
        for(int i=1;i<tot1;i++)
            for(int k=0;k<4;k++){
                int a=a1[i][0]+mov[k][0],b=a1[i][1]+mov[k][1];
                if(a>0&&a<=n&&b>0&&b<=m&&!g[a][b]&&!dis[a][b])
                    dis[a][b]=level,a2[tot2][0]=a,a2[tot2++][1]=b;
            }
        for(int i=1;i<tot2;i++)
            a1[i][0]=a2[i][0],a1[i][1]=a2[i][1];
        tot1=tot2;
        level++;
    }
}
void extend(int x,int y,int S){
    memset(now,0,sizeof now);
    now[x][y]=true;
    for(int i=1;i<L;i++){
        now[x-=mov[S&3][0]][y-=mov[S&3][1]]=true;
        S>>=2;
    }
}
int Astar(){
    priority_queue<St> que;
    que.push(St(buf[1][0],buf[1][1],start,0));
    while(!que.empty()){
        St hd=que.top();
        if(hd.x==1&&hd.y==1)return hd.c;
        que.pop();
        extend(hd.x,hd.y,hd.s);
        for(int i=0;i<4;i++){
            int a=hd.x+mov[i][0],b=hd.y+mov[i][1];
            if(a<=0||a>n||b<=0||b>m||g[a][b]||now[a][b])continue;
            int s=((hd.s<<2)+i)&maxs;
            if(vis[a][b][s])continue;
            vis[a][b][s]=true;
            que.push(St(a,b,s,hd.c+1));
        }
    }
    return -1;
}
int main(){
    int tc=0;
    while(~scanf("%d%d%d",&n,&m,&L)&&n&&m&&L){
        tc++;
        init();
        read();
        initivebfs();
        if(!dis[buf[1][0]][buf[1][1]]){
            printf("Case %d: -1\n",tc);
            continue;
        }
        printf("Case %d: %d\n",tc,Astar());
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章