poj 2688

      一道搜索题,用了两种方法:一种BFS+优先队列优化,950ms+ 险过,另一种是BFS+DFS,效率还不错,不到300ms。

     第一种方法,主要是用状态压缩记录已走过的路径,然后就是常见的BFS搜索;第二种方法,先用BFS求出任意两点之间的距离,然后用DFS搜索答案。

代码(G++)(第一种方法):

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>

#define MAX 21
using namespace std;

const int dir[4][2]={{-1,0},{1,0},{0,1},{0,-1}};
struct Node{
    int x;
    int y;
    int path;
    int t;
    bool operator< (Node n) const
    {
        return t>n.t;
    }
};

char g[MAX][MAX];
int h,w,time[MAX][MAX][1<<10];

int bfs(int bx,int by,int state)
{
    priority_queue<Node> qn;
    Node node;
    int x,y,nx,ny,i,path,d;

    time[bx][by][0]=0;
    qn.push((Node){bx,by,0,0});
    while(!qn.empty())
    {
        node=qn.top();
        qn.pop();
        x=node.x;
        y=node.y;

        if(node.path==state) return node.t;

        for(i=0;i<4;i++)
        {
            path=node.path;
            nx=x+dir[i][0];
            ny=y+dir[i][1];
            if(nx>=0&&ny>=0&&nx<h&&ny<w&&g[nx][ny]!='x'){
                if(g[nx][ny]>='0'&&g[nx][ny]<='9'){
                    d=g[nx][ny]-'0';
                    path|=1<<d;
                }
                if(time[nx][ny][path]==-1||time[nx][ny][path]>node.t+1){
                    time[nx][ny][path]=node.t+1;
                    qn.push((Node){nx,ny,path,node.t+1});
                }
            }
        }

    }
    return -1;
}

int main()
{
    //freopen("in.txt","r",stdin);

    int i,j,k,bx,by,ans,state;
    while(scanf("%d %d",&w,&h)&&h+w!=0)
    {
        k=state=0;
        for(i=0;i<h;i++)
        {
            scanf("%s",g[i]);
            for(j=0;j<w;j++)
            {
                if(g[i][j]=='o'){
                   bx=i;
                   by=j;
                }else if(g[i][j]=='*'){   //编号从0开始
                    g[i][j]='0'+k;
                    state|=1<<k;
                    k++;
                }
            }
        }

        memset(time,-1,sizeof(time));
        ans=bfs(bx,by,state);
        printf("%d\n",ans);
    }
    return 0;
}

代码(G++)(第二种方法):

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>

#define MAX 21
#define MAX_V 12
#define INF (1<<30)
using namespace std;

const int dir[4][2]={{-1,0},{1,0},{0,1},{0,-1}};
struct Node{
    int x;
    int y;
    int t;
};

char g[MAX][MAX];
int h,w,dist[MAX_V][MAX_V],ans,c;
bool vis[MAX][MAX],flag[MAX_V];

bool bfs(int x,int y,int k,int t)
{
    queue<Node> q;
    Node node;
    int i,nx,ny,p;

    p=1;
    dist[k][k]=0;
    vis[x][y]=true;
    q.push((Node){x,y,0});

    if(p==t) return true;

    while(!q.empty())
    {
        node=q.front();
        q.pop();

        for(i=0;i<4;i++)
        {
            nx=node.x+dir[i][0];
            ny=node.y+dir[i][1];
            if(nx>=0&&ny>=0&&nx<h&&ny<w&&g[nx][ny]!='x'){
                if(!vis[nx][ny]){
                    vis[nx][ny]=true;

                    if(g[nx][ny]>='0'&&g[nx][ny]<='9'){
                        dist[k][g[nx][ny]-'0']=dist[g[nx][ny]-'0'][k]=node.t+1;
                        p++;
                        if(t==p) return true;
                    }

                    q.push((Node){nx,ny,node.t+1});
                }
            }
        }
    }
    return false;
}

