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. [问题解决] Tomcat Child not unique

    错误: child not unique   发生场景: tomcat服务器   解决方案: 将tomcat中的server.xml文件配置: <Host name="localhos ...

  2. c++实现将表达式转换为逆波兰表达式

    https://github.com/Lanying0/lintcode 所属: 数据结构->线性结构->栈 问题: 给定一个表达式字符串数组,返回该表达式的逆波兰表达式(即去掉括号). ...

  3. openssl命令行Base64编解码

    openssl对base64编解码的规范支持较差,用它编解码的结果别的语言如php处理很不方便,注意的几点整理如下 1,如果php加密结果做base64编码长度小于64,则需要添加一个换行符opens ...

  4. SQL Server 2012学习笔记 1 命令行安装

    setup.exe /Q /IACCEPTSQLSERVERLICENSETERMS /ACTION=install /PID=748RB-X4T6B-MRM7V-RTVFF-CHC8H /FEATU ...

  5. C#复习一( Twenty Days)

      今天开始将要复习最近所学的一些C#知识.下面就来总结一下今天所复习的内容,以此来巩固我对C#知识的掌握.今天主要以举几个程序为例. 首先还是要注意代码的规范: –注释//,/**/,/// –骆驼 ...

  6. DEV GridControl 小结(持续添加)

    一.属性: 1.Views OptionsBehavior=>Editable:False  列表不可编辑 OptionsSelection=>EnableAppearanceFocuse ...

  7. 利用 squid 反向代理提高网站性能

    http://www.ibm.com/developerworks/cn/linux/l-cn-squid/ http://www.squid-cache.org/ http://www.beijin ...

  8. 删除Lb重复的数,用La输出(顺序表)

    #include<stdio.h> typedef int A; const int LIST_INIT_SIZE=100; const int LISTINCRMENT=10; type ...

  9. BZOJ 1046: [HAOI2007]上升序列(LIS)

    题目挺坑的..但是不难.先反向做一次最长下降子序列.然后得到了d(i),以i为起点的最长上升子序列,接下来贪心,得到字典序最小. ----------------------------------- ...

  10. 磁盘性能,你可能不知道的IOPS计算方法

    每个I/O 请求到磁盘都需要若干时间.主要是因为磁盘的盘边必须旋转,机头必须寻道.磁盘的旋转常常被称为”rotational delay”(RD),机头的移动称为”disk seek”(DS).一个I ...