Bzoj 1687: [Usaco2005 Open]Navigating the City 城市交通 广搜,深搜
1687: [Usaco2005 Open]Navigating the City 城市交通
Time Limit: 5 Sec Memory Limit: 64 MB
Submit: 122 Solved: 85
[Submit][Status][Discuss]
Description
3 6
+-+-+.+-+-+
|...|.....|
+-+.+-+-+-+
..|.......|
S-+-+-+.E-+
3 6
+-+-+.+-+-+
|...|.....|
+-+.+-+-+-+
..|.......|
S-+-+-+.E-+
Input
第1行:两个用空格隔开的整数N和E.
第2到2N行:每行有2E-I个字符,表示地图.
Output
每行有一个表示方向的字母和一个表示要开几个十字路口的数字表示.
Sample Input
Sample Input
3 6
+-+-+.+-+-+
|...|.....|
+-+.+-+-+-+
..|.......|
S-+-+-+.E-+
Sample Output
N 1
W 1
N 1
E 2
S 1
E 3
S 1
W 1
HINT
Source
题解:
BFS+DFS
先用BFS遍历图,跑出所有的步数,再用DFS倒推找到合法路径。最后输出即可。
#include<bits/stdc++.h>
using namespace std;
#define INF 1e9
int fx[]={-,,,};
int fy[]={,,-,};
int n,m,dis[][],pd,bx,by;
int qx[],qy[];
bool vv[][],vis[][];
char a[][];
void dfs(int x,int y,int bs)
{
int i,xx,yy;
vv[x][y]=true;
if(pd==)return;
if(x==bx&&y==by){pd=;return;}
for(i=;i<=;i++)
{
xx=x+fx[i];
yy=y+fy[i];
if(xx>=&&xx<=*n-&&yy>=&&yy<=*m-&&dis[xx][yy]==bs-)
{
dfs(xx,yy,bs-);
if(pd==)return;
}
}
}
void print(int k)
{
if(k==)printf("N");
if(k==)printf("S");
if(k==)printf("W");
if(k==)printf("E");
}
int main()
{
int i,j,xx,yy,ex,ey,jl,fx1,ux,uy,vx,vy,head,tail;
scanf("%d %d",&n,&m);
for(i=;i<=*n-;i++)scanf("\n%s",a[i]+);
bx=;by=;ex=;ey=;
for(i=;i<=*n-;i++)
{
for(j=;j<=*m-;j++)
{
if(a[i][j]=='S')bx=i,by=j;
if(a[i][j]=='E')ex=i,ey=j;
dis[i][j]=INF;
}
}
head=;tail=;
qx[tail]=bx;qy[tail]=by;dis[bx][by]=;
memset(vis,false,sizeof(vis));vis[bx][by]=true;
pd=;
while(head!=tail)
{
head++;if(head==)head=;
ux=qx[head];
uy=qy[head];
for(i=;i<=;i++)
{
vx=ux+fx[i];
vy=uy+fy[i];
if(vx>=&&vx<=*n-&&vy>=&&vy<=*m-&&vis[vx][vy]==false&&dis[vx][vy]>dis[ux][uy]+&&a[vx][vy]!='.')
{
vis[vx][vy]=true;
dis[vx][vy]=dis[ux][uy]+;
tail++;if(tail==)tail=;
qx[tail]=vx;
qy[tail]=vy;
if(vx==ex&&vy==ey){pd=;break;}
}
}
if(pd==)break;
vis[ux][uy]=false;
}
memset(vv,false,sizeof(vv));
pd=;
dfs(ex,ey,dis[ex][ey]);
fx1=-;//北0,南1,西2,东3.
jl=;
vv[bx][by]=false;
while()
{
if(bx==ex&&by==ey){if(jl!=){print(fx1);printf(" %d\n",(jl+)/);}break;}
for(i=;i<=;i++)
{
xx=bx+fx[i];
yy=by+fy[i];
if(vv[xx][yy]==true)break;
}
vv[xx][yy]=false;
bx=xx;
by=yy;
if(i==fx1)jl++;
else
{
if(fx1==-){fx1=i;jl=;}
else {print(fx1);printf(" %d\n",(jl+)/);fx1=i;jl=;}
}
}
return ;
}
Bzoj 1687: [Usaco2005 Open]Navigating the City 城市交通 广搜,深搜的更多相关文章
- 1687: [Usaco2005 Open]Navigating the City 城市交通
1687: [Usaco2005 Open]Navigating the City 城市交通 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 94 Sol ...
- 【BZOJ】1687: [Usaco2005 Open]Navigating the City 城市交通(bfs)
http://www.lydsy.com/JudgeOnline/problem.php?id=1687 bfs后然后逆向找图即可.因为题目保证最短路唯一 #include <cstdio> ...
- bzoj:1687;poj 2434:[Usaco2005 Open]Navigating the City 城市交通
Description A dip in the milk market has forced the cows to move to the city. The only employment av ...
- Bzoj 1674: [Usaco2005]Part Acquisition dijkstra,堆
1674: [Usaco2005]Part Acquisition Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 337 Solved: 162[Sub ...
- [BZOJ 1733] [Usaco2005 feb] Secret Milking Machine 【二分 + 最大流】
题目链接:BZOJ - 1733 题目分析 直接二分这个最大边的边权,然后用最大流判断是否可以有 T 的流量. 代码 #include <iostream> #include <cs ...
- BZOJ 1739: [Usaco2005 mar]Space Elevator 太空电梯
题目 1739: [Usaco2005 mar]Space Elevator 太空电梯 Time Limit: 5 Sec Memory Limit: 64 MB Description The c ...
- BZOJ 1672: [Usaco2005 Dec]Cleaning Shifts 清理牛棚
题目 1672: [Usaco2005 Dec]Cleaning Shifts 清理牛棚 Time Limit: 5 Sec Memory Limit: 64 MB Description Farm ...
- BZOJ 1677: [Usaco2005 Jan]Sumsets 求和( dp )
完全背包.. --------------------------------------------------------------------------------------- #incl ...
- BZOJ 1679: [Usaco2005 Jan]Moo Volume 牛的呼声( )
一开始直接 O( n² ) 暴力..结果就 A 了... USACO 数据是有多弱 = = 先sort , 然后自己再YY一下就能想出来...具体看code --------------------- ...
随机推荐
- Peter Pan By: J. M. Barrie
Audio book: (mp3+txt) http://www.booksshouldbefree.com/book/peter-pan-by-j-m-barrie
- Batch file Functions
Quote from: http://ss64.com/nt/syntax-functions.html Batch file Functions Packaging up code into a d ...
- 专题一、ArrayList增删操作技术细节详解
一.索引检查 1)在指定位置插入元素时,第一步都需要检查输入的指定位置是否合法 public void add(int index, E element){ rangeCheckForAdd(i ...
- mysql主从之主键冲突
收到短信报警,两台数据库都报slave同步失败了,先说明一下环境,架构:lvs+keepalived+amoeba+mysql,主主复制,单台写入, 主1:192.168.0.223(写) 主2:19 ...
- ubuntu14.04+opencv 3.0+python2.7安装及测试
本文记录了ubuntu下使用源码手动安装opencv的过程.步骤来自opencv官网 此外记录了在python中安装及载入opencv的方法. 1.安装opencv所需的库(编译器.必须库.可选库) ...
- less学习-语法(二)
变量 @color1:#fff; 选择器 // Variables @mySelector: banner; // Usage .@{mySelector} { font-weight: bold; ...
- XStream简单使用01——xml和Ojbect互转
package org.zhb.test; /** * author : zhb * data : 2014-2-14 * use packages: * xmlpull-1.1.3.1.jar * ...
- wpf+xml实现的一个随机生成早晚餐的小demo
话说每到吃完的时间就发愁,真的不知道该吃什么,然后就想到做一个生成吃什么的小软件,既然这个软件如此的简单,就打算用wpf开发吧,也不用数据库了,直接保存在xml中就可以了 程序整体结构如下图 首先我写 ...
- Swift开发之 ---- Swift宏定义
swift中没有了#Define这种宏定义了,可以用let来声明常量来取代,判断当前系统版本 let IS_IOS7 = (UIDevice.currentDevice().systemVersion ...
- No Hibernate Session bound to thread, and configuration does not allow creat
No Hibernate Session bound to thread, and configuration does not allow creat 今天遇到这么一个错误,在网上差了很多都没有能解 ...