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. 

Source

思路:从头部開始想尾部走,不同方向代表不同的值,就得到蛇的状态,然后就是普通的BFS,还要注意不能咬到自己,首尾相连也不行。

#include <stdio.h>

struct S{
int step,x[9],y[9],head;
}que[1000000]; bool vis[20][20][16384];
int mp[20][20],nxt[4][2]={{1,0},{0,-1},{0,1},{-1,0}},mi[8]={1,4,16,64,156,1024,4096,16384}; int main()
{
int n,m,l,i,j,k,x,y,head,temp,lx,ly,tl,valx,valy,casenum=1,top,bottom; while(~scanf("%d%d%d",&n,&m,&l) && n)
{
for(i=0;i<l;i++)
{
scanf("%d%d",&x,&y); que[0].x[i]=x-1;
que[0].y[i]=y-1;
} for(i=0;i<n;i++) for(j=0;j<m;j++) mp[i][j]=1;
for(i=0;i<n;i++) for(j=0;j<m;j++) for(k=0;k<mi[l-1];k++) vis[i][j][k]=0; scanf("%d",&k); while(k--)
{
scanf("%d%d",&x,&y); mp[x-1][y-1]=0;
} que[0].step=0;
que[0].head=0; top=0;
bottom=1; while(top<bottom)
{
if(!que[top].x[que[top].head] && !que[top].y[que[top].head])
{
printf("Case %d: %d\n",casenum++,que[top].step); break;
} head=que[top].head-1; if(head==-1) head=l; que[top].step++; for(i=0;i<4;i++)
{
x=que[top].x[head]=que[top].x[que[top].head]+nxt[i][0];
y=que[top].y[head]=que[top].y[que[top].head]+nxt[i][1]; if(x>=0 && x<n && y>=0 && y<m && mp[x][y])
{
temp=0; lx=x;
ly=y; tl=0; for(j=que[top].head;tl<l;j++)
{
if(j==l+1) j=0; if(que[top].x[j]==x && que[top].y[j]==y) break;//假设会咬到自己 if(tl<l-1)//计算状态
{
valx=lx-que[top].x[j];
valy=ly-que[top].y[j]; if(valx==0 && valy==1) temp=temp<<2;
else if(valx==1 && valy==0) temp=temp<<2|1;
else if(valx==-1 && valy==0) temp=temp<<2|2;
else if(valx==0 && valy==-1) temp=temp<<2|3; lx=que[top].x[j];
ly=que[top].y[j];
} tl++;
} if(tl==l && !vis[x][y][temp])//假设不会咬到自己而且该状态没有訪问过
{
vis[x][y][temp]=1; que[top].head--; if(que[top].head==-1) que[top].head=l; que[top].x[head]=x;
que[top].y[head]=y; que[bottom++]=que[top]; que[top].head++; if(que[top].head==l+1) que[top].head=0;
}
}
} top++;
} if(top==bottom) printf("Case %d: -1\n",casenum++);
}
}

