zoj 1649 BFS

简介: // zoj 1649#include<stdio.h>#include<queue>#include<string.h>using namespace std;#define MAXN 200#define INF 1000000struct point{ int x,y; int step,time;};queue
// zoj 1649
#include<stdio.h>
#include<queue>
#include<string.h>
using namespace std;
#define MAXN 200
#define INF 1000000
struct point
{
    int x,y;
    int step,time;
};
queue<point> Q;
int N,M,ax,ay;
char map[MAXN][MAXN];
int time[MAXN][MAXN];
int dir[4][2]={{-1,0},{1,0},{0,-1},{0,1}};
int bfs(point s)
{
    int i;
    Q.push(s);
    point jk;
    while(!Q.empty())
    {
        jk=Q.front();
        Q.pop();
        for(i=0; i<4;i++)
        {
            int x=jk.x+dir[i][0];
            int y=jk.y+dir[i][1];
            if(x>=0&&x<=N-1&&y>=0&&y<=M-1&&map[x][y]!='#')
            {
                point t;
                t.x=x;
                t.y=y;
                t.step=jk.step+1;
                t.time=jk.time+1;
                if(map[x][y]=='x')
                    t.time++;
                if(t.time<time[x][y])
                {
                    time[x][y]=t.time;
                    Q.push(t);
                }
            }
        }
    }
    return time[ax][ay];
}
int main()
{
    int i,j;
    //freopen("input.txt","r",stdin);
    while(scanf("%d%d",&N,&M)!=EOF)
    {
        memset(map,0,sizeof(map));
        for(i=0; i<N; i++)
            scanf("%s",map[i]);
        int sx,sy;
        point start;
        for(i=0; i<N; i++)
            for(j=0; j<M; j++)
            {
                time[i][j]=INF;
                if(map[i][j]=='a')
                {
                    ax=i;
                    ay=j;
                }
                else if(map[i][j]=='r')
                {
                    sx=i;
                    sy=j;
                }
            }
        start.x=sx;
        start.y=sy;
        start.step=0;
        start.time=0;
        int min=bfs(start);
        if(min<INF)
            printf("%d\n",min);
        else printf("Poor ANGEL has to stay in the prison all his life.\n");
    }
    return 0;
}

目录
相关文章
|
算法
1091 zoj Knight Moves的BFS算法和DFS
1091 zoj Knight Moves的BFS算法和DFS
70 0
|
图形学 C++
ZOJ1117 POJ1521 HDU1053 Huffman编码
Huffman编码的思想就是贪心,我们这里使用stl里的优先队列,priority_queue使用堆进行优化,虽然自己也可以写一个堆,但我感觉对于这道题有点主次不分了,再次感觉到stl确实是一个很强大的东西。
57 0
HDOJ 1081(ZOJ 1074) To The Max(动态规划)
HDOJ 1081(ZOJ 1074) To The Max(动态规划)
86 0
HDOJ 1081(ZOJ 1074) To The Max(动态规划)
|
机器学习/深度学习
POJ 1775 (ZOJ 2358) Sum of Factorials
POJ 1775 (ZOJ 2358) Sum of Factorials
151 0
|
人工智能 机器学习/深度学习
POJ 1775 (ZOJ 2358) Sum of Factorials
Description John von Neumann, b. Dec. 28, 1903, d. Feb. 8, 1957, was a Hungarian-American mathematician who made important contributions t...
1156 0
最短路径-zoj-2797
zoj-2797-106 miles to Chicago In the movie "Blues Brothers", the orphanage where Elwood and Jack were raised may be sold to the Board of Education if they do not pay 5000 dollars in taxes at the 
1192 0
最小生成树-并查集-Kruskal-zoj-2048-special judge
Highways description The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has a very poor system of public highways. The Flatopian government is aware of this problem and has
1205 0

热门文章

最新文章