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

广搜……不解释

只要注意搜到某个点朝某个方向的时候向四个方向都判一下转个方向入队

#include<cstdio>
#include<cstring>
const int mx[4]={1,0,-1,0};
const int my[4]={0,1,0,-1};
int n,sx,sy,ex,ey,ans=100000,t,w;
int dist[4][110][110];
int q[100001];
int dire[100001];
bool map[110][110];
char ch[110];
inline int min(int a,int b){return a<b?a:b;}
inline void bfs()
{
for (int i=0;i<4;i++)
{
q[++w]=(sx-1)*n+sy;
dire[w]=i;
dist[i][sx][sy]=1;
}
while (t<w)
{
int nx;if(q[++t]%n)nx=q[t]/n+1;else nx=q[t]/n;
int ny=q[t]%n;if (!ny)ny=n;
int d=dire[t];
int step=dist[d][nx][ny];
for (int k=0;k<4;k++)
if (step+1<dist[k][nx][ny])
{
dist[k][nx][ny]=step+1;
q[++w]=(nx-1)*n+ny;
dire[w]=k;
}
int wx=nx+mx[d];
int wy=ny+my[d];
if (wx<1||wy<1||wx>n||wy>n||!map[wx][wy])continue;
if (step<dist[d][wx][wy])
{
dist[d][wx][wy]=step;
q[++w]=(wx-1)*n+wy;
dire[w]=d;
}
}
}
int main()
{
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%s",ch);
for (int j=0;ch[j];j++)
{
if (ch[j]=='A')
{
sx=i;
sy=j+1;
map[i][j+1]=1;
}else
if (ch[j]=='B')
{
ex=i;
ey=j+1;
map[i][j+1]=1;
}else
if (ch[j]=='.')map[i][j+1]=1;
}
}
memset(dist,127/3,sizeof(dist));
bfs();
for(int i=0;i<4;i++)
if (dist[i][ex][ey])ans=min(ans,dist[i][ex][ey]);
printf("%d",ans-1);
}

bzoj1644 [Usaco2007 Oct]Obstacle Course 障碍训练课的更多相关文章

  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. 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. bzoj 1644: [Usaco2007 Oct]Obstacle Course 障碍训练课【spfa】

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

  6. 【题解】[USACO2007 OCT]Obstacle Course-C++

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

  7. BZOJ1709: [Usaco2007 Oct]Super Paintball超级弹珠

    1709: [Usaco2007 Oct]Super Paintball超级弹珠 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 324  Solved: ...

  8. BZOJ1708: [Usaco2007 Oct]Money奶牛的硬币

    1708: [Usaco2007 Oct]Money奶牛的硬币 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 513  Solved: 329[Submi ...

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

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

随机推荐

  1. < welcome > 一起学习,进步,分享。

    现在时间:2014-3-24 hello world my blog. 第一次做博客,欢迎各路朋友指教.慢慢的分享学习到得东西,本人目前正在做IOS,也在学习数据库简单地应用. 作为一个IOS开发者, ...

  2. PS抠图神器: KnockOut 2.0安装汉化和使用教程

    PS抠图神器: KnockOut 2.0安装汉化和使用教程 http://jingyan.baidu.com/article/6b97984d8aeadc1ca2b0bf3b.html

  3. Jquery autocomplete 插件示例

    通过Jquery autocomplete 插件动态传递输入参数完成自动完成提示: <%@ page language="java" import="java.ut ...

  4. Encode and Decode Strings 解答

    Question Design an algorithm to encode a list of strings to a string. The encoded string is then sen ...

  5. <转载>Wait and Waitpid

    转载http://www.cnblogs.com/lihaosky/articles/1673341.html 一.Wait #include <sys/types.h> /* 提供类型p ...

  6. linux使用共享内存通信的进程同步退出问题

    两个甚至多个进程使用共享内存(shm)通信,总遇到同步问题.这里的“同步问题”不是说进程读写同步问题,这个用信号量就好了.这里的同步问题说的是同步退出问题,到底谁先退出,怎么知道对方退出了.举个例子: ...

  7. iOS面试知识点

    1 iOS基础 1.1 父类实现深拷贝时,子类如何实现深度拷贝.父类没有实现深拷贝时,子类如何实现深度拷贝. 深拷贝同浅拷贝的区别:浅拷贝是指针拷贝,对一个对象进行浅拷贝,相当于对指向对象的指针进行复 ...

  8. What a Mess(二分)

    What a Mess Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit  ...

  9. C++模板 静态成员 定义(实例化)

    问一个问题: 考虑一个模板: template <typename T> class Test{ public: static std::string info; }; 对于下面若干种定义 ...

  10. 观察者模式与Boost.Signals

      1)  观察者模式定义 略,各种设计模式的书上都有定义. 2)  观察者模式一般实现 观察者模式一般实现,都是“被观察者”保存一个“观察者”的列表,循环这个列表来通知“观察者”.代码,其中使用了b ...