题意:给你一个地图,’+’代表十字路口,‘-’‘|’表示街道,‘.’表示建筑物,‘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. 如何安装Favicon

    如何安装Favicon favicon.ico图像放在根目录下(也可以是其他目录)在页面源文件的<head></head>标签之间插入 <link rel="s ...

  2. [转载]C#导入XLS数据到数据库

    Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/--> ...

  3. A geometric interpretation of the covariance matrix

    A geometric interpretation of the covariance matrix Contents [hide] 1 Introduction 2 Eigendecomposit ...

  4. PAT-乙级-1004. 成绩排名 (20)

    1004. 成绩排名 (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 读入n名学生的姓名.学号.成绩,分 ...

  5. SDK更新问题解决

    更新C:\WINDOWS\system32\drivers\etc\host文件百试不爽 第一步 打开SDK Manager下Tools->Options,选中“Force https://… ...

  6. HDU 1494 跑跑卡丁车 (DP)

    题目链接 题意 : 中文题不详述. 思路 : sum = L*N 段,每走过一段如果不用加速卡的话,能量会增20%,将20%看作1,也就是说每涨到15就要变为10,所以最多是14才不浪费. dp[i] ...

  7. HDU4611+数学

    /* 找规律 题意:abs(i%A - i%B) 对i从0~N-1求和 从0~N-1一个一个算必TLE,着A,B两者差相同的部分合并起来算 */ #include<stdio.h> #in ...

  8. MSSQLServer基础01(数据类型)

    数据库设计:范式 现阶段,必须遵守满足3NF 1范式:列的原子性,即列不可再拆分 2范式:表中不能描述多个信息,不能有数据冗余 3范式:引用其它表的主键信息 数据类型的意义: 1>提高效率.(减 ...

  9. Java API ——String类

    1.String类概述 · 字符串是由多个字符组成的一串数据(字符序列),也可以看成是一个字符数组. · 字符串字符值“abc”也可以看成是一个字符串对象. · 字符串是常量,一旦被赋值,就不能被改变 ...

  10. 几种必知的oracle结构图

    一.数据库结构 二.Oracle 内存结构 三.进程结构 1. 用户进程:在数据库用户请求连接到Oracle 服务器时启动 2. 服务器进程:可以连接到Oracle实例,它在用户建立会话时启动 3. ...