zoukankan      html  css  js  c++  java
  • Problem 2285 迷宫寻宝 (BFS)

    题目链接:http://acm.fzu.edu.cn/problem.php?pid=2285

    Problem 2285 迷宫寻宝

    Accept: 323    Submit: 1247
    Time Limit: 1000 mSec    Memory Limit : 32768 KB

    Problem Description

    洪尼玛今天准备去寻宝,在一个n*n (n行, n列)的迷宫中,存在着一个入口、一些墙壁以及一个宝藏。由于迷宫是四连通的,即在迷宫中的一个位置,只能走到与它直接相邻的其他四个位置(上、下、左、右)。现洪尼玛在迷宫的入口处,问他最少需要走几步才能拿到宝藏?若永远无法拿到宝藏,则输出-1。

    Input

    多组测试数据。

    每组数据输入第一行为正整数n,表示迷宫大小。

    接下来n行,每行包括n个字符,其中字符'.'表示该位置为空地,字符'#'表示该位置为墙壁,字符'S'表示该位置为入口,字符'E'表示该位置为宝藏,输入数据中只有这四种字符,并且'S'和'E'仅出现一次。

    n≤1000

    Output

    输出拿到宝藏最少需要走的步数,若永远无法拿到宝藏,则输出-1。

    Sample Input

    5 S.#.. #.#.# #.#.# #...E #....

    Sample Output

    宽度优先搜索简单题,但是这题输入比较多需要用scanf

     1 #include <iostream>
     2 #include <algorithm>
     3 #include <cmath> 
     4 #include <string>
     5 #include <cstdio> 
     6 #include <cstring>
     7 #include <queue>
     8 using namespace std;
     9 const int INF=0x3f3f3f3f;
    10 typedef pair<int,int> P;
    11 char a[1005][1005];
    12 int n;
    13 int dx[4]={1,-1,0,0},dy[4]={0,0,1,-1};
    14 int sx,sy,gx,gy;
    15 int d[1005][1005];
    16 int bfs()
    17 {
    18     //for(int i=0;i<n;i++){
    19     //    for(int j=0;j<n;j++){
    20     //        d[i][j]=INF;
    21     //    }
    22     //}
    23     memset(d,INF,sizeof(d));//上面的简写 
    24     d[sx][sy]=0;
    25     queue<P> que;
    26     que.push(P(sx,sy));
    27     while(!que.empty()){
    28         P p=que.front();
    29         que.pop();
    30         int x=p.first,y=p.second;
    31         if(x==gx&&y==gy) break;
    32         for(int i=0;i<4;i++){
    33             int nx=x+dx[i],ny=y+dy[i];
    34             if(nx>=0&&nx<n&&ny>=0&&ny<n&&a[nx][ny]!='#'&&d[nx][ny]==INF){
    35                 que.push(P(nx,ny));
    36                 d[nx][ny]=d[x][y]+1;
    37             }
    38         }
    39     }
    40     if(d[gx][gy]==INF) return -1;
    41     else return d[gx][gy];
    42 }
    43 int main()
    44 {
    45     while(scanf("%d",&n)!=EOF){
    46         for(int i=0;i<n;i++){
    47             scanf("%s",a[i]);
    48         }
    49         for(int i=0;i<n;i++){
    50             for(int j=0;j<n;j++){
    51                 if(a[i][j]=='S'){
    52                     sx=i,sy=j;
    53                 }
    54                 if(a[i][j]=='E'){
    55                     gx=i,gy=j;
    56                 }
    57             }
    58         }
    59         cout<<bfs()<<endl;
    60     }
    61     return 0;
    62 }
  • 相关阅读:
    在 Flink 算子中使用多线程如何保证不丢数据?
    日处理数据量超10亿:友信金服基于Flink构建实时用户画像系统的实践
    Java编码技巧之高效代码50例
    codeforces 1284D. New Year and Conference(线段树)
    codeforces 1284C. New Year and Permutation(组合数学)
    codeforces 1284B. New Year and Ascent Sequence(二分)
    Codeforces Hello2020 A-E简要题解
    POJ2456 Aggressive cows(二分)
    POJ3122 Pie(二分)
    POJ3258 River Hopscotch(二分最大化最小值)
  • 原文地址:https://www.cnblogs.com/shixinzei/p/10747283.html
Copyright © 2011-2022 走看看