poj 1475 Pushing Boxes

Description
Imagine you are standing inside a two-dimensional maze composed of square cells which may or may not be filled with rock. You can move north, south, east or west one cell at a step. These moves are called walks.
One of the empty cells contains a box which can be moved to an adjacent free cell by standing next to the box and then moving in the direction of the box. Such a move is called a push. The box cannot be moved in any other way than by pushing, which means that if you push it into a corner you can never get it out of the corner again.

One of the empty cells is marked as the target cell. Your job is to bring the box to the target cell by a sequence of walks and pushes. As the box is very heavy, you would like to minimize the number of pushes. Can you write a program that will work out the best such sequence?

Input
The input contains the descriptions of several mazes. Each maze description starts with a line containing two integers r and c (both <= 20) representing the number of rows and columns of the maze.

Following this are r lines each containing c characters. Each character describes one cell of the maze. A cell full of rock is indicated by a `#' and an empty cell is represented by a `.'. Your starting position is symbolized by `S', the starting position of the box by `B' and the target cell by `T'.

Input is terminated by two zeroes for r and c.
Output

For each maze in the input, first print the number of the maze, as shown in the sample output. Then, if it is impossible to bring the box to the target cell, print ``Impossible.''.

Otherwise, output a sequence that minimizes the number of pushes. If there is more than one such sequence, choose the one that minimizes the number of total moves (walks and pushes). If there is still more than one such sequence, any one is acceptable.

Print the sequence as a string of the characters N, S, E, W, n, s, e and w where uppercase letters stand for pushes, lowercase letters stand for walks and the different letters stand for the directions north, south, east and west.

Output a single blank line after each test case.
Sample Input

1 7
SB....T
1 7
SB..#.T
7 11
###########
#T##......#
#.#.#..####
#....B....#
#.######..#
#.....S...#
###########
8 4
....
.##.
.#..
.#..
.#.B
.##S
....
###T
0 0
Sample Output

Maze #1
EEEEE

Maze #2
Impossible.

Maze #3
eennwwWWWWeeeeeesswwwwwwwnNN

Maze #4

swwwnnnnnneeesssSSS

思路:

本来是在网上找A*算法的题才做的,发现并不太像A*(F=g+h 刚刚学不懂)。题要最少的push数,所以F函数有点难想。

所以就直接优先push数,用数组标记人与箱子的状态。

失误:

标记数组使用不当,不能标记了还放进队列,这样既影响空间有影响时间,开始还不明不白的找了好久错在哪。


103280k 1563ms 代码:

#include <iostream>
#include<stdio.h>
#include<string.h>
#include<queue>
#include<cmath>
#include<algorithm>
using namespace std;
struct node
{
    int x,y,a,b,j,tb,te;//人的坐标x y;箱子坐标a b;队列ID j;推箱子数 tb;人走的步数 te;
    bool operator <(const node &xx) const//优先级:优先推(push)箱子最少的,如果push一样的就优先te
    {
        if(tb==xx.tb) return te>xx.te;
        return tb>xx.tb;
    }
};
struct nodetep
{
    int f;
    char s;
};
int n,m,S,E,x,y,a,b,vist[5005][5005];
int F[4][2]= {{1,0},{-1,0},{0,-1},{0,1}};
nodetep tep[500005];
char s[30][30];
char SS(int i)
{
    switch (i)
    {
    case 0:
        return 's';
    case 1:
        return 'n';
    case 2:
        return 'w';
    case 3:
        return 'e';
    }
    return 0;
}
int Astar()
{
    priority_queue<node>q;
    node v,u;
    v.x=x,v.y=y;
    v.a=a,v.b=b;
    v.j=0,v.tb=0,v.te=0;
    q.push(v);
    tep[0].f=-1;
    int j=0,r,c,z,p;;
    while(!q.empty())
    {
        v=q.top();
        q.pop();
        if(v.a==S&&v.b==E)
            return v.j;
        for(int i=0; i<4; i++)
        {
            r=v.x+F[i][0], c=v.y+F[i][1];
            if(r>=0&&r<n&&c>=0&&c<m&&s[r][c]!='#')
            {
                if(r==v.a&&c==v.b) //推箱子
                {
                    z=r+F[i][0],p=c+F[i][1];
                    if(z>=0&&z<n&&p>=0&&p<m&&s[z][p]!='#') //能推
                    {
                        u.x=r,  u.y=c;
                        u.a=z,  u.b=p;
                        u.j=j+1,  u.te=v.te+1;
                        u.tb=v.tb+1;
                        int vv=u.x*100+u.y,yy=u.a*100+u.b;//标记数组   改成四维数组居然会快将近1000ms,可见多次的运算是多么费时啊!还费空间 390625<<<25050025
                        if(!vist[vv][yy])
                        {
                            vist[vv][yy]=1;
                            q.push(u);
                            j++;
                            tep[j].f=v.j;
                            tep[j].s=SS(i)-32;
                        }
                    }
                }
                else  //没有推
                {
                    u.x=r,  u.y=c;
                    u.a=v.a,  u.b=v.b;
                    u.j=j+1,   u.te=v.te+1;
                    u.tb=v.tb;
                    int vv=u.x*100+u.y,yy=u.a*100+u.b;//
                    if(!vist[vv][yy])
                    {
                        vist[vv][yy]=1;
                        q.push(u);
                        j++;
                        tep[j].f=v.j;
                        tep[j].s=SS(i);
                    }
                }
            }
        }
    }
    return 0;
}
int main()
{
    int T=1;
    while(scanf("%d %d",&n,&m)&&(n+m))
    {
        memset(tep,0,sizeof(tep));
        memset(vist,0,sizeof(vist));
        for(int i=0; i<n; i++)
        {
            scanf("%s",s[i]);
            for(int j=0; j<m; j++)
            {
                if(s[i][j]=='T') S=i,E=j;
                if(s[i][j]=='S') x=i,y=j;
                if(s[i][j]=='B') a=i,b=j;
            }
        }
        cout<<"Maze #"<<T++<<endl;
        int f=Astar(),r=0;
        if(f)
        {
            char st[500005];
            while(f!=-1)
            {
                st[r++]=tep[f].s;
                f=tep[f].f;
            }
            for(int i=r-2; i>=0; i--)
                printf("%c",st[i]);
            cout<<endl;
        }
        else
        {
            cout<<"Impossible."<<endl;
        }
        cout<<endl;
    }
    return 0;
}
</span>


