poj1324 Holedox Moving

POJ 1324 Holedox Moving Name: Holedox Moving P_ID: POJ 1324 题目链接:http://poj.org/problem?id=1324题意描述: n*m(最大20)的迷宫,l长(不超过8)的贪吃蛇,蛇身可弯曲,出口在(1,1),不能走石头所在格子,求蛇身最少移动次数。题目分析: 题目本身是要考bfs最短路这个很明确,唯一不寻常的是在蛇移动过程中蛇身也在动。刚开始我认为这道题并没有什么 阅读详情
Holedox Moving
Time Limit: 5000MSMemory Limit: 65536K
Total Submissions: 11975Accepted: 2873

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

     这是一道BFS的题目,但由于此题中蛇可以移动,所以不能通过一个简单的二维标记数组来判重。如:

若通过简单的二维标记数组,则此样例无解,显然对于这个图存在最少移动次数9。

   然后我又想着用一个三维数组来标记,即在原先的二维数组的基础上加一个方向,若蛇头经过当前这个格子是来自于另一个方向的话,则入队。但这样做后发现还是存在bug,依旧是上个例子就通不过。

   紧接着又尝试用蛇的头和尾进行判重(即一个四维数组),感觉还行,而且测试数据都能过,但还是wrong了,仔细一想其实还是有问题。

   最后不得不记录整个蛇的状态了,但实在不知道如何压缩状态了。看了解题报告后才明白可以依次记下每一个节点相对前一个节点的位置,这样通过一个三维数组就可以判重了,而且内存也不会爆。

 

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

using namespace std;
struct st
{
    int step;
    int B[10][2];//蛇的位置
}w,v;
int dir[4][2]={{0,1},{0,-1},{1,0},{-1,0}};
int map[21][21];//0表示空地,1表示石头的位置
bool flag[21][21][16384];//用4进制压缩状态,最大为4^7-1
int n,m,L,K;

int location(int px,int py,int nx,int ny)//求两个节点的相对位置
{
    if(px==nx)
    {
        if(py>ny)
            return 0;
        else
            return 1;
    }
    else
    {
        if(px>nx)
            return 2;
        else
            return 3;
    }
}

int getstate(int B[][2])//压缩状态
{
    int s=0;
    for(int i=0;i<L-1;i++)
        s=s*4+location(B[i][0],B[i][1],B[i+1][0],B[i+1][1]);
    return s;
}

bool path(int x,int y,int B[][2])
{
    if(x<1||x>n||y<1||y>m||map[x][y]!=0)
        return false;
    for(int i=0;i<L;i++)
        if(x==B[i][0]&&y==B[i][1])
            return false;
    return true;
}

int bfs()
{
    if(w.B[0][0]==1&&w.B[0][1]==1)
        return 0;
    queue<st>q;
    q.push(w);
    while(!q.empty())
    {
        w=q.front();
        q.pop();
        for(int i=0;i<4;i++)
        {
            int x,y,state;
            x=w.B[0][0]+dir[i][0];
            y=w.B[0][1]+dir[i][1];
            if(x==1&&y==1)
                return w.step+1;
            if(!path(x,y,w.B))
                continue;
            v.step=w.step+1;
            for(int j=L-1;j>0;j--)
            {
                v.B[j][0]=w.B[j-1][0];
                v.B[j][1]=w.B[j-1][1];
            }
            v.B[0][0]=x;
            v.B[0][1]=y;
            state=getstate(v.B);
            if(flag[x][y][state])
                continue;
            flag[x][y][state]=true;
            q.push(v);
        }
    }
    return -1;
}

int main()
{
    int Case=1;
    while(scanf("%d%d%d",&n,&m,&L),n+m+L)
    {
        memset(map,0,sizeof(map));
        int mst=pow(4,L-1);
        for(int i=1;i<=n;i++)
            for(int j=1;j<=m;j++)
                for(int k=0;k<mst;k++)
                    flag[i][j][k]=false;
        for(int i=0;i<L;i++)
        {
            scanf("%d%d",&w.B[i][0],&w.B[i][1]);
        }
        scanf("%d",&K);
        for(int i=0;i<K;i++)
        {
            int row,col;
            scanf("%d%d",&row,&col);
            map[row][col]=1;
        }
        w.step=0;
        printf("Case %d: %d\n",Case++,bfs());
    }
    return 0;
}
/*
4 4 4
3 2
3 1
2 1
2 2
6
1 2
1 3
1 4
2 3
2 4
4 1
*/


 

 

        

