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:由于各种原因而不可通 ...
随机推荐
- hdu-3790最短路径问题
Problem Description 给你n个点,m条无向边,每条边都有长度d和花费p,给你起点s终点t,要求输出起点到终点的最短距离及其花费,如果最短距离有多条路线,则输出花费最少的. Inp ...
- Spring 基于注解零配置开发
本文是转载文章,感觉比较好,如有侵权,请联系本人,我将及时删除. 原文网址:< Spring 基于注解零配置开发 > 一:搜索Bean 再也不用在XML文件里写什么配置信息了. Sprin ...
- java.lang.String小测试
还记得java.lang.String么,如果现在给你一个小程序,你能说出它的结果么 public static String ab(String a){ return a + "b&quo ...
- [深入React] 3.JSX的神秘面纱
<div> <List /> </div> 会被编译为: React.createElement("div",null, React.creat ...
- Python 获取Facebook用户的Friends的爱好中的Top10
CODE; #!/usr/bin/python # -*- coding: utf-8 -*- ''' Created on 2014-8-12 @author: guaguastd @name: f ...
- HTML5硕士学习笔记
如今,该集团经过培训的同事给大家HTML5,他出席了两个5训练日,大概过一次给我们,在一个很形象.同事们更感兴趣的是. 课后共享所有的课件.在热情的新技术,我想工作有一个良好的早晨,我决定重新学习课件 ...
- android中正确保存view的状态
英文原文: http://trickyandroid.com/saving-android-view-state-correctly/ 转载此译文须注明出处. 今天我们聊一聊安卓中保存和恢复view状 ...
- VritualBox 中Debian安装tool
VritualBox 中Debian安装tool 环境 Debian 8 VirtualBox 配置Debian的源 #163源 deb http://mirrors.163.com/debian/ ...
- C#、.NET和ASP.NET三者之间的区别
刚毕业后出去找工作面试的时候就遇到这个问题!.回答不上来.回来网上查的如下: 那么 .NET.C#和ASP.NET这三者之间区别不清楚,到底它们之间有什么联系呢? 1..NET是一个平台,一个抽象的平 ...
- ISO3834认证所需的部分标准
SO9606-1 焊工考试——熔化焊——第一部分:钢 ISO9606-2 焊工考试——熔化焊——第二部分:铝及铝合金 ISO14732 焊接人员——金属材料全机械化及自动化焊接的熔化焊操作攻击电阻焊安 ...