188MS代码:

while(!q.empty())
    {
        v=q.top();
        q.pop();
        if(v.a==S&&v.b==E)
            return v.j;
        for(int i=0; i<4; i++)
        {
            r=v.x+F[i][0], c=v.y+F[i][1];
            if(r>=0&&r<n&&c>=0&&c<m&&s[r][c]!='#')
            {
                if(r==v.a&&c==v.b) //推箱子
                {
                    z=r+F[i][0],   p=c+F[i][1];
                    if(z>=0&&z<n&&p>=0&&p<m&&s[z][p]!='#') //能推
                    {
                        u.x=r, u.y=c;
                        u.a=z, u.b=p;
                        u.j=j+1;
                        u.te=v.te+1, u.tb=v.tb+1;
                        if(!vist[u.x][u.y][u.a][u.b])
                        {
                            vist[u.x][u.y][u.a][u.b]=1;
                            q.push(u);
                            j++;
                            tep[j].f=v.j;
                            tep[j].s=SS(i)-32;
                        }
                    }
                }
                else  //没有推
                {
                    u.x=r, u.y=c;
                    u.a=v.a, u.b=v.b;
                    u.j=j+1;
                    u.te=v.te+1, u.tb=v.tb;
                    if(!vist[u.x][u.y][u.a][u.b])
                    {
                        vist[u.x][u.y][u.a][u.b]=1;
                        q.push(u);
                        j++;
                        tep[j].f=v.j;
                        tep[j].s=SS(i);
                    }
                }
            }
        }
    }
    return 0;
}
</span>


Runtime Error 代码部分:

 while(!q.empty()){
        v=q.top();
        q.pop();
        int vv=v.x*100+v.y,yy=v.a*100+v.b;
        if(vist[vv][yy])//虽然避免了重复状态的访问,但是这样却让访问过得状态一直能进队列导致溢出。
            continue;
        vist[vv][yy]=1;
        if(v.a==S&&v.b==E){
            return v.j;
        }
        ff=v.j;
        for(int i=0;i<4;i++)
        {
            int r,c,z,p;
            r=v.x+F[i][0], c=v.y+F[i][1];
            if(r>=0&&r<n&&c>=0&&c<m&&s[r][c]!='#'){
                 if(r==v.a&&c==v.b){//推箱子
                     z=r+F[i][0],p=c+F[i][1];
                     if(z>=0&&z<n&&p>=0&&p<m&&s[z][p]!='#'){//能推
                        u.x=r,u.y=c,u.a=z,u.b=p,u.j=j+1;
                        u.t=v.t+1;
                        u.f=fact(u);
                        q.push(u);//没有判断是否重复就进队列!低级错误!
                        j++;
                        tep[j].f=ff;
                        tep[j].s=SS(i)-32;
                     }
                 }else{//没有推
                    u.x=r,u.y=c,u.a=v.a,u.b=v.b,u.j=j+1;
                    u.t=v.t;
                    u.f=fact(u);
                    q.push(u);
                    j++;
                    tep[j].f=ff;
                    tep[j].s=SS(i);
                 }
            }
        }
    }
    return 0;
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值