BFS (shortest path)

Links: https://ac.nowcoder.com/acm/contest/993/F
Source: Cattle-off network

Farmer John is leaving his house promptly at 6 AM for his daily milking of Bessie. However, the previous evening saw a heavy rain, and the fields are quite muddy. FJ starts at the point (0, 0) in the coordinate plane and heads toward Bessie who is located at (X, Y) (-500 ≤ X ≤ 500; -500 ≤ Y ≤ 500). He can see all N (1 ≤ N ≤ 10,000) puddles of mud, located at points (Ai, Bi) (-500 ≤ Ai ≤ 500; -500 ≤ Bi ≤ 500) on the field. Each puddle occupies only the point it is on.
Having just bought new boots, Farmer John absolutely does not want to dirty them by stepping in a puddle, but he also wants to get to Bessie as quickly as possible. He's already late because he had to count all the puddles. If Farmer John can only travel parallel to the axes and turn at points with integer coordinates, what is the shortest distance he must travel to reach Bessie and keep his boots clean? There will always be a path without mud that Farmer John can take to reach Bessie.

Enter a description:

* Line 1: Three space-separate integers: X, Y, and N.
* Lines 2..N+1: Line i+1 contains two space-separated integers: Ai and Bi

Output Description:

* Line 1: The minimum distance that Farmer John has to travel to reach Bessie without stepping in mud.
Example 1

Entry

copy
1 2 7
0 2
-1 3
3 1
1 1
4 2
-1 1
2 2

Export

copy
11

Explanation

. Bessie is at (1, 2 ) Farmer John sees 7 mud puddles, located at (0, 2); (-1, 3); (3, 1); (1, 1); (4, 2); ( -1,. 1) and (2, 2). 
of The Best path for Farmer, John IS (0, 0); (-1, 0); (-2, 0); (-2,. 1); (-2, 2); (-2, 3) ; (-2, 4); (-1, 4); (0, 4); (0, 3); (1, 3); and (1, 2), finally reaching Bessie.

ideas: bfs Note: this problem coordinates will appear negative, so the flag array to unify plus 500 into a positive number.
#include <iostream>
#include <iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>
#include <stdio.h>
#include <string.h>
#include <stack>
#include <vector>
#include <queue>
#include <set>
using namespace std;
const int N = 500 ;
int   x[10009] , y[10009] , vis[1009][1009];
int len1 , len2 , dir[4][2] = {{1 , 0} , {-1 , 0}, {0 , 1} ,{0 , -1}};
int mx , my , p;

struct Node{
    int x, y , ans ;
    Node(int x = 0, int y = 0 , int ans = 0){
        this->x = x ;
        this->y = y ;
        this->ans = ans ;//记录步数
    }
}n[10009];

bool check(int x , int y)
{
    if(!vis[x + N][y + N] && x + 500 <= 1000 && x + 500 >= 0 && y + 500 <= 1000 && y + 500 >= 0)
        return true ;
    else
        return false ;
}

int bfs(int x , int y)
{
    queue<Node>q;
    q.push(Node(x , y , 0));
    vis[x + N][y + N] = 1 ;
    Node temp , step ;
    while(!q.empty())
    {

        temp = q.front();
        q.pop() ;
        if(temp.x == mx && temp.y == my)
        {
            return temp.ans ;
        }
        for(int i = 0 ; i < 4 ; i++)
        {
            step.x = temp.x + dir[i][0];
            step.y = temp.y + dir[i][1];
            step.ans = temp.ans + 1 ;
            if(check(step.x , step.y))
            {
                vis[step.x + N][step.y + N] = 1 ;
                q.push(Node(step));
            }
        }

    }

}

void init()
{
    memset(vis , 0 , sizeof(vis));
}


int main()
{
    while(~scanf("%d%d%d" , &mx , &my , &p))
    {
        init() ;
        for(int i = 0 ; i < p ; i++)
        {
            scanf("%d%d" , &n[i].x , &n[i].y);
            vis[n[i].x + N][n[i].y + N] = 1 ;
        }
        cout << bfs(0 , 0) <<endl ;

    }


    return 0;
}

 

 

Guess you like

Origin www.cnblogs.com/nonames/p/11240515.html