POJ-1324-Holedox Moving(BFS)的更多相关文章

  1. POJ 1324 Holedox Moving (状压BFS)

    POJ 1324 Holedox Moving (状压BFS) Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 18091 Acc ...

  2. poj 1324 Holedox Moving

    poj 1324 Holedox Moving 题目地址: http://poj.org/problem?id=1324 题意: 给出一个矩阵中,一条贪吃蛇,占据L长度的格子, 另外有些格子是石头, ...

  3. POJ - 1324 Holedox Moving (状态压缩+BFS/A*)

    题目链接 有一个n*m(1<=n,m<=20)的网格图,图中有k堵墙和有一条长度为L(L<=8)的蛇,蛇在移动的过程中不能碰到自己的身体.求蛇移动到点(1,1)所需的最小步数. 显然 ...

  4. POJ 1324 Holedox Moving 搜索

    题目地址: http://poj.org/problem?id=1324 优先队列---A*的估价函数不能为蛇头到(1,1)的距离,这样会出错. 看了discuss,有大神说这题A*的估价函数为BFS ...

  5. poj 1324 状态压缩+bfs

    http://poj.org/problem?id=1324 Holedox Moving Time Limit: 5000MS   Memory Limit: 65536K Total Submis ...

  6. poj1324 Holedox Moving

    Holedox Moving Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 16980   Accepted: 4039 D ...

  7. poj 3414 Pots 【BFS+记录路径 】

    //yy:昨天看着这题突然有点懵,不知道怎么记录路径,然后交给房教了,,,然后默默去写另一个bfs,想清楚思路后花了半小时写了120+行的代码然后出现奇葩的CE,看完FAQ改了之后又WA了.然后第一次 ...

  8. UVALive 2520 Holedox Moving(BFS+状态压缩)

    这个题目在比赛的时候我们是没有做出来的,但是听到他们说进制哈希的时候,感觉真的是挺高端的,于是赛后开始补题,本着我的习惯在看题解之前自己再试着写一遍,我当时存储状态的方法是string + map,我 ...

  9. POJ 1324(BFS + 状态压缩)

    题意:给你一条蛇,要求一以最少的步数走到1,1 思路: 最开始一直没想到应该怎样保存状态,后来发现别人用二进制保存蛇的状态,即每两个节点之间的方向和头节点,二进制最多14位(感觉状态保存都能扯到二进制 ...

  10. poj 3026 Borg Maze (BFS + Prim)

    http://poj.org/problem?id=3026 Borg Maze Time Limit:1000MS     Memory Limit:65536KB     64bit IO For ...

随机推荐

  1. 深入浅出Hadoop Mahout数据挖掘实战(算法分析、项目实战、中文分词技术)

    Mahout简介 Mahout 是 Apache Software Foundation(ASF) 旗下的一个开源项目, 提供一些可扩展的机器学习领域经典算法的实现,旨在帮助开发人员更加方便快捷地创建 ...

  2. python datetime模块strptime/strptime format常见格式命令_施罗德_新浪博客

    python datetime模块strptime/strptime format常见格式命令_施罗德_新浪博客     python datetime模块strptime/strptime form ...

  3. 【OpenCV】漫水填充

    漫水填充:也就是用一定颜色填充联通区域,通过设置可连通像素的上下限以及连通方式来达到不同的填充效果;漫水填充经常被用来标记或分离图像的一部分以便对其进行进一步处理或分析,也可以用来从输入图像获取掩码区 ...

  4. 最长公共子序列python实现

    最长公共子序列是动态规划基本题目,以下依照动态规划基本步骤解出来. 1.找出最优解的性质,并刻划其结构特征 序列a共同拥有m个元素,序列b共同拥有n个元素,假设a[m-1]==b[n-1],那么a[: ...

  5. SPARK在linux中的部署,以及SPARK中聚类算法的使用

    眼下,SPARK在大数据处理领域十分流行.尤其是对于大规模数据集上的机器学习算法.SPARK更具有优势.一下初步介绍SPARK在linux中的部署与使用,以及当中聚类算法的实现. 在官网http:// ...

  6. Effective C++_笔记_条款02_尽量以const、enum、inline替换#define

    (整理自Effctive C++,转载请注明.整理者:华科小涛@http://www.cnblogs.com/hust-ghtao/) 这个条款或许改为“宁可以编译器替换预处理器”比较好,因为或许#d ...

  7. TCP/IP协议的编写《转载》

    基于HHARM9-EDU的TCP/IP(UDP)协议的实现 原文网址:http://blog.csdn.net/lhj0503/article/details/3323788 摘 要:嵌入式技术的发展 ...

  8. python 网络爬虫(二) BFS不断抓URL并放到文件中

    上一篇的python 网络爬虫(一) 简单demo 还不能叫爬虫,只能说基础吧,因为它没有自动化抓链接的功能. 本篇追加如下功能: [1]广度优先搜索不断抓URL,直到队列为空 [2]把所有的URL写 ...

  9. (step6.3.3)hdu 1150(Machine Schedule——二分图的最小点覆盖数)

    题目大意:第一行输入3个整数n,m,k.分别表示女生数(A机器数),男生数(B机器数),以及它们之间可能的组合(任务数). 在接下来的k行中,每行有3个整数c,a,b.表示任务c可以有机器A的a状态或 ...

  10. 4.锁--Synchronizer Framework Base Class—AbstractQueuedSynchronizer介绍

    1. AQS简单介绍 AQS是Java并发类库的基础.其提供了一个基于FIFO队列,可以用于构建锁或者其它相关同步装置的基础框架.该同步器(下面简称同步器)利用了一个int来表示状态,期望它可以成为实 ...