题意:给你一个地图,’+’代表十字路口,‘-’‘|’表示街道,‘.’表示建筑物,‘s’,’E’ 起点和终点。输出从起点到终点的的 最短路径(包括方向和沿该方向的经过的十字路口数)

分析:ans[i][j],起点到(i,j)点的最短路径,bfs求出所有,再从终点回推到起点得最短路径。

#include <map>
#include <set>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <cstdio>
#include <vector>
#include <string>
#include <cctype>
#include <complex>
#include <cassert>
#include <utility>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
typedef pair<int,int> PII;
typedef long long ll;
#define lson l,m,rt<<1
#define pi acos(-1.0)
#define rson m+1,r,rt<<11
#define All 1,N,1
#define read freopen("in.txt", "r", stdin)
const ll INFll = 0x3f3f3f3f3f3f3f3fLL;
const int INF= 0x7ffffff;
const int mod = ;
struct point{
int x,y;
};
int ans[][],n,m,mapc[][];
int sx,sy,ex,ey;
int dir[][]={{,},{,},{,-},{-,}};
int dirc[],dept[],index=;
void bfs(){
point a,b;
memset(ans,0x3f,sizeof(ans));
a.x=sx;a.y=sy;
ans[sx][sy]=;
queue<point>q;
q.push(a);
while(!q.empty()){
b=q.front();
q.pop();
if(b.x==ex&&b.y==ey){
break;
}
for(int i=;i<;++i){
int xx=b.x+dir[i][];
int yy=b.y+dir[i][];
if(xx<||xx>=n||yy<||yy>=m||!mapc[xx][yy]||ans[xx][yy]<=ans[b.x][b.y]+)
continue;
ans[xx][yy]=ans[b.x][b.y]+;
a.x=xx;
a.y=yy;
q.push(a);
}
}
//逆向求路径
int tx=ex,ty=ey,ncase=-,num=;
while(tx!=sx||ty!=sy){
for(int i=;i<;++i){
int xx=tx+dir[i][];
int yy=ty+dir[i][];
if(xx<||xx>=n||yy<||yy>=m||!mapc[xx][yy]||ans[xx][yy]!=ans[tx][ty]-)continue;
if(mapc[xx][yy]==)
num++;
if(ncase!=i&&ncase!=-){
dirc[++index]=ncase;
dept[index]=num;
num=;
}
ncase=i;
tx=xx;
ty=yy;
}
}
dirc[++index]=ncase;
dept[index]=num+;
}
int main()
{
char dic[]={'W','N','E','S'};
scanf("%d%d",&n,&m);
n=*n-;
m=*m-;
char ch;
for(int i=;i<n;++i)
for(int j=;j<m;++j)
{
cin>>ch;
if(ch=='.')mapc[i][j]=;
else if(ch=='+')mapc[i][j]=;
else if(ch=='S'){mapc[i][j]=;sx=i;sy=j;}
else if(ch=='E'){mapc[i][j]=;ex=i;ey=j;}
else mapc[i][j]=;
}
/*for(int i=0;i<n;++i){
for(int j=0;j<m;++j)
cout<<mapc[i][j]<<" ";
cout<<endl;
}*/
bfs();
for(int i=index;i>=;--i)
printf("%c %d\n",dic[dirc[i]],dept[i]);
return ;
}

POJ 2435Navigating the City(bfs)的更多相关文章

  1. POJ 2312:Battle City(BFS)

    Battle City Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9885   Accepted: 3285 Descr ...

  2. POJ 3984 迷宫问题(BFS)

    迷宫问题 Description 定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, ...

  3. poj 3669 Meteor Shower(bfs)

    Description Bessie hears that an extraordinary meteor shower is coming; reports say that these meteo ...

  4. 【POJ - 3414】Pots(bfs)

    Pots 直接上中文 Descriptions: 给你两个容器,分别能装下A升水和B升水,并且可以进行以下操作 FILL(i)        将第i个容器从水龙头里装满(1 ≤ i ≤ 2); DRO ...

  5. POJ 1573 Robot Motion(BFS)

    Robot Motion Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 12856   Accepted: 6240 Des ...

  6. POJ 3126 Prime Path (BFS)

    [题目链接]click here~~ [题目大意]给你n,m各自是素数,求由n到m变化的步骤数,规定每一步仅仅能改变个十百千一位的数,且变化得到的每个数也为素数 [解题思路]和poj 3278类似.b ...

  7. poj 2251 Dungeon Master(bfs)

    Description You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is co ...

  8. POJ 3221 Diamond Puzzle(BFS)

    Description A diamond puzzle is played on a tessellated hexagon like the one shown in Figure 1 below ...

  9. 题解报告:poj 3669 Meteor Shower(bfs)

    Description Bessie hears that an extraordinary meteor shower is coming; reports say that these meteo ...

随机推荐

  1. C# - 高级方法参数

    可选参数 -必须有个默认值,默认值必须是字面值,常量值,新对象实例或者默认值类型值. public List<string> GetWords( string sentence, bool ...

  2. 2338: [HNOI2011]数矩形 - BZOJ

    因为已经看了一眼题解,知道是算中点和长度,相同时构成一个矩形,所以就把所有的线段算出来,然后排序,相同的就更新答案 为了避免误差,我们都用整数存,中点直接相加就行了,没必要除2,长度也只要平方就行了, ...

  3. [Oracle]any, all解析

    因为很少用到, 所以几乎忘记了这几个函数, 不过它们还是很有用的使用它们可以大大简化一些SQL文的语法, 至于效率问题, 如CCW所说它们和EXISTS, IN 之类没有什么差别, 而且要具体问题具体 ...

  4. 从List[Future[T]]到Future[List[T]]

    在课程<Principles Of Reactive Programming>里Week3的一节 "Promises, promises, promises"中,Eri ...

  5. PreparedStatement的用法

    转载:http://www.cnblogs.com/raymond19840709/archive/2008/05/12/1192948.html jdbc(java database connect ...

  6. Jmeter java协议测试jar和依赖jar的路径研究

    昨天在使用2.12版本的jmeter时,执行多机负载测试一直报错,最终查明是使用不当的问题,现将详情记录如下,使用jmeter测试java协议脚本时要注意以下几点: 1. jar包的方式路径一定是这样 ...

  7. 【BZOJ 1046】 1046: [HAOI2007]上升序列

    1046: [HAOI2007]上升序列 Description 对于一个给定的S={a1,a2,a3,-,an},若有P={ax1,ax2,ax3,-,axm},满足(x1 < x2 < ...

  8. UR #13 Ernd

    考试的时候没有注意到可以将(a,b)放在二维平面上之后旋转坐标系,使得转移变成树状数组二维偏序 这样就算我想出来了第二个转移的斜率优化也没有什么卵用啊(摔西瓜 设g(i)表示当前站在第i个水果下面且第 ...

  9. MapReduce编程系列 — 3:数据去重

    1.项目名称: 2.程序代码: package com.dedup; import java.io.IOException; import org.apache.hadoop.conf.Configu ...

  10. laravel Authentication and Security

    Creating the user modelFirst of all, we need to define the model that is going to be used to represe ...