POJ 1324Holedox Moving (状态BFS) Holedox Moving Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 15264   Accepted: 3686 Description During winter, the most hungry and severe time, Holedox sle 阅读详情

相关推荐

北大POJ 1324.cpp

北大POJ第1324题(C++)

POJ 1324 [Holedox Moving] 状态压缩BFS

题目链接:http://poj.org/problem?id=1324 题目大意:n*m网格里有蛇和障碍,蛇只能向空格处移动,不能撞到自己,问到(1,1)的最短步数,如无法到达输出-1. 关键思想:不能直接对蛇头进行BFS,因为蛇身对蛇头的决策也有影响,故BFS时应该保存蛇身状态,我们放在state里用位运算来做。因为根据蛇头和每一节延伸的方向(东南西北)我们可以还原出整条蛇。比较坑的是这道题...

weixin_30363981的博客 203

三极管开关电路在脉冲应用中的性能瓶颈分析

深入探讨三极管开关电路在高频脉冲场景下的响应延迟与功耗问题,揭示其性能限制的关键因素。结合实际应用,分析开关速度与信号完整性之间的矛盾,为优化三极管开关电路设计提供实用参考。

weixin_42602368的博客 587

POJ 1324 Holedox Moving 搜索

题目地址: http://poj.org/problem?id=1324 优先队列---A*的估价函数不能为蛇头到(1,1)的距离,这样会出错。 超时代码: #include #include #include #include #include #include #include #include #include #include #include

lamborghini1993 1128

AI:Coze、Dify、n8n:三款低代码工具如何选?

Coze、Dify、n8n:三款低代码工具如何选?

Kant2048的博客 2806

如何加载交易策略EA到MT4/MT5

1、打开MT4/MT5软件 2、首先,必须有一个EA,而且必须是mq4/ex4为扩展名。点击左上角“文件”—“打开数据文件夹”,找到安装路径 3、将EA的ex4或mq4文件放到MT4目录\MQL4\Experts里面(本文以名为Demo的EA为例) 4、如果有DLL库文件,复制到experts目录下的libraries子目录中 5、如果有tpl模板文件,复制到templates目录中 6、添加完成后重启MT4,点击允许EA运行图标 7、在导航中找到刚才安装的EA,拖拽到...

weixin_41364817的博客 7359

POJ 1324 状压搜索(4进制)

给出图中蛇的范围和不可到达的地方,求蛇头到(1,1)点的

622

HEC-HMS水文建模从入门到精通:手把手教你安装与配置(附常见问题解决)

本文详细指导HEC-HMS水文建模软件的安装与配置,重点解析了基于InstallShield Wizard的安装流程,并提供了针对常见错误代码(如-6003)的解决方案。旨在帮助水文、水利领域从业者快速搭建稳定的建模环境,为后续流域模拟与洪水预报奠定基础。

weixin_29213827的博客 496

poj 1324 Holedox Moving

poj 1324Holedox Moving 题目地址:http://poj.org/problem?id=1324 题意: 给出一个矩阵中,一条贪吃蛇,占据L长度的格子, 另外有些格子是石头, 不能通过, 请问蛇到达 (1,1)格子最短距离。 明显的BFS问题, 将每一个可以走的格子进入队列, 格子判断能否走?(给蛇身体标序号(蛇头l, 蛇尾1,其他置为0)...

weixin_30679823的博客 153

ArcGIS通过已知坐标点转面(通过excel文件,有工具可批量生成面要素)

通过使用ArcGIS中的工具,将坐标点XY数据转换成可视化的点线面矢量数据,更加方便快捷有效的获取对应信息

qq_41046162的博客 3万+

DDD系列 - 第6讲 仓库Repository - Mybatis篇

