poj 1324 状态压缩+bfs

http://poj.org/problem?id=1324

Holedox Moving

Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 17042   Accepted: 4065

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

Beijing 2002

bfs,难点在于标记数组的表示,如何表示当前蛇的状态,有个很巧妙的方法是利用位运算进行状压,只要知道蛇头的位置以及每一部分之间的关系(即up,down,left,right)我们就可以表示出蛇的状态,最长有7个身长(不包括头),用0,1,2,3表示四个方向每个数占两位最多14位所以内存可以接受。只要处理好状压的蛇身基本就能A了。注意不能走到石头处和当前蛇身处。跑了1700ms。

 1 #include<iostream>
 2 #include<cstring>
 3 #include<cstdio>
 4 #include<queue>
 5 #include<algorithm>
 6 using namespace std;
 7 int N,M,L,K,all;
 8 struct xy{int x,y;}P[15];
 9 struct node{int x,y,bs,has;};
10 bool vis[21][21][1<<14];
11 bool sto[21][21];
12 int fx[4][2]={-1,0,1,0,0,-1,0,1};
13 int ldx[4]={1,0,3,2};
14 bool check(node t,int dx,int dy)
15 {
16     int x=t.x,y=t.y;
17     for(int i=2;i<=L;++i)
18     {
19         int tt=0;
20         if(t.has&1) tt+=1;t.has>>=1;
21         if(t.has&1) tt+=2;t.has>>=1;
22         x=x+fx[tt][0];
23         y=y+fx[tt][1];
24         if(x==dx&&y==dy) return 0;
25     }
26     return 1;
27 }
28 void bfs(node st)
29 {
30     memset(vis,0,sizeof(vis));
31     queue<node>q;
32     q.push(st);
33     while(!q.empty()){
34         node t=q.front();q.pop();
35         if(vis[t.x][t.y][t.has]) continue;
36         vis[t.x][t.y][t.has]=1;
37         if(t.x==1&&t.y==1){cout<<t.bs<<endl;return;}
38         for(int i=0;i<4;++i)
39         {
40             node _t=t;
41             int dx=_t.x+fx[i][0];
42             int dy=_t.y+fx[i][1];
43             if(dx<1||dy<1||dx>N||dy>M||sto[dx][dy]||!check(_t,dx,dy)) continue;
44             int has=(_t.has<<2)&(all)|(ldx[i]);
45             _t.has=has;
46             _t.bs++;
47             _t.x=dx;
48             _t.y=dy;
49             if(vis[dx][dy][has]) continue;
50             q.push(_t);
51         }
52     }
53     puts("-1");
54 }
55 int main()
56 {
57     int i,j,k=0;
58     while(cin>>N>>M>>L){
59         if(N==0&&M==0&&L==0) break;
60         memset(sto,0,sizeof(sto));
61         for(i=1;i<=L;++i) scanf("%d%d",&P[i].x,&P[i].y);
62         scanf("%d",&K);
63         for(i=1;i<=K;++i)
64         {
65             int o,p;
66             scanf("%d%d",&o,&p);
67             sto[o][p]=1;
68         }
69         printf("Case %d: ",++k);
70         all=(1<<((L-1)*2))-1;
71         node st;
72         st.x=P[1].x;
73         st.y=P[1].y;
74         st.bs=0;
75         st.has=0;
76         for(i=2;i<=L;++i)
77         {
78             for(j=0;j<4;++j)
79             {
80                int dx=P[i-1].x+fx[j][0];
81                int dy=P[i-1].y+fx[j][1];
82                if(dx==P[i].x&&dy==P[i].y){
83                 st.has=st.has|(j<<((i-2)*2));
84                }
85             }
86         }
87         bfs(st);
88     }
89     return 0;
90 }
时间: 2024-10-12 04:02:40

poj 1324 状态压缩+bfs的相关文章

胜利大逃亡(续)(状态压缩bfs)

胜利大逃亡(续) Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 7357    Accepted Submission(s): 2552 Problem Description Ignatius再次被魔王抓走了(搞不懂他咋这么讨魔王喜欢)……这次魔王汲取了上次的教训,把Ignatius关在一个n*m的地牢里,并在地牢的某些地方安装了带

2014 Super Training #6 G Trim the Nails --状态压缩+BFS

原题: ZOJ 3675 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3675 由m<=20可知,可用一个二进制数表示指甲的状态,最多2^20,初始状态为0,表示指甲都没剪,然后BFS找解,每次枚举剪刀的两个方向,枚举移动的位数进行扩展状态即可. 代码: #include <iostream> #include <cstdio> #include <cstring> #include &

hdu1429胜利大逃亡(续) (状态压缩+BFS)

Problem Description Ignatius再次被魔王抓走了(搞不懂他咋这么讨魔王喜欢)-- 这次魔王汲取了上次的教训,把Ignatius关在一个n*m的地牢里,并在地牢的某些地方安装了带锁的门,钥匙藏在地牢另外的某些地方.刚开始Ignatius被关在(sx,sy)的位置,离开地牢的门在(ex,ey)的位置.Ignatius每分钟只能从一个坐标走到相邻四个坐标中的其中一个.魔王每t分钟回地牢视察一次,若发现Ignatius不在原位置便把他拎回去.经过若干次的尝试,Ignatius已画

hdu 4771 Stealing Harry Potter&#39;s Precious (状态压缩+bfs)

Stealing Harry Potter's Precious Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1297    Accepted Submission(s): 619 Problem Description Harry Potter has some precious. For example, his invisib

hdu1885Key Task(状态压缩+bfs)

题目链接: 啊哈哈,点我点我 这题和hdu1429是姊妹题  请参见传送门 题目: Key Task Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1187    Accepted Submission(s): 470 Problem Description The Czech Technical University is rath

hdu1429胜利大逃亡(续)(状态压缩+bfs)

题目链接: 啊哈哈,点我点我 题意及思路 最开始我以为跟普通的bfs一样,所以直接写了一个朴素的bfs,一跑,前两组数据对了,但是第三组不对,一看,走过的还可以走啊,所以不能标记,结果我的bfs乱改,最后 毫无疑问改成了死循环.所以看题解... 思路:因为有10中不同的钥匙,每种都有两种状态,所以结合计算机是二进制保存的特点,刚好把这10把钥匙当成每一个为,要要1<<10个位保存所有的状态,然后就是模拟捡起钥匙,捡起钥匙就是说明这个位上的数字变成1这个状态,所以自然而然想到了位运算,只要|一下

Mondriaan&#39;s Dream(POJ 2411状态压缩dp)

题意:用1*2的方格填充m*n的方格不能重叠,问有多少种填充方法 分析:dp[i][j]表示i行状态为j时的方案数,对于j,0表示该列竖放(影响下一行的该列),1表示横放成功(影响下一列)或上一列竖放成功.状态转移时,枚举每一行可能的状态上一行取反得下一行能放的状态. #include <map> #include <set> #include <list> #include <cmath> #include <queue> #include &

hdu5094 状态压缩+bfs

http://acm.hdu.edu.cn/showproblem.php?pid=5094 Problem Description This story happened on the background of Star Trek. Spock, the deputy captain of Starship Enterprise, fell into Klingon's trick and was held as prisoner on their mother planet Qo'noS.

POJ 1324 Holedox Moving 贪吃蛇 状态压缩 BFS

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.