题意:给你一个地图,’+’代表十字路口,‘-’‘|’表示街道,‘.’表示建筑物,‘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. Android水平(横向)翻页列表,类似水平GridVIew

    Android水平(横向)翻页列表,类似于水平方向的GridView,行列自定义,但要翻页切换,考虑加载性能,当Item数据很多时加载和翻页要流畅,翻页时要有动画效果,效果图如下: 实现方式: 1:翻 ...

  2. 解决mybatis查询返回结果值串查

    方式一: 通过as 指定 大写重名列的 别名 方式二: 命名数据库中表名时 每个表的主键 id 要起不同的名称, 避免主键重复(但是子表的外键可以和主表的id主键重名, 你想啊, 从表的外键性质不就是 ...

  3. 安装oracle 11g详细过程仅供参考

  4. [转载]MongoDB优化的几点原则

    .查询优化 确认你的查询是否充分利用到了索引,用explain命令查看一下查询执行的情况,添加必要的索引,避免扫表操作. .搞清你的热数据大小 可能你的数据集非常大,但是这并不那么重要,重要的是你的热 ...

  5. jquery判断对象是否获得焦点

    var isFocus=$("#tRow").is(":focus"); if(true==isFocus){ alert("focus") ...

  6. JAVA面试题:69道Spring面试题和答案

    目录 Spring 概述 依赖注入 Spring beans Spring注解 Spring数据访问 Spring面向切面编程(AOP) Spring MVC Spring 概述 1. 什么是spri ...

  7. 将 Qt 5.6 集成至 VS2015

    摘要: 由于VS2015不再支持addin,所以要用其他手段. 这里给出64位系统下的安装步骤,32位类似. 一.安装VS2015 过程略.值得注意的是要选择需要安装的内容,既然要用Qt,那么C++相 ...

  8. python脚本工具 - 3 目录遍历

    遍历系统中某一目录下的所有文件名 #! /usr/bin/python # coding:utf-8 import os def dirList(path): filelist = os.listdi ...

  9. Java API ——包装类

    1.包装类的概述 · 将基本数据类型封装成对象的好处在于可以在对象中定义更多的功能方法操作该数据. · 常用的操作之一:用于基本数据类型与字符串之间的转换. · 基本类型和包装类的对应 为了对基本数据 ...

  10. How to download apk for google play online?

    http://apps.evozi.com/apk-downloader/ Online APK Downloader http://apkpure.com/ apkpure http://www.c ...