BFS...

---------------------------------------------------------------------------------------

#include<cstdio>
#include<algorithm>
#include<cstring>
#include<queue>
#include<iostream>
 
#define rep( i , n ) for( int i = 0 ; i < n ; i++ )
#define clr( x , c ) memset( x , c , sizeof( x ) )
 
using namespace std;
 
const int dir[ 4 ][ 2 ] = { { -1 , 0 } , { 1 , 0 } , { 0 , 1 } , { 0 , -1 } };
const int inf = 0x3f3f3f3f;
const int maxn = 10000 + 5;
const int maxm = 1000 + 5;
 
bool G[ maxm ][ maxm ];
 
int d[ maxm ][ maxm ];
 
struct Node {
int x , y , d;
};
 
queue< Node > Q;
 
int main() {
freopen( "test.in" , "r" , stdin );
clr( d , inf );
int n;
int goal[ 2 ];
cin >> goal[ 0 ] >> goal[ 1 ] >> n;
goal[ 0 ] += 500 , goal[ 1 ] += 500;
clr( G , -1 );
while( n-- ) {
int x , y;
scanf( "%d%d" , &x , &y );
G[ x + 500 ][ y + 500 ] = false;
}
Q.push( ( Node ) { 500 , 500 , d[ 500 ][ 500 ] = 0 } );
while( ! Q.empty() ) {
Node o = Q.front();
Q.pop();
int coor[ 2 ] = { o.x , o.y } , dist = o.d;
if( ! memcmp( coor , goal , sizeof( coor ) ) )
   break;
dist++;
rep( i , 4 ) {
rep( j , 2 ) coor[ j ] += dir[ i ][ j ];
if( G[ coor[ 0 ] ][ coor[ 1 ] ] && dist < d[ coor[ 0 ] ][ coor[ 1 ] ] )
Q.push( ( Node ) { coor[ 0 ] , coor[ 1 ] , d[ coor[ 0 ] ][ coor[ 1 ] ] = dist } );
rep( j , 2 ) coor[ j ] -= dir[ i ][ j ];
}
}
cout << d[ goal[ 0 ] ][ goal[ 1 ] ] << "\n";
return 0;
}

---------------------------------------------------------------------------------------

1627: [Usaco2007 Dec]穿越泥地

Time Limit: 5 Sec  Memory Limit: 64 MB
Submit: 522  Solved: 342
[Submit][Status][Discuss]

Description

清早6:00,Farmer John就离开了他的屋子,开始了他的例行工作:为贝茜挤奶。前一天晚上,整个农场刚经受过一场瓢泼大雨的洗礼,于是不难想见,FJ 现在面对的是一大片泥泞的土地。FJ的屋子在平面坐标(0, 0)的位置,贝茜所在的牛棚则位于坐标(X,Y) (-500 <= X <= 500; -500 <= Y <= 500)处。当然咯, FJ也看到了地上的所有N(1 <= N <= 10,000)个泥塘,第i个泥塘的坐标为 (A_i, B_i) (-500 <= A_i <= 500;-500 <= B_i <= 500)。每个泥塘都只占据了它所在的那个格子。 Farmer John自然不愿意弄脏他新买的靴子,但他同时想尽快到达贝茜所在的位置。为了数那些讨厌的泥塘,他已经耽搁了一些时间了。如果Farmer John 只能平行于坐标轴移动,并且只在x、y均为整数的坐标处转弯,那么他从屋子门口出发,最少要走多少路才能到贝茜所在的牛棚呢?你可以认为从FJ的屋子到牛棚总是存在至少一条不经过任何泥塘的路径。

Input

* 第1行: 3个用空格隔开的整数:X,Y 和 N

* 第2..N+1行: 第i+1行为2个用空格隔开的整数:A_i 和 B_i

Output

* 第1行: 输出1个整数,即FJ在不踏进泥塘的情况下,到达贝茜所在牛棚所需要 走过的最小距离

Sample Input

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

输入说明:

贝茜所在牛棚的坐标为(1, 2)。Farmer John能看到7个泥塘,它们的坐标分
别为(0, 2)、(-1, 3)、(3, 1)、(1, 1)、(4, 2)、(-1, 1)以及(2, 2)。
以下为农场的简图:(*为FJ的屋子,B为贝茜呆的牛棚)

4 . . . . . . . .
3 . M . . . . . .
Y 2 . . M B M . M .
1 . M . M . M . .
0 . . * . . . . .
-1 . . . . . . . .
-2-1 0 1 2 3 4 5

X

Sample Output

11

