Holedox Moving (zoj 1361 poj 1324)bfs

 
Holedox Moving

Time Limit: 10 Seconds      Memory Limit: 32768 KB

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


Note: 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: Asia 2002, Beijing (Mainland China)

 

 

这是在知道蛇的状态用1,2,3,4代表所 写的代码
属于盲目搜索、在zoj上可以1700+Ms水过
在Poj上果断TLT;
#include <iostream> #include <stdio.h> #include <queue> #include <set> #include <vector> #include <string.h> #include <algorithm> using namespace std; int dir[4][2]={-1,0,1,0,0,-1,0,1}; int rc[4]={4,1,3,2}; struct node { int x,y; }; bool map[22][22]; struct snake { node t; node w[7]; int v; int dis; }; int n,m,L,fc; int get(node &a,node &b) { if(a.y==b.y&&a.x>b.x) return 1; if(a.x==b.x&&a.y>b.y) return 2; if(a.x==b.x&&a.y<b.y) return 3; return 4; } int BFS(snake &a) { set <int> st[401]; set <int>::iterator it; queue <snake> Q; snake b; int v=0,nd=(a.t.x-1)*m+a.t.y; int i,k; int x,y; v=v*10+get(a.t,a.w[0]); for(k=1;k<L;k++) v=v*10+get(a.w[k-1],a.w[k]); st[nd].insert(v); a.v=v; Q.push(a); while(!Q.empty()) { a=Q.front();Q.pop(); //printf("(%d,%d)->",a.v,a.dis); if(a.t.y==1&&a.t.x==1) return a.dis; map[a.t.x][a.t.y]=1; for(i=0;i<L;i++) { map[a.w[i].x][a.w[i].y]=1; } for(i=0;i<4;i++) { x=a.t.x+dir[i][0]; y=a.t.y+dir[i][1]; if(x<1||x>n||y<1||y>m||map[x][y]) continue; nd=(x-1)*m+y; v=rc[i]; v=v*fc+a.v/10; it=st[nd].find(v); //printf("%d s=%d ",nd,st[nd].size()); if(it==st[nd].end()||st[nd].empty()) { st[nd].insert(v); b.v=v; b.t.x=x;b.t.y=y; b.dis=a.dis+1; b.w[0].x=a.t.x; b.w[0].y=a.t.y; for(k=1;k<L;k++) b.w[k]=a.w[k-1]; Q.push(b); }else continue; } map[a.t.x][a.t.y]=0; for(i=0;i<L;i++) { map[a.w[i].x][a.w[i].y]=0; } } return -1; } int main() { int i,k; int t=1; snake s; int x,y; while(scanf("%d %d %d",&n,&m,&L),n||m||L) { memset(map,0,sizeof(map)); scanf("%d %d",&s.t.x,&s.t.y); s.dis=0; L--; fc=1; for(i=0;i<L;i++) { fc=fc*10; scanf("%d %d",&s.w[i].x,&s.w[i].y); } fc/=10; scanf("%d",&k); for(i=1;i<=k;i++) { scanf("%d %d",&x,&y); map[x][y]=1; } printf("Case %d: %d\n",t++,BFS(s)); } return 0; }

//了解相关资料后、修修补补写了3小时、终于、、141Ms在POj上排50以内了
优化 就是A*启发式+优先队列 目前步数+改点到终点步数
//以后再有新知识再来继续修改这题吧
#include <iostream>
#include <stdio.h>
#include <queue>
#include <stack>
#include <set>
#include <vector>
#include <string.h>
#include <algorithm>
using namespace std;
//bool h[1<<23];
bool map[22][22];
int dis[22][22];
int dir[4][2]={-1,0,0,1,1,0,0,-1};
int get(int x,int y,int a,int b)
{
    if(x==a)
      if(y<b) return 1;
      else    return 3;
    else
      if(x>a) return 0;
   return 2;
}
struct node
{
    int x,y,step,to;
    int s;
    bool operator <(const node &b)const
    {
        return to>b.to;
    }
};
int n,m,L,l;
void bfs()
{
    dis[1][1]=0;
    node a,b;
    a.x=1,a.y=1;
    queue<node> Q;
    Q.push(a);
    int i;
    while(!Q.empty())
    {
        a=Q.front();Q.pop();
        for(i=0;i<4;i++)
        {
            b.x=a.x+dir[i][0];
            b.y=a.y+dir[i][1];
            if(b.x<1||b.y<1||b.x>n||b.y>m||map[b.x][b.y]||dis[b.x][b.y]!=-1) continue;
            dis[b.x][b.y]=dis[a.x][a.y]+1;
           // printf("%d % d %d\n",b.x,b.y,dis[b.x][b.y]);
            Q.push(b);
        }
    }
}
bool f;
void BFS(node &st)
{
    set <int> S;
    set <int>::iterator it;
    int sa=st.x*n+st.y;
    sa=(sa<<(l<<1))|st.s;
    S.insert(sa);
    node temp;
    int pos[10][2];
    int i,k;
    priority_queue<node> Q;
    Q.push(st);
    while(!Q.empty())
    {
        temp=Q.top();Q.pop();
        //printf("%d %d\n",temp.x,temp.y);
        if(temp.x==1&&temp.y==1){f=1;printf("%d\n",temp.step);return;}
        pos[0][0]=temp.x;
        pos[0][1]=temp.y;
        map[temp.x][temp.y]=1;
        sa=temp.s;
        for(i=1;i<L;i++)
        {
            k=(sa>>((l-i)<<1))&3;
            pos[i][0]=pos[i-1][0]+dir[k][0];
            pos[i][1]=pos[i-1][1]+dir[k][1];
            map[pos[i][0]][pos[i][1]]=1;
        }
        for(i=0;i<4;i++)
        {
            st.x=temp.x+dir[i][0];
            st.y=temp.y+dir[i][1];
            if(st.y<1||st.x<1||st.x>n||st.y>m||map[st.x][st.y]) continue;
            st.s=(get(st.x,st.y,temp.x,temp.y)<<((l-1)<<1))|(temp.s>>2);
            sa=st.x*n+st.y;
            sa=(sa<<(l<<1))|st.s;
            it=S.find(sa);
            if(!S.empty()&&it!=S.end()) continue;
            S.insert(sa);
            st.step=temp.step+1;
            st.to=st.step+dis[st.x][st.y];
            Q.push(st);
        }
        for(i=0;i<L;i++)
        {
            map[pos[i][0]][pos[i][1]]=0;
        }
    }
}
int main()
{
    int T=1;
    int i,k;
    int x,y;
    int a,b;
    node st;
    while(scanf("%d %d %d",&n,&m,&L),n||m||L)
    {
        memset(map,0,sizeof(map));
        memset(dis,-1,sizeof(dis));
        scanf("%d %d",&x,&y);
        st.x=x;
        st.y=y;
        k=0;l=L-1;
        for(i=1;i<L;i++)
        {
            scanf("%d %d",&a,&b);
            k=(k<<2)|(get(x,y,a,b));
            x=a,y=b;
        }
        st.s=k;
        scanf("%d",&k);
        for(i=0;i<k;i++)
        {
            scanf("%d %d",&a,&b);
            map[a][b]=1;
        }
        bfs();
        printf("Case %d: ",T++);
        if(dis[st.x][st.y]==-1) { printf("-1\n"); continue; }
        //printf("%d\n",dis[st.x][st.y]);
        st.step=0;
        st.to=dis[st.x][st.y];
        f=0;
        BFS(st);
        if(!f) printf("-1\n");
    }
    return 0;
}
 

 

原文地址:https://www.cnblogs.com/372465774y/p/2754407.html