BFS... 我连水题都不会写了QAQ

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

#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
#include<queue>
 
#define rep( i , n ) for( int i = 0 ; i < n ; ++i )
#define clr( x , c ) memset( x , c , sizeof( x ) )
#define Rep( i , n ) for( int i = 1 ; i <= n ; ++i )
 
using namespace std;
 
const int maxn = 100 + 5;
const int inf = 0x3f3f3f3f;
const int dir[ 4 ][ 2 ] = { { 0 , 1 } , { 0 , -1 } , { 1 , 0 } , { -1 , 0 } };
const int turn[ 4 ][ 4 ] = { { 1 , 0 , 1 , 1 } , { 1 , 1 , 1 , 0 } , { 1 , 1 , 0 , 1 } , { 0 , 1 , 1 , 1 } };
const int face[ 4 ] = { 1 , 3 , 2 , 0 };
 
int d[ 4 ][ maxn ][ maxn ];
int G[ maxn ][ maxn ];
int goal[ 2 ] , start[ 2 ];
 
#define goal( x , y ) ( x == goal[ 0 ] && y == goal[ 1 ] )
 
struct Node {
int x , y , cnt , f;
};
 
queue< Node > Q;
 
void BFS() {
rep( i , 4 ) {
   d[ i ][ start[ 0 ] ][ start[ 1 ] ] = 0;
   
   Q.push( ( Node ) { start[ 0 ] , start[ 1 ] , 0 , i } );
   
}
while( ! Q.empty() ) {
Node o = Q.front();
Q.pop();
rep( i , 4 ) {
int x = o.x + dir[ i ][ 0 ] , y = o.y + dir[ i ][ 1 ];
if( ! G[ x ][ y ] ) continue;
int cnt = o.cnt + turn[ i ][ o.f ];
if( d[ face[ i ] ][ x ][ y ] > cnt ) {
d[ face[ i ] ][ x ][ y ] = cnt;
if( ! goal( x , y ) ) Q.push( ( Node ) { x , y , cnt , face[ i ] } );
}
}
}
}
 
int main() {
// freopen( "test.in" , "r" , stdin );
// freopen( "test.out" , "w" , stdout );
int n;
cin >> n;
clr( G , 0 );
Rep( i , n )
   Rep( j , n ) {
   
    char c = getchar();
   
    while( !( c == '.' || c== 'x' || c == 'A' || c == 'B' ) ) 
       c = getchar();
       
    switch( c ) {
   
    case '.' : G[ i ][ j ] = 1; break;
    case 'A' : G[ start[ 0 ] = i ][ start[ 1 ] = j ] = 1; break;
    case 'B' : G[ goal[ 0 ] = i ][ goal[ 1 ] = j ] = 1; break;
   
    default  : break;
   
    }
   
   }
clr( d , inf );
BFS();
int ans = inf;
rep( i , 4 ) 
   ans = min( ans , d[ i ][ goal[ 0 ] ][ goal[ 1 ] ] );
   
cout << ans << "\n";
return 0;
}

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

1644: [Usaco2007 Oct]Obstacle Course 障碍训练课

Time Limit: 5 Sec  Memory Limit: 64 MB
Submit: 390  Solved: 199
[Submit][Status][Discuss]

Description

考虑一个 N x N (1 <= N <= 100)的有1个个方格组成的正方形牧场。有些方格是奶牛们不能踏上的,它们被标记为了'x'。例如下图:

. . B x .
. x x A .
. . . x .
. x . . .
. . x . .

贝茜发现自己恰好在点A处,她想去B处的盐块舔盐。缓慢而且笨拙的动物,比如奶牛,十分讨厌转弯。尽管如此,当然在必要的时候她们还是会转弯的。对于一个给定的牧场,请你计算从A到B最少的转弯次数。开始的时候,贝茜可以使面对任意一个方向。贝茜知道她一定可以到达。

Input

第 1行: 一个整数 N 行

2..N + 1: 行 i+1 有 N 个字符 ('.', 'x', 'A', 'B'),表示每个点的状态。

Output

行 1: 一个整数,最少的转弯次数。

Sample Input

3
.xA
...
Bx.

Sample Output

2

HINT

Source

