http://www.lydsy.com/JudgeOnline/problem.php?id=1644

这和原来一题用dp来做的bfs很像啊orz。。

我们设f[i][j][k]代表i,j这个点之前的方向过来的拐弯数

那么

f[fx][fy][i]=min(f[x][y][j]+1) 当i!=j

f[fx][fy][i]=min(f[x][y][j]) 当i=j

#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
#define rep(i, n) for(int i=0; i<(n); ++i)
#define for1(i,a,n) for(int i=(a);i<=(n);++i)
#define for2(i,a,n) for(int i=(a);i<(n);++i)
#define for3(i,a,n) for(int i=(a);i>=(n);--i)
#define for4(i,a,n) for(int i=(a);i>(n);--i)
#define CC(i,a) memset(i,a,sizeof(i))
#define read(a) a=getint()
#define print(a) printf("%d", a)
#define dbg(x) cout << #x << " = " << x << endl
#define printarr(a, n, m) rep(aaa, n) { rep(bbb, m) cout << a[aaa][bbb]; cout << endl; }
inline const int getint() { int r=0, k=1; char c=getchar(); for(; c<'0'||c>'9'; c=getchar()) if(c=='-') k=-1; for(; c>='0'&&c<='9'; c=getchar()) r=r*10+c-'0'; return k*r; }
inline const int max(const int &a, const int &b) { return a>b?a:b; }
inline const int min(const int &a, const int &b) { return a<b?a:b; } const int N=105, Q=N*N*N*3, dx[]={-1, 1, 0, 0}, dy[]={0, 0, -1, 1};
int mp[N][N], front, tail, n, f[N][N][4];
struct dat { int x, y; }q[Q];
int main() {
read(n);
char s[105];
int x, y, xx, yy;
for1(i, 1, n) {
scanf("%s", s+1);
for1(j, 1, n) {
if(s[j]=='x') mp[i][j]=1;
else if(s[j]=='B') mp[i][j]=3, xx=i, yy=j;
else if(s[j]=='A') mp[i][j]=2, x=i, y=j;
}
}
CC(f, 0x7f);
int ans=~0u>>1;
q[tail].x=x, q[tail++].y=y;
rep(i, 4) f[x][y][i]=0;
while(front!=tail) {
dat &t=q[front++]; if(front==Q) front=0;
x=t.x, y=t.y;
rep(i, 4) {
int fx=dx[i]+x, fy=dy[i]+y;
if(fx<1 || fy<1 || fx>n || fy>n || mp[fx][fy]==1) continue;
rep(j, 4) {
bool flag=0;
if(i==j && f[fx][fy][i]>f[x][y][i]) {
f[fx][fy][i]=f[x][y][i];
flag=1;
}
if(i!=j && f[fx][fy][i]>f[x][y][j]+1) {
f[fx][fy][i]=f[x][y][j]+1;
flag=1;
}
if(flag) {
dat &t2=q[tail++]; if(tail==Q) tail=0;
t2.x=fx; t2.y=fy;
}
}
}
}
rep(i, 4) ans=min(f[xx][yy][i], ans);
print(ans);
return 0;
}

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 障碍训练课( BFS )

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

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

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

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

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

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

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

  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的缓存

    删除tomcat目录下的work目录中的Catalina目录就好了!

  2. [Exception JavaWeb 1] - Cause: com.microsoft.sqlserver.jdbc.SQLServerException: '@P2' 附近有语法错误。

    严重: Servlet.service() for servlet [springMVC] in context with path [/ExceptionManageSystem] threw ex ...

  3. WebApi2 知识点总结

    1.建议使用异步接口async Task<> public async Task<IHttpActionResult> Get() 如果返回的是IEnumerable请使用: ...

  4. java.lang.Void and void

    java.lang.Void is analogous to java.lang.Integer. Integer is a way of boxing values of the primitive ...

  5. Android开发之定位系统

    2013-07-04 定位系统 全球定位系统(Global Positioning System, GPS), 又称全球卫星定位系统. 最少只需其中3颗卫星,就能迅速确定用户组地球所处的位置及海拔高度 ...

  6. tomcat设置jvm参数

    http://www.quiee.com.cn/archives/592/ Tomcat默认可以使用的内存为128MB,Windows下,在文件{tomcat_home}/bin/catalina.b ...

  7. Android SDK 快速安装方法

    我们都知道使用Android sdk manager下载安装sdk速度非常慢,一般在10k/s以内,本文章推荐一种能够借助迅雷等下载工具下载sdk的zip包从而快速安装sdk的方法. 1.下载3个xm ...

  8. 摘:C/C++中时间类time.h

    摘要:本文从介绍基础概念入手,探讨了在C/C++中对日期和时间操作所用到的数据结构和函数,并对计时.时间的获取.时间的计算和显示格式等方面进行了阐述.本文还通过大量的实例向你展示了time.h头文件中 ...

  9. 常用RGB颜色表

      作者:张家珩2005-12-02 20:51分类:默认分类     R G B 值   R G B 值   R G B 值 黑色 0 0 0 #000000 黄色 255 255 0 #FFFF0 ...

  10. win10 配置 python3 + opencv3.2 + VideoCapture

    最近需要在 win10 上进行图片处理,使用深度学习框架 tensorflow ,所以安装了python3.5 + opencv3.2 + tensorflow + VideoCapture + PI ...