http://www.lydsy.com/JudgeOnline/problem.php?id=1687

bfs后然后逆向找图即可。因为题目保证最短路唯一

#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
#define rep(i, n) for(int i=0; i<(n); ++i)
#define for1(i,a,n) for(int i=(a);i<=(n);++i)
#define for2(i,a,n) for(int i=(a);i<(n);++i)
#define for3(i,a,n) for(int i=(a);i>=(n);--i)
#define for4(i,a,n) for(int i=(a);i>(n);--i)
#define CC(i,a) memset(i,a,sizeof(i))
#define read(a) a=getint()
#define print(a) printf("%d", a)
#define dbg(x) cout << #x << " = " << x << endl
#define printarr2(a, b, c) for1(i, 1, b) { for1(j, 1, c) cout << a[i][j]; cout << endl; }
#define printarr1(a, b) for1(i, 1, b) cout << a[i]; cout << endl
inline const int getint() { int r=0, k=1; char c=getchar(); for(; c<'0'||c>'9'; c=getchar()) if(c=='-') k=-1; for(; c>='0'&&c<='9'; c=getchar()) r=r*10+c-'0'; return k*r; }
inline const int max(const int &a, const int &b) { return a>b?a:b; }
inline const int min(const int &a, const int &b) { return a<b?a:b; } const int N=100, Q=N*N, dx[]={-1, 1, 0, 0}, dy[]={0, 0, -1, 1};
int mp[N][N], n, m, f[N][N], front, tail, ans1[N], ans2[N], cnt, X, Y, XX, YY;
char s[N];
struct dat { int x, y; }q[Q];
void bfs() {
front=tail=0;
q[tail].x=X, q[tail++].y=Y;
CC(f, 0x3f); f[X][Y]=0;
while(tail!=front) {
dat &t=q[front++]; if(front==Q) front=0;
int x=t.x, y=t.y, d=f[x][y];
rep(i, 4) {
int fx=dx[i]+x, fy=dy[i]+y;
if(fx<1 || fy<1 || fx>n || fy>m || !mp[fx][fy] || f[fx][fy]<=d+1) continue;
f[fx][fy]=d+1;
q[tail].x=fx, q[tail++].y=fy; if(tail==Q) tail=0;
}
}
int x=XX, y=YY, now=-1, tot=0;
while(!(x==X && y==Y)) {
rep(i, 4) {
int fx=dx[i]+x, fy=dy[i]+y;
if(fx<1 || fy<1 || fx>n || fy>m || !mp[fx][fy] || f[fx][fy]!=f[x][y]-1) continue;
if(mp[fx][fy]==1) ++tot;
if(now!=i && now!=-1) {
ans1[++cnt]=now;
ans2[cnt]=tot;
tot=0;
}
now=i;
x=fx, y=fy;
}
}
ans1[++cnt]=now; ans2[cnt]=tot+1;
} int main() {
read(n); read(m); n=n*2-1, m=m*2-1;
for1(i, 1, n) {
scanf("%s", s+1);
for1(j, 1, m) {
if(s[j]=='+') mp[i][j]=1;
else if(s[j]=='S') X=i, Y=j, mp[i][j]=3;
else if(s[j]=='E') XX=i, YY=j, mp[i][j]=4;
else if(s[j]=='.') mp[i][j]=0;
else mp[i][j]=2;
}
}
bfs();
for3(i, cnt, 1) {
char c='H';
if(ans1[i]==2) c='E';
else if(ans1[i]==3) c='W';
else if(ans1[i]==1) c='N';
else if(ans1[i]==0) c='S';
printf("%c %d\n", c, ans2[i]);
}
return 0;
}

Description

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

Input

第1行:两个用空格隔开的整数N和E.

第2到2N行:每行有2E-I个字符,表示地图.

Output

每行有一个表示方向的字母和一个表示要开几个十字路口的数字表示.

Sample Input

Sample Input

 

Sample Output

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

HINT

Source

【BZOJ】1687: [Usaco2005 Open]Navigating the City 城市交通(bfs)的更多相关文章

  1. Bzoj 1687: [Usaco2005 Open]Navigating the City 城市交通 广搜,深搜

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

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

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

  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 1671: [Usaco2005 Dec]Knights of Ni 骑士 (bfs)

    题目: https://www.lydsy.com/JudgeOnline/problem.php?id=1671 题解: 按题意分别从贝茜和骑士bfs然后meet_in_middle.. 把一个逗号 ...

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

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

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

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

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

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

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

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

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

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

随机推荐

  1. webDriver API——第9部分Firefox WebDriver

    class selenium.webdriver.firefox.webdriver.WebDriver(firefox_profile=None, firefox_binary=None, time ...

  2. 优秀运维人员20道必会iptables面试题(转载)

    (一)企业面试口试题 1.详述iptales工作流程以及规则过滤顺序? 2.iptables有几个表以及每个表有几个链? 3.iptables的几个表以及每个表对应链的作用,对应企业应用场景? 4.画 ...

  3. ZK框架笔记4、通用组件、页面、桌面

    组件(component)是一种用户接口(UI)对象,如一个标签.按钮.树.         页面(page)是一个组件的集合.         桌面(desktop)是一个包含相同URL请求的页面. ...

  4. 记一次.net core调用SOAP接口遇到的问题

    背景        最近需要将一些外部的Web Service及其他SOAP接口的调用移到一个独立的WebAPI项目中,然后供其他.Net Core项目调用.之前的几个Web Service已经成功迁 ...

  5. DEDECMS 添加栏目图片

    当我们一个栏目列表都用缩略图来表示产,而不仅仅只是文字,如果没有这项功能会非常麻烦,网上有很多这方面的资料,但是都试过了有很多问题,自己研究一下,测试基本通过.需要新加字段 typeimg 后台执行S ...

  6. Redis 实例排除步骤

    Redis 应用案例 - 在问题中不断成长 原创 2017-02-05 杜亦舒  本文翻译整理自 Andy Grunwald 发布的一篇文章,写的是作者所在公司使用 Redis 时遇到的问题,以及处理 ...

  7. mybatis想要在控制台显示sql语句配置文件

    在src目录下创建一个properties文件 配置内容如下 log4j.rootLogger=debug,stdout,logfile log4j.appender.stdout=org.apach ...

  8. Resources.FindObjectsOfTypeAll<T>()的坑(Ghost prefab)

    今天遇到了一个Bug,因为调用Resources.FindObjectsOfTypeAll<T>()遍历整个场景,结果遍历出的对象不对.比较哈希一查果然是两个.原来prefab本身和pre ...

  9. node-nginx二级域名添加配置

    首先在阿里云配置解析域名 指向端口为3200的nodejs服务,在nginx/conf.d下增加文件chat.conf,内容如下: server { listen 80; server_name ww ...

  10. 【Android】利用Fiddler进行抓包详解教程。抓取接口以及数据,可以抓真实安卓手机或者模拟器。

    大家都知道抓包的方法很多.我这里给大家介绍介绍一种,利用fiddler进行抓包,当然比如Wireshark也可以抓包,我们这里不做介绍.我这里演示的是fiddler+天天模拟器,当然真实安卓手机也是一 ...