HINT

    约翰的最佳路线是:(0,0),(一1,0),(一2,0),(一2,1),(一2,2),(一2,3),(一2,4),(一1,4),(0,4),  (0,3),  (1,3),  (1,2).

Source

BZOJ 1627: [Usaco2007 Dec]穿越泥地( BFS )的更多相关文章

  1. bzoj 1627: [Usaco2007 Dec]穿越泥地【bfs】

    在洛谷上被卡了一个点开了O2才过= = bfs即可,为方便存储,把所有坐标+500 #include<iostream> #include<cstdio> #include&l ...

  2. 1627: [Usaco2007 Dec]穿越泥地

    1627: [Usaco2007 Dec]穿越泥地 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 504  Solved: 325[Submit][Sta ...

  3. 【BZOJ】1627: [Usaco2007 Dec]穿越泥地(bfs)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1627 裸bfs不解释.. #include <cstdio> #include < ...

  4. [Usaco2007 Dec]穿越泥地[bfs][水]

    Description 清早6:00,Farmer John就离开了他的屋子,开始了他的例行工作:为贝茜挤奶.前一天晚上,整个农场刚经受过一场瓢泼大雨的洗礼,于是不难想见,FJ 现在面对的是一大片泥泞 ...

  5. BZOJ1627: [Usaco2007 Dec]穿越泥地

    1627: [Usaco2007 Dec]穿越泥地 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 478  Solved: 303[Submit][Sta ...

  6. BZOJ 1692: [Usaco2007 Dec]队列变换( 贪心 )

    数据 n <= 30000 , 然后 O( n² ) 的贪心也过了..... USACO 数据是有多弱啊 = = ( ps : BZOJ 1640 和此题一模一样 , 双倍经验 ) ------ ...

  7. BZOJ 1692: [Usaco2007 Dec]队列变换 [后缀数组 贪心]

    1692: [Usaco2007 Dec]队列变换 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 1383  Solved: 582[Submit][St ...

  8. BZOJ 1691: [Usaco2007 Dec]挑剔的美食家 [treap 贪心]

    1691: [Usaco2007 Dec]挑剔的美食家 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 786  Solved: 391[Submit][S ...

  9. BZOJ 1625: [Usaco2007 Dec]宝石手镯( dp )

    最裸的01背包.... --------------------------------------------------------------------- #include<cstdio ...

随机推荐

  1. jQuery 插件入门

    先看http://www.cnblogs.com/lideng/p/3605551.html http://www.cnblogs.com/xcj26/p/3345556.html (非常细致  很多 ...

  2. HDU 1012 u Calculate e

    题解:直接模拟 #include <cstdio> int main(){ puts("n e");puts("- -----------");pu ...

  3. iOS加载HTML, CSS代码

    NSString *strHTML = @"<div style=\"text-align:center;\"><img src=\"/Upl ...

  4. uva 11137 Ingenuous Cubrency(完全背包)

    题目连接:11137 - Ingenuous Cubrency 题目大意:由21种规模的立方体(r 1~21),现在给出一个体积, 要求计算可以用多少种方式组成. 解题思路:完全背包, 和uva674 ...

  5. 保存android程序崩溃日志到SD卡

    private boolean writeToSDCard(Throwable ex) { boolean isDealing = false; if (Environment.getExternal ...

  6. SSIS: 使用Lookup 和 Cache transformation 进行数据匹配简单介绍

    本文将讲解Cache transformation的使用方式,并且用Lookup transformation进行匹配. 背景 如下图,我们的产品目标表中有些有尺寸信息有些没有.我们需要用Cache组 ...

  7. 在mac系统安装Apache Tomcat的详细步骤(转载自himi的博客,修改了错误添加了图片)

    链接地址:http://blog.csdn.net/liuyuyefz/article/details/8072485 1. 2. 3. 4. 5. 对于Apache Tomcat 估计很多童鞋都会, ...

  8. Java 网络编程(一) 网络基础知识

    链接地址:http://www.cnblogs.com/mengdd/archive/2013/03/09/2951826.html 网络基础知识 网络编程的目的:直接或间接地通过网络协议与其他计算机 ...

  9. Eclipse怎么全局搜索替换(整个项目)

    链接地址:http://jingyan.baidu.com/article/3ea51489c1c0d752e61bba2e.html 我们用Eclipse编程,有时候需要将整个项目的某个字符串替换成 ...

  10. IOS 使用程序外地图(IOS Map and google Map)

    1.调用IOS6苹果地图 IOS6中实现这个功能需要使用Map Kit中的MKPlaceMark和MKMapItem两个类,因此我们需要在工程中添加MapKit.framework主要代码如下: - ...