POJ 1324 Holedox Moving 贪吃蛇 状态压缩 BFS
来源:互联网 发布:楼盘销售软件 编辑:程序博客网 时间:2024/05/16 09:27
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).
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.
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 44 14 23 23 132 33 33 44 4 42 31 31 42 442 12 23 44 20 0 0
Sample Output
Case 1: 9Case 2: -1题意:一个M X N的地图 和一个长度为l的蛇 给出蛇各个部位的坐标 和所有障碍物的坐标 求蛇头到达终点的最短距离。首先想到是根据蛇头BFS遇到障碍或身体则不可行 但这样不可行 因为蛇头可能再次经过已经经过的点时间开销太大 所以应该想一种方法构成标记数组(hash)的第三维元素 由此想到状态压缩 将蛇的形状压缩成一个四进制的数(每一位代表相对前一部分的方向偏移大小)这样就可以实现去重。#include<iostream>#include<cstdio>#include<cstring>#include<queue>using namespace std;struct node{ int x,y,step; int state;} q[1<<20],head,rear;int vis[25][25][16384]; //蛇的形状int viss[25][25]; //障碍int dir[4][2]= {-1,0,0,1,1,0,0,-1};int bit[3][3]; //偏移大小数组 x ,y都加了1防止越界int m,n,len;void init(){ bit[0][1]=0; bit[1][2]=1; bit[2][1]=2; bit[1][0]=3; //四方向 一定要和dir数组对应 memset(vis,0,sizeof(vis)); memset(viss,0,sizeof(viss)); return ;}void bfs(){ int a=0,b=1; q[0]=head; while(a<b) { head=q[a++]; if(head.x==1&&head.y==1) { cout<<head.step<<endl; return ; } for(int i=0; i<4; i++) { rear.x=head.x+dir[i][0]; rear.y=head.y+dir[i][1]; if(rear.x<1||rear.y<1||rear.x>m||rear.y>n||viss[rear.x][rear.y]) continue; int j,t,tx=head.x,ty=head.y,tstate=head.state; rear.state=head.state%(1<<((len-2)*2)); //去掉四进制的最高位 %1000..(二进制) rear.state=rear.state*4+bit[head.x-rear.x+1][head.y-rear.y+1]; for(j=1; j<len; j++) //是否碰到身体 { t=tstate%4; tstate/=4; tx+=dir[t][0]; ty+=dir[t][1]; if(tx==rear.x&&ty==rear.y) break; } if(j<len||vis[rear.x][rear.y][rear.state]) continue; vis[rear.x][rear.y][rear.state]=1; rear.step=head.step+1; q[b++]=rear; } } cout<<-1<<endl; return ;}int main(){ int i,j,nn,test=0; int snake[8][2]; while(scanf("%d%d%d",&m,&n,&len)&&(m||n||len)) { init(); int a,b; for(i=0; i<len; i++) scanf("%d%d",&snake[i][0],&snake[i][1]); scanf("%d",&nn); while(nn--) { scanf("%d%d",&a,&b); viss[a][b]=1; } head.state=0; for(i=len-1; i>=1; i--) head.state=head.state*4+bit[snake[i][0]-snake[i-1][0]+1][snake[i][1]-snake[i-1][1]+1]; //第三为元素 state head.x=snake[0][0]; head.y=snake[0][1]; head.step=0; vis[head.x][head.y][head.state]=1; printf("Case %d: ",++test); bfs(); } return 0;}
1 0
- POJ 1324 Holedox Moving 贪吃蛇 状态压缩 BFS
- Holedox Moving 贪吃蛇 BFS 状态压缩
- POJ 1324 Holedox Moving(状态压缩BFS)
- poj 1324 Holedox Moving(bfs+状态压缩)
- POJ-1324:Holedox Moving(BFS+状态压缩+蛇蛇历险记)
- poj 1324 Holedox Moving (贪吃蛇 状态压缩存蛇的状态)
- POJ 1324Holedox Moving (状态BFS)
- POJ 1324 Holedox Moving 状态压缩+A* -
- (BFS,状态空间优化)Holedox Moving,贪吃蛇
- Holedox Moving----POJ_1324----BFS+状态压缩
- hdu1324 Holedox Moving (bfs+状态压缩+A*)
- POJ1324 Holedox Moving(状态压缩+BFS)
- poj1324-Holedox Moving-状态压缩+BFS
- POJ-1324-Holedox Moving(BFS)
- POJ 1324 贪吃蛇状态压缩BFS
- POJ 1324 Holedox Moving(A*+状态压缩)
- POJ 1324 Holedox Moving(bfs + hash)
- POJ 1324 Holedox Moving (BFS+剪枝)
- spark内核揭秘-08-spark的Web监控页面
- android截取图片
- hdu1018 big number
- 解决Maven中使用很多本地jar包的编译问题
- 老外看中国:本土移动应用差异在哪?
- POJ 1324 Holedox Moving 贪吃蛇 状态压缩 BFS
- DNS、DNS Proxy配置要点及实例
- eclipse中双击一个变量,文件中所有这个变量都会变成黄色,现在变不了了的问题。
- 中小企业如何选择网络营销平台
- VMware mount共享文件夹到指定位置
- apktool简单反编译
- 使用OpenSSL生成证书
- 【Java】打印流与缓冲区读者完成输入与输出到文件操作
- 输入框字数限制+动态提示:10个汉字或20个字符的问题