【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());
    }
}

转载于:https://www.cnblogs.com/Hineven/p/5843569.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这是一道比较经典的计数问题。题目描述如下: 给定一个 $n \times n$ 的网格图,其中一些格子被标记为障碍。一个连通块是指一些被标记为障碍的格子的集合,满足这些格子在网格图中连通。一个格子是连通的当且仅当它与另一个被标记为障碍的格子在网格图中有公共边。 现在,你需要计算在这个网格图中,有多少个不同的连通块,满足这个连通块的大小(即包含的格子数)恰好为 $k$。 这是一道比较经典的计数问题,一般可以通过计算生成函数的方法来解决。具体来说,我们可以定义一个生成函数 $F(x)$,其中 $[x^k]F(x)$ 表示大小为 $k$ 的连通块的个数。那么,我们可以考虑如何计算这个生成函数。 对于一个大小为 $k$ 的连通块,我们可以考虑它的形状。具体来说,我们可以考虑以该连通块的最左边、最上边的格子为起点,从上到下、从左到右遍历该连通块,把每个格子在该连通块中的相对位置记录下来。由于该连通块的大小为 $k$,因此这些相对位置一定是 $(x,y) \in [0,n-1]^2$ 中的 $k$ 个不同点。 现在,我们需要考虑如何计算这些点对应的连通块是否合法。具体来说,我们可以考虑从左到右、从上到下依次处理这些点,对于每个点 $(x,y)$,我们需要考虑它是否能够与左边的点和上边的点连通。具体来说,如果 $(x-1,y)$ 和 $(x,y)$ 都在该连通块中且它们在网格图中有公共边,那么它们就是连通的;同样,如果 $(x,y-1)$ 和 $(x,y)$ 都在该连通块中且它们在网格图中有公共边,那么它们也是连通的。如果 $(x,y)$ 与左边和上边的点都不连通,那么说明这个点不属于该连通块。 考虑到每个点最多只有两个方向需要检查,因此时间复杂度为 $O(n^2 k)$。不过,我们可以使用类似于矩阵乘法的思想,将这个过程优化到 $O(k^3)$ 的时间复杂度。 具体来说,我们可以设 $f_{i,j,k}$ 表示状态 $(i,j)$ 所代表的点在连通块中,且连通块的大小为 $k$ 的方案数。显然,对于一个合法的 $(i,j,k)$,我们可以考虑 $(i-1,j,k-1)$ 和 $(i,j-1,k-1)$ 这两个状态,然后把点 $(i,j)$ 加入到它们所代表的连通块中。因此,我们可以设计一个 $O(k^3)$ 的 DP 状态转移,计算 $f_{i,j,k}$。 具体来说,我们可以考虑枚举连通块所包含的最右边和最下边的格子的坐标 $(x,y)$,然后计算 $f_{x,y,k}$。对于一个合法的 $(x,y,k)$,我们可以考虑将 $(x,y)$ 所代表的点加入到 $(x-1,y,k-1)$ 和 $(x,y-1,k-1)$ 所代表的连通块中。不过,这里需要注意一个细节:如果 $(x-1,y)$ 和 $(x,y)$ 在网格图中没有相邻边,那么它们不能算作连通的。因此,我们需要特判这个情况。 最终,$f_{n,n,k}$ 就是大小为 $k$ 的连通块的个数,时间复杂度为 $O(n^2 k + k^3)$。 参考代码:

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值