首页 > 其他分享 >POJ - 3626 Mud Puddles (poj绝对有毒)

POJ - 3626 Mud Puddles (poj绝对有毒)

时间:2023-02-07 12:04:56浏览次数:62  
标签:x1 int 3626 Puddles Mud y1 Bessie include 500


Mud Puddles

Time Limit: 1000MS

 

Memory Limit: 65536K

Total Submissions: 3642

 

Accepted: 2016

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 (XY) (-500 ≤ X ≤ 500; -500 ≤ Y ≤ 500). He can see all N (1 ≤ N ≤ 10,000) puddles of mud, located at points (AiBi) (-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: XY, 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 7 0 2 -1 3 3 1 1 1 4 2 -1 1 2 2


Sample Output


11


Source

​USACO 2007 December Silver​

 

题意:

n个水坑,从(0,0)到(x,y)的最短距离。

分析:

简单的dfs

这题尽然卡队列,队列必须发在函数体外面(主函数)也行。

 

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<utility>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
#include<bitset>
#define EPS 1e-9
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define LL long long
const int MOD = 1E9+7;
const int N = 1000+5;
const int dx[] = {-1,1,0,0,-1,-1,1,1};
const int dy[] = {0,0,-1,1,-1,1,-1,1};
using namespace std;
const int maxC=500;
int n,m;
int st,en;
int vis[N][N];
int mapp[N][N];
struct node
{
int x,y,step;
}a,b;
queue<node> q; //必须要移动到外面
int bfs()
{
memset(vis,0,sizeof(vis));

a.x=500;
a.y=500;
a.step=0;

q.push(a);

while(!q.empty())
{
node u=q.front();
q.pop();
for(int i=0;i<4;i++)
{
int x1=u.x+dx[i];
int y1=u.y+dy[i];

if(vis[x1][y1]==0&&mapp[x1][y1]==0)
{
if(x1==st&&y1==en)
{
return u.step+1;
}
vis[x1][y1]=1;

node v;
v.step=u.step+1;
v.x=x1;
v.y=y1;
q.push(v);
}

}
}
}
int main()
{
scanf("%d%d%d",&st,&en,&n);
st+=500;
en+=500;
memset(mapp,0,sizeof(mapp));
for(int i=1;i<=n;i++)
{
int x,y;
scanf("%d%d",&x,&y);
x+=500;
y+=500;
mapp[x][y]=-1;
}
printf("%d",bfs());

return 0;
}

 

标签:x1,int,3626,Puddles,Mud,y1,Bessie,include,500
From: https://blog.51cto.com/u_14932227/6041847

相关文章

  • git Smudge error: Error downloading object
    项目中使用了gitlfs存储文件,使用gitclone复制代码,发现被gitlfs接管的文件检出失败。上面显示的 *.pdf文件是我之前用gitlfstrack过的文件,现在重新拉取项目,......
  • Muduo库之Buffer
    Buffer类的设计是一种应用层的输入输出缓冲技术。对于Non-blockingIO来说,其核心思想是避免阻塞在read()或write()或其他的IO系统调用上,这样就可以最大限度地复用......
  • Muduo库之Endian、SocketsOps、InetAddress和Socket
    EndianEndian.h是一个公共头文件,里面包含了一些网络字节序和主机字节序相互转换的问题。其中所使用的方法如下://XX位主机转网络uint64_thtobe64(uint64_tdata);u......
  • Muduo库之线程
    Thread在Thread.cc中,有一个ThreadNameInitializer类,用于线程环境初始化操作:voidafterFork(){muduo::CurrentThread::t_cachedTid=0;muduo::CurrentThread:......
  • Muduo库之阻塞队列
    在并发编程中,常常需要用到线程安全的队列。常见的线程安全队列的设计分为两种:阻塞队列:常用于生产者和消费者的场景,其中,生产者存放元素,而消费者获取元素。常用的实现方法......
  • Muduo库之异步日志
    该框架中的日志为诊断日志,用于将代码运行时的重要信息进行保存,方便故障诊断和追踪。日志通常分为如下两种:同步日志:当需要写出一条日志消息时,只有等到这条日志消息完全写......
  • Muduo库之同步原语
    Atomic在Atomic.h文件中定义了原子操作类类型AtomicIntegerT<T>,它使用了GCC内置的原子操作来实现。原子操作在多线程开发中经常用到,比如计数器、序列产生器等。这些......
  • Muduo库之WeakCallback、Singleton
    WeakCallback在WeakCallback.h文件中定义了模板类WeakCallback,在其模板参数中,有一个可变模板参数ARGS,用以指示回调函数的参数。在类内部,定义有两个成员变量,分别是ob......
  • Muduo库之StringPiece、Time
    StringPiece在StringPiece.h文件中,声明了两个类类型,一个是StringArg,另一个是StringPiece,前者用于在传递函数参数时同时兼容C风格的字符串(constchar*)和C++风格......
  • Muduo库之ProcessInfo、Exception、FileUtil
    ProcessInfo在ProcessInfo.h文件中,存在一个命名空间ProcessInfo,其中声明了大部分进程需要使用到相关信息的函数,比如进程的PID、UID、EUID,主机名,进程状态,程状态等等。......