Hineven's Blog

【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());
    }
}
posted @ 2016-07-14 19:54  Hineven  阅读(237)  评论(0编辑  收藏  举报