zoukankan      html  css  js  c++  java
  • bfs(同一最短路径)

    http://oj.jxust.edu.cn/contest/Problem?id=1702&pid=7

    题意:现在有两个相同大小的地图,左上角为起点,右下角问终点。问是否存在同一条最短路径。最短距离一样,他们走的路径也一样。

    n 行 m 列(1 <= n , m <= 500)

    存在就输出YES , 否则NO;

    解法:三个bfs (其中一个是合并的图),判断三个最短路径是否相等且不为-1(不存在)。

    //#include <bits/stdc++.h>
    #include <cstdio>
    #include <cstring>
    #include <cmath>
    #include <algorithm>
    #include <iostream>
    #include <string>
    #include <stdio.h>
    #include <queue>
    #include <stack>
    #include <map>
    #include <set>
    #include <string.h>
    #include <vector>
    #define ME(x , y) memset(x , y , sizeof(x))
    #define SF(n) scanf("%d" , &n)
    #define rep(i , n) for(int i = 0 ; i < n ; i ++)
    #define INF  0x3f3f3f3f
    #define mod 20191117
    #define PI acos(-1)
    using namespace std;
    typedef long long ll ;
    char s[509][509] , str[509][509];
    int vis[509][509];
    int dir[4][2] = {{1 , 0} , {-1 , 0} , {0 , 1} , {0 , -1}};
    int n , m ;
    
    struct node
    {
        int x , y , w ;
    };
    
    
    int bfs1(int x , int y , int w)
    {
        node a , b ;
        a.x = x , a.y = y , a.w = w ;
        queue<node>q;
        q.push(a);
        vis[x][y] = 1 ;
        while(!q.empty())
        {
            a = q.front();
            q.pop();
            if(a.x == n - 1 && a.y == m -1)
            {
                return a.w ;
            }
            for(int i = 0 ; i < 4 ; i++)
            {
                b.x = a.x + dir[i][0];
                b.y = a.y + dir[i][1];
                b.w = a.w + 1 ;
                if(b.x < 0 || b.x >= n || b.y < 0 || b.y >= m || vis[b.x][b.y] || s[b.x][b.y] == '#')
                {
                    continue ;
                }
                vis[b.x][b.y] = 1 ;
                q.push(b);
            }
        }
        return -1;
    }
    
    int bfs2(int x , int y , int w)
    {
        node a , b ;
        a.x = x , a.y = y , a.w = w ;
        queue<node>q;
        q.push(a);
        vis[x][y] = 1 ;
        while(!q.empty())
        {
            a = q.front();
            q.pop();
            if(a.x == n - 1 && a.y == m -1)
            {
                return a.w ;
            }
            for(int i = 0 ; i < 4 ; i++)
            {
                b.x = a.x + dir[i][0];
                b.y = a.y + dir[i][1];
                b.w = a.w + 1 ;
                if(b.x < 0 || b.x >= n || b.y < 0 || b.y >= m || vis[b.x][b.y] || str[b.x][b.y] == '#')
                {
                    continue ;
                }
                vis[b.x][b.y] = 1 ;
                q.push(b);
            }
        }
        return -1;
    }
    
    int main()
    {
    
        scanf("%d%d" , &n , &m);
        for(int i = 0 ; i < n ; i++)
        {
            for(int j = 0 ; j < m ; j++)
            {
                cin >> s[i][j] ;
            }
        }
        int ans1 = bfs1(0 , 0 , 0);
        for(int i = 0 ; i < n ; i++)
        {
            for(int j = 0 ; j < m ; j++)
            {
                cin >> str[i][j];
                if(str[i][j] == '#' && s[i][j] == '*')
                {
                    s[i][j] = '#';
                }
            }
        }
        memset(vis , 0 , sizeof(vis));
        int ans2 = bfs2(0 , 0 , 0) ;
        memset(vis , 0 , sizeof(vis));
        int ans3 = bfs1(0 , 0 , 0) ;
        if(ans1 == ans2 && ans2 == ans3 && ans1 != -1)
        {
            printf("YES
    ");
        }
        else{
            printf("NO
    ");
        }
    
        return 0;
    }
  • 相关阅读:
    ES7/ES8 语法学习
    JavaScript中this对象原理简洁说明
    浅谈http协议
    各个浏览器之间常见的兼容性问题
    npm -v 报错:cannot find module 'core-util-is'
    对象遍历的几种方法
    Vue项目如何关闭Eslint检测
    axios 基本运用
    如何去掉vue路由中的#
    vue 父子组件、兄弟组件传值
  • 原文地址:https://www.cnblogs.com/nonames/p/11923149.html
Copyright © 2011-2022 走看看