1. 程式人生 > >poj 1324 狀態壓縮+bfs

poj 1324 狀態壓縮+bfs

program order nta cati beginning pro always origin accept

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 }

poj 1324 狀態壓縮+bfs