BZOJ1671: [Usaco2005 Dec]Knights of Ni
1671: [Usaco2005 Dec]Knights of Ni
Time Limit: 5 Sec Memory Limit: 64 MB
Submit: 175 Solved: 107
[Submit][Status][Discuss]
Description
Input
Output
Sample Input
4 1 0 0 0 0 1 0
0 0 0 1 0 1 0 0
0 2 1 1 3 0 4 0
0 0 0 4 1 1 1 0
INPUT DETAILS:
Width=8, height=4. Bessie starts on the third row, only a few squares away
from the Knights.
Sample Output
OUTPUT DETAILS:
Bessie can move in this pattern to get a shrubbery for the Knights:
N, W, N, S, E, E, N, E, E, S, S. She gets the shrubbery in the northwest
corner and then makes her away around the barriers to the east and then
south to the Knights.
HINT
Source
#include<cstdio>
#include<cstring>
struct target{
int x,y;
}t[];
struct queue{
int x,y;
}q[];
const int mx[]={,,,-};
const int my[]={,,-,};
int n,m,cnt,x1,y1,x2,y2,head,tail,ans=;
int map[][];
int dis1[][];
int dis2[][];
bool mrk[][];
inline int min(int a,int b)
{return a<b?a:b;}
inline int read()
{
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
inline void bfs1(int x,int y)
{
head=;tail=;mrk[x][y]=;
q[].x=x;q[].y=y;
while (head<tail)
{
int nx=q[++head].x,ny=q[head].y;
for (int k=;k<;k++)
{
int xx=nx+mx[k],yy=ny+my[k];
if (xx<||xx>n||yy<||yy>m)continue;
if (mrk[xx][yy]||map[xx][yy]==) continue;
dis1[xx][yy]=dis1[nx][ny]+;
q[++tail].x=xx;q[tail].y=yy;
mrk[xx][yy]=;
}
}
}
inline void bfs2(int x,int y)
{
memset(q,,sizeof(q));
memset(mrk,,sizeof(mrk));
head=;tail=;mrk[x][y]=;
q[].x=x;q[].y=y;
while (head<tail)
{
int nx=q[++head].x,ny=q[head].y;
for (int k=;k<;k++)
{
int xx=nx+mx[k],yy=ny+my[k];
if (xx<||xx>n||yy<||yy>m)continue;
if (mrk[xx][yy]||map[xx][yy]==) continue;
dis2[xx][yy]=dis2[nx][ny]+;
q[++tail].x=xx;q[tail].y=yy;
mrk[xx][yy]=;
}
}
}
int main()
{
m=read();n=read();
for (int i=;i<=n;i++)
for(int j=;j<=m;j++)
{
map[i][j]=read();
if (map[i][j]==)
{
t[++cnt].x=i;
t[cnt].y=j;
}else
if (map[i][j]==)
{
x1=i;
y1=j;
map[i][j]=;
}else
if (map[i][j]==)
{
x2=i;
y2=j;
map[i][j]=;
}
}
bfs1(x1,y1);
bfs2(x2,y2);
for (int i=;i<=cnt;i++)
{
int nx=t[i].x,ny=t[i].y;
if (!(dis1[nx][ny]+dis2[nx][ny]))continue;
ans=min(ans,dis1[nx][ny]+dis2[nx][ny]);
}
printf("%d",ans);
}
BZOJ1671: [Usaco2005 Dec]Knights of Ni的更多相关文章
- POJ3170 Bzoj1671 [Usaco2005 Dec]Knights of Ni 骑士
1671: [Usaco2005 Dec]Knights of Ni 骑士 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 281 Solved: 180 ...
- bzoj1671 [Usaco2005 Dec]Knights of Ni 骑士
Description Bessie is in Camelot and has encountered a sticky situation: she needs to pass through t ...
- 【BZOJ1671】[Usaco2005 Dec]Knights of Ni 骑士 BFS
[Usaco2005 Dec]Knights of Ni 骑士 Description 贝茜遇到了一件很麻烦的事:她无意中闯入了森林里的一座城堡,如果她想回家,就必须穿过这片由骑士们守护着的森林.为 ...
- 1671: [Usaco2005 Dec]Knights of Ni 骑士
1671: [Usaco2005 Dec]Knights of Ni 骑士 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 254 Solved: 163 ...
- 【BZOJ】1671: [Usaco2005 Dec]Knights of Ni 骑士(bfs)
http://www.lydsy.com/JudgeOnline/problem.php?id=1671 从骑士bfs一次,然后从人bfs一次即可. #include <cstdio> # ...
- BZOJ_1671_[Usaco2005 Dec]Knights of Ni 骑士_BFS
Description Bessie is in Camelot and has encountered a sticky situation: she needs to pass through t ...
- [Usaco2005 Dec]Knights of Ni 骑士
Description Bessie is in Camelot and has encountered a sticky situation: she needs to pass through t ...
- BZOJ 1671: [Usaco2005 Dec]Knights of Ni 骑士 (bfs)
题目: https://www.lydsy.com/JudgeOnline/problem.php?id=1671 题解: 按题意分别从贝茜和骑士bfs然后meet_in_middle.. 把一个逗号 ...
- bzoj 1671: [Usaco2005 Dec]Knights of Ni 骑士【bfs】
bfs预处理出每个点s和t的距离d1和d2(无法到达标为inf),然后在若干灌木丛格子(x,y)里取min(d1[x][y]+d2[x][y]) /* 0:贝茜可以通过的空地 1:由于各种原因而不可通 ...
随机推荐
- 查看MySQL数据库的默认编码
查看MySQL数据库的默认编码 1.使用status命令能够显示数据库的相关系信息,示例如下: mysql> status;————–mysql Ver 14.12 Distrib 5.0.77 ...
- erlang mnesia 数据库查询
Mnesia是一个分布式数据库管理系统,适合于电信和其它需要持续运行和具备软实时特性的Erlang应用,越来越受关注和使用,但是目前Mnesia资料却不多,很多都只有官方的用户指南.下面的内容将着重说 ...
- java随机数生成器
一.java.lang.Math.Random 调用这个Math.Random()函数能够返回带正号的double值,取值范围是[0.0,1.0)的左闭右开区间,并在该范围内(近似)均匀分布. 二.j ...
- Spring 基于注解零配置开发
本文是转载文章,感觉比较好,如有侵权,请联系本人,我将及时删除. 原文网址:< Spring 基于注解零配置开发 > 一:搜索Bean 再也不用在XML文件里写什么配置信息了. Sprin ...
- OSI七层结构
- C#中对于接口的实现方式
转载: C#中对于接口的实现方式有隐式接口和显式接口两种: 隐式地实现接口成员创建一个接口,IChinese,包含一个成员 Speak;我们创建一个类Speaker,实现接口Chinese //隐藏式 ...
- js函数收藏:获取cookie值
//先设置一段子cookie var d = new Date(); d.setMonth(d.getMonth() + 1); d = d.toGMTString(); var a = " ...
- JAXB--学习2
一.Jaxb处理java对象和xml之间转换常用的annotation有: @XmlType @XmlElement @XmlRootElement @XmlAttribute @XmlAccesso ...
- ZOJ 3511 不相交切切多边形 线段树求最大边数
题意: n多凸边形 m刀 (把n切m刀,问切完后的图形中 最多的边数 是多少) 切a点-b点 数据保证切的刀不会相交 思路: 2点之间的剩余点数就是边数, 把a-b距离 近 排序 切完一刀就统计一下切 ...
- yum安装于卸载软件常见命令
1.使用yum安装和卸载软件,有个前提是yum安装的软件包都是rpm格式的. 安装的命令是,yuminstall ~,yum会查询数据库,有无这一软件包,如果有,则检查其依赖冲突关系,如果没有依赖冲突 ...