BZOJ 1644: [Usaco2007 Oct]Obstacle Course 障碍训练课( BFS )的更多相关文章

  1. BZOJ 1644: [Usaco2007 Oct]Obstacle Course 障碍训练课

    题目 1644: [Usaco2007 Oct]Obstacle Course 障碍训练课 Time Limit: 5 Sec  Memory Limit: 64 MB Description 考虑一 ...

  2. bzoj 1644: [Usaco2007 Oct]Obstacle Course 障碍训练课【spfa】

    洛谷的数据毒啊 把(i,j,k)作为一个点spfa,表示点(i,j)朝向k方向,然后向四个方向转移即可 #include<iostream> #include<cstdio> ...

  3. 1644: [Usaco2007 Oct]Obstacle Course 障碍训练课

    1644: [Usaco2007 Oct]Obstacle Course 障碍训练课 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 383  Solved ...

  4. 【BZOJ】1644: [Usaco2007 Oct]Obstacle Course 障碍训练课(bfs)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1644 这和原来一题用dp来做的bfs很像啊orz.. 我们设f[i][j][k]代表i,j这个点之前 ...

  5. bzoj1644 [Usaco2007 Oct]Obstacle Course 障碍训练课

    Description 考虑一个 N x N (1 <= N <= 100)的有1个个方格组成的正方形牧场.有些方格是奶牛们不能踏上的,它们被标记为了'x'.例如下图: . . B x . ...

  6. BZOJ 1708: [Usaco2007 Oct]Money奶牛的硬币( dp )

    背包dp.. -------------------------------------------------------------------------------- #include< ...

  7. BZOJ 1708: [Usaco2007 Oct]Money奶牛的硬币

    1708: [Usaco2007 Oct]Money奶牛的硬币 Description 在创立了她们自己的政权之后,奶牛们决定推广新的货币系统.在强烈的叛逆心理的驱使下,她们准备使用奇怪的面值.在传统 ...

  8. BZOJ 1709: [Usaco2007 Oct]Super Paintball超级弹珠

    Description 奶牛们最近从著名的奶牛玩具制造商Tycow那里,买了一套仿真版彩弹游戏设备(类乎于真人版CS). Bessie把她们玩游戏草坪划成了N * N(1 <= N<= 1 ...

  9. bzoj 1709: [Usaco2007 Oct]Super Paintball超级弹珠【枚举】

    k是1e5范围的,吗? 注意到n只有100,这意味着k去重之后之后n^2,也就是1e4! 然后就可以愉快的n^4枚举了,枚举每个格子,再枚举每个敌人,如果当前格子射不到敌人则退出,否则满足所有敌人则a ...

随机推荐

  1. RedHat升级内核成功

    升级前 uname -aLinux localhost.localdomain 2.6.18-194.el5 #1 SMP Tue Mar 16 21:52:39 EDT 2010 x86_64 x8 ...

  2. 阿里云ECS每天一件事D4:安装mysql5.5.40

    Linux平台上MySQL也没什么好说的了,首先准备一下软件环境: yum install gcc gcc-c++ gcc-g77 autoconf automake make cmake bison ...

  3. d指针在Qt上的应用及实现(d指针能实现二进制兼容)

    Qt为了使其动态库最大程度上实现二进制兼容,引入了d指针的概念.那么为什么d指针能实现二进制兼容呢?为了回答这个问题,首先弄清楚什么是二进制兼容?所谓二进制兼容动态库,指的是一个在老版本库下运行的程序 ...

  4. Visual Studio 中用管理员权限运行、调试程序

    原文:Visual Studio 中用管理员权限运行.调试程序 一个Sample小程序,用于验证WoW64的Windows Registry的读写访问.在Visual Studio 2010中调试运行 ...

  5. VS2010/MFC对话框:字体对话框

    字体对话框) 在上一节为大家讲解了文件对话框的使用,本节则主要介绍字体对话框如何应用. 字体对话框的作用是用来选择字体.我们也经常能够见到.MFC使用CFontDialog类封装了字体对话框的所有操作 ...

  6. Please ensure that adb is correctly located at '...adb.exe' and can be executed.

    Android Launch! The connection to adb is down, and a severe error has occured. You must restart adb ...

  7. 展开字符串(dfs)

    展开字符串 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Subm ...

  8. USACO Section 5.4 TeleCowmunication(最小割)

    挺裸的一道最小割.把每台电脑拆成一条容量为1的边,然后就跑最大流.从小到大枚举每台电脑,假如去掉后 最大流=之前最大流+1,那这台电脑就是answer之一了. -------------------- ...

  9. 10条PHP编程习惯

    过去的几周对我来说是一段相当复杂的经历.我们公司进行了大裁员,我是其中之一,但却体验到了其中的乐 趣.我从来没有被开除过,所以很难不去想得太多.我开始浏览招聘板块,一个全职PHP程序员的职位很吸引人, ...

  10. A Byte of Python 笔记(5)函数:定义、形参、局部变量、默认参数、关键参数

    第7章  函数 函数是重要的程序段.它们允许你给一块语句一个名称,然后你可以在程序的任何地方使用这个名称任意多次地运行这个语句块.这被称为 调用 函数. 定义函数 函数通过 def 关键字定义.def ...