poj 3626 Mud Puddles (简单BFS)

来源:互联网 发布:吉林大学网络教育学费 编辑:程序博客网 时间:2024/05/17 01:11
Mud Puddles
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 2743 Accepted: 1607

Description

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.

Input

* 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

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

Sample Input

1 2 70 2-1 33 11 14 2-1 12 2

Sample Output

11

Source

USACO 2007 December Silver
 
AC掉了的代码:

#include<iostream>
#include<cstring>
#include<queue>
using namespace std;

int x,y,n,vs[1001][1001];
int dx[4]={0,0,1,-1};
int dy[4]={1,-1,0,0};
struct Node{
    int x;
    int y;
    int counter;
};

void bfs()
{
    int i;//用于选择4个方向;
    queue<Node>Q;//创建一个队列;
    Node p,q;
    p.x=500;
    p.y=500;
    p.counter=0;
    vs[p.x][p.y]=1;
    Q.push(p);//入队;
    while(!Q.empty())//当队列不为空的时候循环;
    {
        p=Q.front();
        Q.pop();
        for(i=0; i<4; i++)
        {
            q.x=p.x+dx[i];
            q.y=p.y+dy[i];
            q.counter=p.counter+1;
            if(q.x>=0 && q.x<=1000 && q.y>=0  && q.y<=1000 && vs[q.x][q.y]==0)
            {
                if(q.x==x && q.y==y)
                {
                    cout<<q.counter<<endl;
                    return;
                }
                vs[q.x][q.y]=1;
                Q.push(q);
            }
        }
    }
}

int main()
{
    int i,c,d;
    cin>>x>>y>>n;
    memset(vs, 0, sizeof(vs));
    x+=500;
    y+=500;
    for(i=0; i<n; i++)
    {
        cin>>c>>d;
        vs[c+500][d+500]=1;
    }
    if(x==500 && y==500)
        cout<<0<<endl;
    else
        bfs();
    return 0;
}

原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 地震来了怎么办60字 如果迷路了你会怎么办 吃多了抽烟想吐怎么办 晚上牙疼得要命怎么办 楼梯被火封锁后怎么办 牙齿疼怎么办能快速不疼 我被短信轰炸了怎么办 火警响了在家该怎么办 痔疮肉球变大了怎么办 痔疮长了好几个怎么办 苹果7手机丢了怎么办 如果油锅着火了怎么办 家里电气著火了怎么办 你家油锅起火了怎么办 交通事故后对方不肯去处理怎么办 租的车出了事故怎么办 借的车出了事故怎么办 台风来了怎么办小知识 台中班台风来了怎么办 地震时在五楼怎么办 小事故对方不来怎么办 发生交通事故对方不处理怎么办 当地震来了该怎么办 地震来了该怎么办教案 地震来了怎么办的问题 住30楼的地震了怎么办 在家里地震来了怎么办? 乐高地震来了怎么办? 地震来了该怎么办300字 地震来了该怎么办200 外地在北京生孩子建档怎么办 怀孕2个月头疼怎么办 怀孕6个月头疼怎么办 轮胎扎了个钉子怎么办 顶菅遇到填土区怎么办 12墙没砌在梁上怎么办 孕32周胎盘二级怎么办 孕36周胎盘三级怎么办 孕24周胎盘一级怎么办 孕24周 胎盘1级怎么办 抗核抗体1:1000怎么办