zoukankan      html  css  js  c++  java
  • Atcoder Beginner Contest151D(迷宫问题求任意两点最短路径的最大值,BFS)

    BFS可以求得最短路,DFS会找到从当前点到图中叶子结点的路径。

     1 #define HAVE_STRUCT_TIMESPEC
     2 #include<bits/stdc++.h>
     3 using namespace std;
     4 int n,m,ans;
     5 char s[25][25];
     6 bool vis[25][25];
     7 struct node{
     8     int x,y,dis;
     9 };
    10 int direction[4][2]={{1,0},{0,1},{-1,0},{0,-1}};
    11 void bfs(int x,int y){
    12     if(s[x][y]=='#')
    13         return ;
    14     memset(vis,0,sizeof(vis));
    15     queue<node>q;
    16     q.push((node){x,y,0});
    17     vis[x][y]=1;
    18     while(!q.empty()){
    19         node now=q.front();
    20         q.pop();
    21         ans=max(ans,now.dis);
    22         for(int i=0;i<4;++i){
    23             int temp_x=now.x+direction[i][0],temp_y=now.y+direction[i][1];
    24             if(temp_x<=0||temp_x>n||temp_y<=0||temp_y>m||s[temp_x][temp_y]=='#'||vis[temp_x][temp_y])
    25                 continue;
    26             q.push((node){temp_x,temp_y,now.dis+1});
    27             vis[temp_x][temp_y]=1;
    28         }
    29     }
    30 }
    31 int main(){
    32     ios::sync_with_stdio(false);
    33     cin.tie(NULL);
    34     cout.tie(NULL);
    35     cin>>n>>m;
    36     for(int i=1;i<=n;++i)
    37         cin>>s[i]+1;
    38     for(int i=1;i<=n;++i)
    39         for(int j=1;j<=m;++j)
    40             bfs(i,j);
    41     cout<<ans;
    42     return 0;
    43 }
    保持热爱 不懈努力 不试试看怎么知道会失败呢(划掉) 世上无难事 只要肯放弃(划掉)
  • 相关阅读:
    HDU5171 GTY's birthday gift —— 矩阵快速幂
    UVA10870 Recurrences —— 矩阵快速幂
    HDU4965 Fast Matrix Calculation —— 矩阵乘法、快速幂
    UVA11551 Experienced Endeavour —— 矩阵快速幂
    D6 I
    亲和串
    Kmp 算法模板 C
    Buy the Ticket
    寒假D3 A Find the Lost Sock
    寒假 D3 D Modular Inverse
  • 原文地址:https://www.cnblogs.com/ldudxy/p/12189149.html
Copyright © 2011-2022 走看看