POJ 1324 Holedox Moving(上)

昨天课太多,晚上写了这题其中的两个函数,今天给补全了:有一点很不一样,判重的时候用的是头结点的位置与身体的相对位置。建一个三维组vis[22][22][16400](除了头以外最多还有7节,每一节相对上一节有上,下,左,右四种位置,可用两位二进制表示 一个14位的二进制)。判重的时候看蛇的形状有没有一样的就行了。进行BFS的时候应该把 图 和 蛇 合并到一起判断可以向那个方向走。
Holedox Moving
Time Limit: 5000MSMemory Limit: 65536K
Total Submissions: 11719Accepted: 2794

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). 
POJ 1324 Holedox Moving(上) - qhn999 - 码代码的猿猿

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 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

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

因为是一个个函数凑上的所以
我的代码一向很长。。。。 

#include <iostream>
#include <string.h>
#include <string>
#include <cmath>
#include <queue>

using namespace std;

struct SNote
{
    int len;
    int SnakeX[8];//蛇 结点 和 位置  0号是头
    int SnakeY[8];
    int spa;
    int deepth;
};

int Map[22][22];

int vis[21][21][16400];

queue<SNote> q;

int tovis(SNote sn)
{
    string msg;
    for(int i=0;i<sn.len-1;i++)
    {
        if(sn.SnakeX+1==sn.SnakeX[i+1]&&sn.SnakeY==sn.SnakeY[i+1])//  shang
        {
            msg+="10";
        }
        else if(sn.SnakeX-1==sn.SnakeX[i+1]&&sn.SnakeY==sn.SnakeY[i+1])//xia
        {
            msg+="11";
        }
        else if(sn.SnakeY-1==sn.SnakeY[i+1]&&sn.SnakeX==sn.SnakeX[i+1])//zuo
        {
            msg+="00";
        }
        else if(sn.SnakeY+1==sn.SnakeY[i+1]&&sn.SnakeX==sn.SnakeX[i+1])//you
        {
            msg+="01";
        }
    }
   // cout<<msg<<endl;

    int sum=0;
    for(int i=0;i<msg.length();i++)
    {
        if(msg=='1')
            sum+=pow(2,i);
    }
    if(vis[sn.SnakeX[0]][sn.SnakeY[0]][sum]==0)
    {
        vis[sn.SnakeX[0]][sn.SnakeY[0]][sum]=1;
        return 1;
    }
    else return 0;
}

int _bfs()
{
    SNote b;
    while(!q.empty())
    {
        b=q.front();
        q.pop();
    int m[22][22];
    for(int i=0;i<22;i++)
        for(int j=0;j<22;j++)
           m[j]=Map[j];
    SNote a=b;
    for(int i=0;i<a.len;i++)
    {
        if(i==0)
            m[b.SnakeX][b.SnakeY]=3;
        else
            m[b.SnakeX][b.SnakeY]=2;
    }
    if(m[1][1]==3)
    {
        return b.deepth;
    }
    else
    {
    //go up
    a=b;
    if(m[a.SnakeX[0]-1][a.SnakeY[0]]==0)
    {
        int ac,bc;
        ac=a.SnakeX[0]; bc=a.SnakeY[0];
        for(int i=1;i<a.len;i++)
        {
            swap(ac,a.SnakeX);
            swap(bc,a.SnakeY);
        }
        a.SnakeX[0]=a.SnakeX[0]-1;

        if(tovis(a))
        {
   //                cout<<"可以向up移动"<<endl;
        a.deepth=b.deepth+1;
        q.push(a);
        }
        else  ;
    }
    //go down
    a=b;
    if(m[a.SnakeX[0]+1][a.SnakeY[0]]==0)
    {
        int ac,bc;
        ac=a.SnakeX[0]; bc=a.SnakeY[0];
        for(int i=1;i<a.len;i++)
        {
            swap(ac,a.SnakeX);
            swap(bc,a.SnakeY);
        }
        a.SnakeX[0]=a.SnakeX[0]+1;
      if(tovis(a))
        {
  //            cout<<"可以向down移动"<<endl;
        a.deepth=b.deepth+1;
        q.push(a);
        }
        else  ;
    }
    //go right
    a=b;
    if(m[a.SnakeX[0]][a.SnakeY[0]+1]==0)
    {
        int ac,bc;
        ac=a.SnakeX[0]; bc=a.SnakeY[0];
        for(int i=1;i<a.len;i++)
        {
            swap(ac,a.SnakeX);
            swap(bc,a.SnakeY);
        }
        a.SnakeY[0]=a.SnakeY[0]+1;
       if(tovis(a))
        {
  //                 cout<<"可以向right移动"<<endl;
        a.deepth=b.deepth+1;
        q.push(a);
        }
        else  ;
    }
    //go left
    a=b;
    if(m[a.SnakeX[0]][a.SnakeY[0]-1]==0)
    {

        int ac,bc;
        ac=a.SnakeX[0]; bc=a.SnakeY[0];
        for(int i=1;i<a.len;i++)
        {
            swap(ac,a.SnakeX);
            swap(bc,a.SnakeY);
        }
        a.SnakeY[0]=a.SnakeY[0]-1;
         if(tovis(a))
        {
  //                 cout<<"可以向left移动"<<endl;
        a.deepth=b.deepth+1;
        q.push(a);
        }
        else  ;
    }
    }
    }
    return -1;
}

int main()
{
    int tot=0;

    int m,n,c;
 while(cin>>n>>m>>c&&m!=0&&c!=0&&n!=0)
 {
     SNote sn;
    tot++;
    memset(Map,0,sizeof(Map));
    memset(vis,0,sizeof(vis));
    for(int i=0;i<=m+1;i++)
    {
        Map[0]=1;
        Map[n+1]=1;
    }
    for(int i=0;i<=n+1;i++)
    {
        Map[0]=1;
        Map[m+1]=1;
    }
    sn.len=c;
    for(int i=0;i<c;i++)
    {
        cin>>sn.SnakeX>>sn.SnakeY;
    }
    sn.deepth=0;

    int k;
    cin>>k;
    for(int i=0;i<k;i++)
    {
        int a,b;
        cin>>a>>b;
        Map[a]=1;
    }

    q.push(sn);
    cout<<"Case "<<tot<<": "<<_bfs()<<endl;
    while(!q.empty())
        q.pop();

 }
    return 0;
}
 
虽然给了5S的运行时间可是依旧超时了,欲知后事如何,请听下回分解:
posted @ 2013-04-17 04:34  码代码的猿猿  阅读(272)  评论(0编辑  收藏  举报