1687: [Usaco2005 Open]Navigating the City 城市交通

Time Limit: 5 Sec  Memory Limit: 64 MB
Submit: 122  Solved: 85
[Submit][Status][Discuss]

Description

    由于牛奶市场的需求,奶牛必须前往城市,但是唯一可用的交通工具是出租车.教会奶牛如何在城市里打的.
    给出一个城市地图,东西街区E(1≤E≤40),南北街区N(1≤N≤30).制作一个开车指南给出租车司机,告诉他如何从起点(用S表示)到终点(用E表示).每一个条目用空格分成两部分,第一个部分是方向(N,E,S,W之一),第二个是一个整数,表示要沿着这个方向开几个十字路口.如果存在多条路线,你应该给出最短的.数据保证,最短的路径存在且唯一.    地图中“+”表示十字路口,道路用“I”和“一”表示.建筑和其他设施用“.”表示.下面是一张地图:

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

E 1
N 1
W 1
N 1
E 2
S 1
E 3
S 1
W 1

HINT

 

Source

Silver

题解:

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 城市交通 广搜,深搜的更多相关文章

  1. 1687: [Usaco2005 Open]Navigating the City 城市交通

    1687: [Usaco2005 Open]Navigating the City 城市交通 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 94  Sol ...

  2. 【BZOJ】1687: [Usaco2005 Open]Navigating the City 城市交通(bfs)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1687 bfs后然后逆向找图即可.因为题目保证最短路唯一 #include <cstdio> ...

  3. 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 ...

  4. Bzoj 1674: [Usaco2005]Part Acquisition dijkstra,堆

    1674: [Usaco2005]Part Acquisition Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 337  Solved: 162[Sub ...

  5. [BZOJ 1733] [Usaco2005 feb] Secret Milking Machine 【二分 + 最大流】

    题目链接:BZOJ - 1733 题目分析 直接二分这个最大边的边权,然后用最大流判断是否可以有 T 的流量. 代码 #include <iostream> #include <cs ...

  6. BZOJ 1739: [Usaco2005 mar]Space Elevator 太空电梯

    题目 1739: [Usaco2005 mar]Space Elevator 太空电梯 Time Limit: 5 Sec  Memory Limit: 64 MB Description The c ...

  7. BZOJ 1672: [Usaco2005 Dec]Cleaning Shifts 清理牛棚

    题目 1672: [Usaco2005 Dec]Cleaning Shifts 清理牛棚 Time Limit: 5 Sec  Memory Limit: 64 MB Description Farm ...

  8. BZOJ 1677: [Usaco2005 Jan]Sumsets 求和( dp )

    完全背包.. --------------------------------------------------------------------------------------- #incl ...

  9. BZOJ 1679: [Usaco2005 Jan]Moo Volume 牛的呼声( )

    一开始直接 O( n² ) 暴力..结果就 A 了... USACO 数据是有多弱 = = 先sort , 然后自己再YY一下就能想出来...具体看code --------------------- ...

随机推荐

  1. Peter Pan By: J. M. Barrie

    Audio book: (mp3+txt) http://www.booksshouldbefree.com/book/peter-pan-by-j-m-barrie

  2. Batch file Functions

    Quote from: http://ss64.com/nt/syntax-functions.html Batch file Functions Packaging up code into a d ...

  3. 专题一、ArrayList增删操作技术细节详解

    一.索引检查 1)在指定位置插入元素时,第一步都需要检查输入的指定位置是否合法 public void add(int index, E element){    rangeCheckForAdd(i ...

  4. mysql主从之主键冲突

    收到短信报警,两台数据库都报slave同步失败了,先说明一下环境,架构:lvs+keepalived+amoeba+mysql,主主复制,单台写入, 主1:192.168.0.223(写) 主2:19 ...

  5. ubuntu14.04+opencv 3.0+python2.7安装及测试

    本文记录了ubuntu下使用源码手动安装opencv的过程.步骤来自opencv官网 此外记录了在python中安装及载入opencv的方法. 1.安装opencv所需的库(编译器.必须库.可选库) ...

  6. less学习-语法(二)

    变量 @color1:#fff; 选择器  // Variables @mySelector: banner; // Usage .@{mySelector} { font-weight: bold; ...

  7. XStream简单使用01——xml和Ojbect互转

    package org.zhb.test; /** * author : zhb * data : 2014-2-14 * use packages: * xmlpull-1.1.3.1.jar * ...

  8. wpf+xml实现的一个随机生成早晚餐的小demo

    话说每到吃完的时间就发愁,真的不知道该吃什么,然后就想到做一个生成吃什么的小软件,既然这个软件如此的简单,就打算用wpf开发吧,也不用数据库了,直接保存在xml中就可以了 程序整体结构如下图 首先我写 ...

  9. Swift开发之 ---- Swift宏定义

    swift中没有了#Define这种宏定义了,可以用let来声明常量来取代,判断当前系统版本 let IS_IOS7 = (UIDevice.currentDevice().systemVersion ...

  10. No Hibernate Session bound to thread, and configuration does not allow creat

    No Hibernate Session bound to thread, and configuration does not allow creat 今天遇到这么一个错误,在网上差了很多都没有能解 ...