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. 算法笔记_144:有向图强连通分量的Tarjan算法(Java)

    目录 1 问题描述 2 解决方案 1 问题描述 引用自百度百科: 如果两个顶点可以相互通达,则称两个顶点强连通(strongly connected).如果有向图G的每两个顶点都强连通,称G是一个强连 ...

  2. Python——极限编程

    很cool的名字,极限编程(Extreme Programming,简写XP)是编程的一种流行趋势: (1)首先是对目标进行计划: (2)然后将测试用例集合编写为一种框架: (3)之后才编写实际的代码 ...

  3. Drawing text

    We begin with drawing some Unicode text on the client area of a window. #!/usr/bin/python # -*- codi ...

  4. 如何打印加密的PDF文件?

    如何打印加密的PDF文件? Pdf加密了不让打印怎么办?? 下载Foxit PDF Editor以下是下载地址:http://www.orsoon.com/Soft/4865.html 用它打开加密的 ...

  5. JUnit 3.8 通过反射测试私有方法

    测试私有(private)的方法有两种: 1)把目标类的私有方法(修饰符:private)修改为(public),不推荐,因为修改了源程序不佳 2)通过反射 (推荐) 代码演示: 目标程序 Priva ...

  6. 【BIRT】01_在win10上安装BIRT

    环境:windows 10 64位 安装文件:链接:https://pan.baidu.com/s/1vYGbB0D1QeQ923oIIdoI9Q 密码:qcde 说明:安装文件也可以自己去官网下载, ...

  7. Native App、Web App 还是Hybrid App

    Native App.Web App 还是Hybrid App? 技术 标点符 1年前 (2014-05-09) 3036℃ 0评论 一.什么是Native App? Native App即原生应用, ...

  8. OGNL表达式中的#、%和$

    $是el表达式  % #是ognl表达式.  http://devilkirin.iteye.com/blog/427375 OGNL表达式非常强大-其中#.%.$这三个符号在OGNL表达式中经常出现 ...

  9. Linux命令-压缩解压命令:tar

    tar [选项] [打包后文件名] [打包前的文件或者目录名称] -c表示创建(create-创建) -z表示压缩(gzip-压缩) -j表示压缩(bzip2-压缩) -v显示进度(verbose-冗 ...

  10. C#USB录像视频拍照-代码

    论坛帖:http://bbs.csdn.net/topics/390536016 using System; using System.Collections.Generic; using Syste ...