书接上回,之前通过一个关于拆解、微服务、面向对象的故事,向大家介绍了如何从微服务、面向对象一步步演进到DDD的,整个演进过程如下图:其中在领域层,我们定义了富血模型的领域模型,包括聚合、聚合根、实体、值对象等。聚合在代码层面中的表现形式即是一个包Package,起到了边界的作用,聚合内的对象间高内聚,而聚合间低耦合。聚合中放着实体、值对象、仓库等,实体相较于值对象的一个典型区别就是实体存在唯一标识,并且可通过该唯一标识查询到该实体并对其进行相关操作,值对象不存在唯一标识且通常作为实体。

罗小爬的技术宝书 1783

POJ-1324 Holedox Moving

Holedox Moving Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 13366   Accepted: 3171 Description During winter, the most hungry and severe time, Holedox sleep

JSure的代码库 1041

提示词工程中的上下文隔离

已经进入context的prompt是没有办法让大模型真正的忘记的,但是可以通过prompt技巧强约束模型的后续推理中“不再使用、不再提及、不再引用”某部分提示词,这其实是一种行为抑制(behavior suppression),并不是删除。

Criss@陈磊 234

Holedox Moving POJ - 1324

Holedox Moving POJ - 1324

weixin_29216119的博客 298

Autosar CAN 网络管理测试用例(3)——Repeat Message Timer 时间测试

Autosar CAN 网络管理测试用例(3)——Repeat Message Timer 时间测试

蚂蚁小兵 2463

OV9734 DATASHEET,完整规格书

OV9734完整规格书,包括电气参数,尺寸规格,寄存器配置表等

POJ 1324 Holedox Moving 贪食蛇

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.  Hol

just_water 2411

VB摄像头监控

该VB模块可对摄像头捕获的画面进行录像等,可在创建的窗口进行控制

【POJ 1324】Holedox Moving A*宽搜

A*宽搜 解题报告 Holedox MovingTime Limit:5000MS Memory Limit:65536KB DescriptionDuring winter, the most hungry and severe time, Holedox sleeps in its lair. When spring comes, Holedox wakes up, moves to the ex

Hineven的博客 OIer 651

蓝牙模块HM-10手册

蓝牙模块HM-10手册,该手册对蓝牙模块HM-10进行了详细的介绍和使用方法以及一些AT指令。

老版QQ原生表情gif动态

这是老版的QQ表情,不过全是动态的的,Windows照片查看器是不会动的,可以右击用浏览器打开查看,再说一遍是老版的,只是分享给宁愿用老版也要动态的人。

POJ 1324 Holedox Moving 位运算+BFS

POJ 1324 Holedox Moving题目描述:  题目链接:POJ 1324 Holedox Moving题目大意:  给定贪吃蛇的初始位置和洞穴中的石头的位置,问这个贪吃蛇到达洞口的最短时间。如果不能到达输出−1-1。解题思路:  这个题的难点在于对贪吃蛇状态的记录以及对蛇的状态的判重。由题知蛇长最大为7因此,我们可以用一个整数(int)的二进制来表示蛇的形态。每一格相对于其上一格的

Running Boy的AC Dream 503

Holedox Moving POJ - 1324

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...

sinker 316

poj 1324 Holedox Moving

Holedox Moving Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 12041   Accepted: 2884 Description During winter, the most hungry and severe time, Holedox sleep

习惯与规则决定命运 1226

POJ 1324 Holedox Moving 状态压缩+A* -

题目地址:http://poj.org/problem?id=1324 写了一天了 ,一开始想表示状态用头和尾位置,但很快写出来就WR 然后就用根据贪吃蛇的节点与上一个节点位置关系保存状态,一共有4个状态,上下左右,0~3表示,正好二进制占两个位 最多有14个状态,所以int就可以保存 然后写出来后MLE然后TLE然后MLE然后TLE然后WR.... 不容易啊woc... 最后发现vec

BRCOCOLI的博客 427

poj1324 Holedox Moving

参考:http://www.cnblogs.com/kedebug/archive/2013/03/26/2983143.html 大致题意: 给定一个地图和一条蛇,问蛇需要最少走多少步才能走到出口(1,1),如果无法到达则-1 大体思路: 因为蛇最多有7个身体那么每个身体都有上下左右4种状态(00-11二进制),可以用2^(7*2) = 1   后一个身体的状态可以根据前一个身体决定, 有

bad 1232
上一篇: poj1465
下一篇: poj2935 Basic Wall Maze
捡石子的小孩
博客等级 码龄14年 51粉丝 117原创
评论 2
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值