zoukankan      html  css  js  c++  java
  • URAL 1119. Metro(BFS)

    点我看题目

    题意  : 这个人在左下角,地铁在右上角,由很多格子组成的地图,每一条边都是一条路,每一条边都是100米。还有的可以走对角线,问你从起点到终点最短是多少。

    Problem illustration

    思路 : 其实我想说一下,,,,,这个题基本都是用DP做的,这是为什么呢?好吧,这要从我最近要刷BFS题开始,于是二货自己很厉害的用BFS做完了,所以就给我推荐了,我其实没看出来能用BFS来做。。。。。。。

    //URAL 1119
    #include <iostream>
    #include <stdio.h>
    #include <queue>
    #include <math.h>
    #include <string.h>
    
    using namespace std;
    
    int n,m ;
    bool ch[1100][1100] ;
    bool vis[1100][1100] ;
    int dir[2][2] = {{0,1},{1,0}} ;
    int k ;
    double sum = 0.0 ;
    struct node
    {
        int x ;
        int y;
        double step ;
    } s,e,temp;
    queue<node>que ;
    
    void BFS()
    {
        que.push(s) ;
        s.step = 0.0 ;
        memset(vis,false ,sizeof(vis)) ;
        vis[s.x][s.y] = true ;
        while(!que.empty())
        {
            temp = que.front() ;
            que.pop() ;
            if(temp.x == e.x && temp.y == e.y)
            {
                sum = temp.step ;
                return  ;
            }
            for(int i = 0 ; i < 2 ; i++)
            {
                int xx = temp.x+dir[i][1] ;
                int yy = temp.y+dir[i][0] ;
                if(xx >= 0 && yy >= 0 && xx <= n && yy <= m && !vis[xx][yy])
                {
                    node temp1 ;
                    temp1.x = xx ;
                    temp1.y = yy ;
                    temp1.step = temp.step+100.0 ;
                    que.push(temp1) ;
                    vis[xx][yy] = true ;
                }
            }
            if(ch[temp.x+1][temp.y+1])
            {
                vis[temp.x+1][temp.y+1] = true ;
                node temp1 ;
                temp1.x = temp.x+1 ;
                temp1.y = temp.y+1 ;
                temp1.step = temp.step+sqrt(2.0)*100.0 ;
                que.push(temp1) ;
            }
        }
    
    
    }
    int main()
    {
        scanf("%d %d",&n,&m) ;
        scanf("%d",&k) ;
        int u,v ;
        memset(ch,false,sizeof(ch)) ;
        for(int i = 0 ; i < k ; i++)
        {
            scanf("%d %d",&u,&v) ;
            ch[u][v] = true ;
        }
        s.x = 0 , s.y = 0 ;
        e.x = n,e.y = m ;
        BFS() ;
        printf("%.0lf
    ",sum) ;
        return 0;
    }
    View Code
  • 相关阅读:
    NGINX-HTTPS
    README
    SSH
    Ubuntu
    Python复利
    Python全双工聊天
    Python半双工聊天
    Python网络编程
    使用Python PIL库中的Image.thumbnail函数裁剪图片
    Python模块 os.walk
  • 原文地址:https://www.cnblogs.com/luyingfeng/p/3624077.html
Copyright © 2011-2022 走看看