搜索好题集合2

八数码

之前博客里贴了三篇这里就不写了

POJ 1475 Pushing Boxes

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 byB’ 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
0 0
Sample Output
Maze #1
EEEEE

Maze #2
Impossible.
嗯..经典小游戏推箱子,童年的回忆啊(没通关过)
此题貌似有很6的解法两次bfs秒杀,不过作为蒟蒻我还是写的很菜,一个bfs同时枚举人和箱子的状态,1400ms卡过..(话说我估计别人的复杂度是箱子和人加起来我是乘起来),不过好处是这个代码短啊,压压行在vj上长度最短,爆了第二名的某费大佬600多哇咔咔咔!!!

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
bool mp[25][25],vis[25][25][25][25]={0};
const int dx[4]={-1,1,0,0};
const int dy[4]={0,0,-1,1};
const char dir[4]={'n','s','w','e'};
int edx,edy,r,c,cntt=0;
struct state{
    int px,py,bx,by,cnt;string foot;
    friend bool operator<(state a,state b)
    {if(a.cnt!=b.cnt)return a.cnt>b.cnt;
     else return a.foot.length()>b.foot.length();}
};
inline state bfs(int px,int py,int bx,int by)
{ 
    priority_queue<state>q;
    state tmp,now;tmp.foot="";tmp.cnt=0;
    tmp.px=px;tmp.py=py;tmp.bx=bx;tmp.by=by;
    q.push(tmp);
    while(!q.empty())
    {   now=q.top();q.pop();
        if(now.bx==edx&&now.by==edy)return now;
        vis[now.px][now.py][now.bx][now.by]=1;
        for(int i=0;i<=3;i++)
           {tmp=now;tmp.px+=dx[i];tmp.py+=dy[i];
            if(!vis[tmp.px][tmp.py][tmp.bx][tmp.by]&&tmp.px>=1&&tmp.py>=1&&tmp.px<=r&&tmp.py<=c&&mp[tmp.px][tmp.py])
            {
             if(tmp.bx==tmp.px&&tmp.by==tmp.py)
               {tmp.bx+=dx[i];tmp.by+=dy[i];
                if(!vis[tmp.px][tmp.py][tmp.bx][tmp.by]&&tmp.bx>=1&&tmp.by>=1&&tmp.bx<=r&&tmp.by<=c&&mp[tmp.bx][tmp.by])
                {tmp.cnt++;tmp.foot+=dir[i]-('a'-'A');q.push(tmp);vis[tmp.px][tmp.py][tmp.bx][tmp.by]=1;}
               }
             else 
               {tmp.foot+=dir[i];vis[tmp.px][tmp.py][tmp.bx][tmp.by]=1;q.push(tmp);}  
            }
           }
    }
    tmp.foot="Impossible.";
    return tmp;
} 
int main()
{   //freopen("in.txt","r",stdin);
    int px,py,bx,by,i,j,cas=0;char tmp;
    while(scanf("%d%d",&r,&c))
    {   if(r==0&&c==0)break;
        memset(mp,0,sizeof mp);
        memset(vis,0,sizeof vis);
        for(i=1;i<=r;i++)
           for(j=1;j<=c;j++)
              {scanf(" %c",&tmp);
                if(tmp=='.')mp[i][j]=1;
                else if(tmp=='T')edx=i,edy=j,mp[i][j]=1;
                else if(tmp=='B')bx=i,by=j,mp[i][j]=1;
                else if(tmp=='S')px=i,py=j,mp[i][j]=1;
              }
        printf("Maze #%d\n",++cas);
        state ans=bfs(px,py,bx,by); 
        cout<<ans.foot<<endl;
        printf("\n"); //这里俩换行很坑
    }
}

猜你喜欢

转载自blog.csdn.net/caoyang1123/article/details/78461782