bool get_dist()
{
    int i,j,t;
    memset(dist,-1,sizeof(dist));
    t=0;
    for(i=0;i<h;i++)
    {
        for(j=0;j<w;j++)
        {
            if(g[i][j]>='0'&&g[i][j]<='9'){
                memset(vis,false,sizeof(vis));
                if(!bfs(i,j,g[i][j]-'0',c-t)) return false;
                t++;
                if(c==t) return true;
                g[i][j]='.';
            }
        }
    }
}

void dfs(int v,int d,int len)
{
    int i;

    if(d==0){
        ans=min(ans,len);
        return;
    }
    if(len+d>ans) return;   //剪枝

    for(i=0;i<c;i++)
    {
        if(!flag[i]){
            flag[i]=true;
            dfs(i,d-1,len+dist[v][i]);
            flag[i]=false;
        }
    }
}

int main()
{
    //freopen("in.txt","r",stdin);

    int i,j;
    while(scanf("%d %d",&w,&h)&&h+w!=0)
    {
        c=1;
        for(i=0;i<h;i++)
        {
            scanf("%s",g[i]);
            for(j=0;j<w;j++)
            {
                if(g[i][j]=='o'){
                    g[i][j]='0';
                }else if(g[i][j]=='*'){   //编号从0开始
                    g[i][j]='0'+c;
                    c++;
                }
            }
        }
        if(get_dist()){
            ans=INF;
            memset(flag,false,sizeof(flag));
            dfs(0,c,0);
            printf("%d\n",ans);
        }else printf("-1\n");
    }
    return 0;
}

题目( http://poj.org/problem?id=2688):

Cleaning Robot
Time Limit: 1000MS Memory Limit: 65536K
   

Description

Here, we want to solve path planning for a mobile robot cleaning a rectangular room floor with furniture. 

Consider the room floor paved with square tiles whose size fits the cleaning robot (1 * 1). There are 'clean tiles' and 'dirty tiles', and the robot can change a 'dirty tile' to a 'clean tile' by visiting the tile. Also there may be some obstacles (furniture) whose size fits a tile in the room. If there is an obstacle on a tile, the robot cannot visit it. The robot moves to an adjacent tile with one move. The tile onto which the robot moves must be one of four tiles (i.e., east, west, north or south) adjacent to the tile where the robot is present. The robot may visit a tile twice or more. 

Your task is to write a program which computes the minimum number of moves for the robot to change all 'dirty tiles' to 'clean tiles', if ever possible.

Input

The input consists of multiple maps, each representing the size and arrangement of the room. A map is given in the following format. 

w h 
c11 c12 c13 ... c1w 
c21 c22 c23 ... c2w 
... 
ch1 ch2 ch3 ... chw 

The integers w and h are the lengths of the two sides of the floor of the room in terms of widths of floor tiles. w and h are less than or equal to 20. The character cyx represents what is initially on the tile with coordinates (x, y) as follows. 

'.' : a clean tile 
'*' : a dirty tile 
'x' : a piece of furniture (obstacle) 
'o' : the robot (initial position) 

In the map the number of 'dirty tiles' does not exceed 10. There is only one 'robot'. 

The end of the input is indicated by a line containing two zeros.

Output

For each map, your program should output a line containing the minimum number of moves. If the map includes 'dirty tiles' which the robot cannot reach, your program should output -1.

Sample Input

7 5
.......
.o...*.
.......
.*...*.
.......
15 13
.......x.......
...o...x....*..
.......x.......
.......x.......
.......x.......
...............
xxxxx.....xxxxx
...............
.......x.......
.......x.......
.......x.......
..*....x....*..
.......x.......
10 10
..........
..o.......
..........
..........
..........
.....xxxxx
.....x....
.....x.*..
.....x....
.....x....
0 0

Sample Output

8
49
-1

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值