程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> zoj1649 BFS

zoj1649 BFS

編輯:C++入門知識

--------------------------------------------------------------------------------

Time Limit: 2 Seconds      Memory Limit: 65536 KB

--------------------------------------------------------------------------------

Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is described as a N * M (N, M <= 200) matrix. There are WALLs, ROADs, and GUARDs in the prison.

Angel's friends want to save Angel. Their task is: approach Angel. We assume that "approach Angel" is to get to the position where Angel stays. When there's a guard in the grid, we must kill him (or her?) to move into the grid. We assume that we moving up, down, right, left takes us 1 unit time, and killing a guard takes 1 unit time, too. And we are strong enough to kill all the guards.

You have to calculate the minimal time to approach Angel. (We can move only UP, DOWN, LEFT and RIGHT, to the neighbor grid within bound, of course.)


Input

First line contains two integers stand for N and M.

Then N lines follows, every line has M characters. "." stands for road, "a" stands for Angel, and "r" stands for each of Angel's friend.

Process to the end of the file.


Output

For each test case, your program should output a single integer, standing for the minimal time needed. If such a number does no exist, you should output a line containing "Poor ANGEL has to stay in the prison all his life."


Sample Input

7 8
#.#####.
#.a#..r.
#..#x...
..#..#.#
#...##..
.#......
........


Sample Output

13

 

        因為普通的BFS是按照步數優先來求解的,但對於本題來說,步數最少的解確不一定是最優解。如:

2 5

axxxr

.....

顯然按照普通的BFS求解的話,結果是7(即筆直向左走到頭),但最優解應該是6。為了解決這個矛盾,我們可以另設一個數組,記錄從起點走到當前位置的最少時間,然後在BFS的過程中,只要從當前位置走到下一個位置的時間小於下一個位置的最小時間,就入隊。此處還可以做一個優化,為了避免同一個節點重復入隊,我們用優先隊列,按照時間由小到大的順序擴展。

[cpp]  #include <iostream>  
#include<cstdio>  
#include<queue>  
 
using namespace std; 
struct st 

    int x,y; 
    int time; 
    bool operator<(const st t)const 
    { 
        return time>t.time; 
    } 
}cur,next; 
char map[205][205]; 
int mt[205][205];//記錄最少時間  
int dir[4][2]={{0,1},{0,-1},{1,0},{-1,0}}; 
int n,m; 
 
bool path(int x,int y) 

    if(x>=0&&x<n&&y>=0&&y<m&&map[x][y]!='#') 
        return true; 
    return false; 

 
int BFS(int sx,int sy) 

    priority_queue<st>q; 
    cur.x=sx; 
    cur.y=sy; 
    cur.time=0; 
    mt[sx][sy]=0; 
    q.push(cur); 
    while(!q.empty()) 
    { 
        cur=q.top(); 
        q.pop(); 
        if(map[cur.x][cur.y]=='a') 
            return cur.time; 
        for(int i=0;i<4;i++) 
        { 
            next.x=cur.x+dir[i][0]; 
            next.y=cur.y+dir[i][1]; 
            next.time=cur.time+1; 
            if(map[next.x][next.y]=='x') 
                next.time++; 
            if(path(next.x,next.y)&&next.time<mt[next.x][next.y]) 
            { 
                mt[next.x][next.y]=next.time; 
                q.push(next); 
            } 
        } 
    } 
    return -1; 

 
int main() 

    int i,j,sx,sy; 
    while(~scanf("%d%d",&n,&m)) 
    { 
        for(i=0;i<n;i++) 
            scanf("%s",map[i]); 
        for(i=0;i<n;i++) 
            for(j=0;j<m;j++) 
            { 
                if(map[i][j]=='r') 
                { 
                    sx=i; 
                    sy=j; 
                } 
                mt[i][j]=100000000; 
            } 
        int res=BFS(sx,sy); 
        if(res==-1) 
            printf("Poor ANGEL has to stay in the prison all his life.\n"); 
        else 
            printf("%d\n",res); 
    } 
    return 0; 

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

using namespace std;
struct st
{
    int x,y;
    int time;
    bool operator<(const st t)const
    {
        return time>t.time;
    }
}cur,next;
char map[205][205];
int mt[205][205];//記錄最少時間
int dir[4][2]={{0,1},{0,-1},{1,0},{-1,0}};
int n,m;

bool path(int x,int y)
{
    if(x>=0&&x<n&&y>=0&&y<m&&map[x][y]!='#')
        return true;
    return false;
}

int BFS(int sx,int sy)
{
    priority_queue<st>q;
    cur.x=sx;
    cur.y=sy;
    cur.time=0;
    mt[sx][sy]=0;
    q.push(cur);
    while(!q.empty())
    {
        cur=q.top();
        q.pop();
        if(map[cur.x][cur.y]=='a')
            return cur.time;
        for(int i=0;i<4;i++)
        {
            next.x=cur.x+dir[i][0];
            next.y=cur.y+dir[i][1];
            next.time=cur.time+1;
            if(map[next.x][next.y]=='x')
                next.time++;
            if(path(next.x,next.y)&&next.time<mt[next.x][next.y])
            {
                mt[next.x][next.y]=next.time;
                q.push(next);
            }
        }
    }
    return -1;
}

int main()
{
    int i,j,sx,sy;
    while(~scanf("%d%d",&n,&m))
    {
        for(i=0;i<n;i++)
            scanf("%s",map[i]);
        for(i=0;i<n;i++)
            for(j=0;j<m;j++)
            {
                if(map[i][j]=='r')
                {
                    sx=i;
                    sy=j;
                }
                mt[i][j]=100000000;
            }
        int res=BFS(sx,sy);
        if(res==-1)
            printf("Poor ANGEL has to stay in the prison all his life.\n");
        else
            printf("%d\n",res);
    }
    return 0;
}

 

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved