程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> poj1324 Holedox Moving

poj1324 Holedox Moving

編輯:C++入門知識

Holedox Moving
Time Limit: 5000MS  Memory Limit: 65536K
Total Submissions: 11975  Accepted: 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了,仔細一想其實還是有問題。

   最後不得不記錄整個蛇的狀態了,但實在不知道如何壓縮狀態了。看了解題報告後才明白可以依次記下每一個節點相對前一個節點的位置,這樣通過一個三維數組就可以判重了,而且內存也不會爆。

 

[cpp]
#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
*/ 

#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
*/


 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved