【BZOJ】1687: [Usaco2005 Open]Navigating the City 城市交通(bfs)
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
Input
第1行:两个用空格隔开的整数N和E.
第2到2N行:每行有2E-I个字符,表示地图.
Output
每行有一个表示方向的字母和一个表示要开几个十字路口的数字表示.
Sample Input
Sample Input
Sample Output
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)的更多相关文章
- Bzoj 1687: [Usaco2005 Open]Navigating the City 城市交通 广搜,深搜
1687: [Usaco2005 Open]Navigating the City 城市交通 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 122 So ...
- 1687: [Usaco2005 Open]Navigating the City 城市交通
1687: [Usaco2005 Open]Navigating the City 城市交通 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 94 Sol ...
- 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 1671: [Usaco2005 Dec]Knights of Ni 骑士 (bfs)
题目: https://www.lydsy.com/JudgeOnline/problem.php?id=1671 题解: 按题意分别从贝茜和骑士bfs然后meet_in_middle.. 把一个逗号 ...
- 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 ...
随机推荐
- nginx安装说明
下载地址:http://nginx.org/en/download.html 安装版本:1.10.0 安装配置如下: /etc/nginx 目录 /home/nginx目录 --prefix=/hom ...
- Win7如何开机直接进桌面
运行CONTROL USERPASSWORDS2 取消登陆要密码那项后再点应用,直接输入密码下次就能自己登陆进入桌面啦
- sip
INVITE sip:10010101402@10.7.36.70:5060 SIP/2.0 Via: SIP/2.0/UDP 10.7.36.250:5060;rport;branch=z9hG4b ...
- 【转】C++ 虚函数&纯虚函数&抽象类&接口&虚基类
1. 动态多态 在面向对象语言中,接口的多种不同实现方式即为多态.多态是指,用父类的指针指向子类的实例(对象),然后通过父类的指针调用实际子类的成员函数. 多态性就是允许将子类类型的指针赋值给父类类型 ...
- 【转】java与C++的区别
转自:http://club.topsage.com/thread-265349-1-1.html Java并不仅仅是C++语言的一个变种,它们在某些本质问题上有根本的不同: (1)Java比C++程 ...
- Unity命令行模式,也能「日志实时输出」
转自自己的简书:http://www.jianshu.com/p/bd97cb8042a9 如果你使用过Unity命令行模式(batchmode),来实现Unity自动化编译构建,你肯定会遇到过这样的 ...
- python selenium --处理下拉框
下拉框是我们最常见的一种页面元素,对于一般的元素,我们只需要一次就定位,但下拉框里的内容需要进行两次定位,先定位到下拉框,再定位到下拉框内里的选项. drop_down.html <html&g ...
- mysql数据库中不能插入0000-00-00 00:00:00日期数据(报错Invalid datetime format: 1292 Incorrect datetime value: '0000-00-00 00:00:00')
报错信息 SQLSTATE[22007]: Invalid datetime format: 1292 Incorrect datetime value: '0000-00-00 00:00:00' ...
- google_gflags使用
gflags是google开源的一个解析命令行参数的工具. 最简单的demo #include <iostream> #include <gflags/gflags.h> us ...
- java的锁池和等待池
谢邀.不知道题中的一段文字出自何处.“锁池”和“等待池”这种翻译我还是头一回见.不过,题主的思路已经对了,即不拘泥于文字,而是在考虑这两个东西在锁的调度(即决定哪个线程可以获得锁的过程)中起到什么作用 ...