POJ 2435Navigating the City(bfs)
题意:给你一个地图,’+’代表十字路口,‘-’‘|’表示街道,‘.’表示建筑物,‘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)的更多相关文章
- POJ 2312:Battle City(BFS)
Battle City Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9885 Accepted: 3285 Descr ...
- 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, ...
- poj 3669 Meteor Shower(bfs)
Description Bessie hears that an extraordinary meteor shower is coming; reports say that these meteo ...
- 【POJ - 3414】Pots(bfs)
Pots 直接上中文 Descriptions: 给你两个容器,分别能装下A升水和B升水,并且可以进行以下操作 FILL(i) 将第i个容器从水龙头里装满(1 ≤ i ≤ 2); DRO ...
- POJ 1573 Robot Motion(BFS)
Robot Motion Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 12856 Accepted: 6240 Des ...
- POJ 3126 Prime Path (BFS)
[题目链接]click here~~ [题目大意]给你n,m各自是素数,求由n到m变化的步骤数,规定每一步仅仅能改变个十百千一位的数,且变化得到的每个数也为素数 [解题思路]和poj 3278类似.b ...
- 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 ...
- POJ 3221 Diamond Puzzle(BFS)
Description A diamond puzzle is played on a tessellated hexagon like the one shown in Figure 1 below ...
- 题解报告:poj 3669 Meteor Shower(bfs)
Description Bessie hears that an extraordinary meteor shower is coming; reports say that these meteo ...
随机推荐
- 【BZOJ 1189】[HNOI2007]紧急疏散evacuate
Description 发生了火警,所有人员需要紧急疏散!假设每个房间是一个N M的矩形区域.每个格子如果是'.',那么表示这是一块空地:如果是'X',那么表示这是一面墙,如果是'D',那么表示这是一 ...
- js模拟触发事件
html标签元素封装着实用的[事件],但在很多时候,需要[模拟触发事件],比如 [按钮单机事件] 可以实实在在点击按钮触发该事件,但体验而言,很多时候需要js逻辑处理让实现 触发事件的效果这时就用 ...
- java和php实现RSA加密互通-b
java和PHP RSA加密实现互通 1:通过openssl 生成公钥和密钥文件(linux) (1) 生产私钥文件命令 openssl genrsa -out rsa_private_key.pe ...
- C#学习笔记---基础入门(一)
C#中的变量: 一个变量就是存储区(内存)中的一个存储单元. 变量声明赋值:int money =1000;/int money;money=1000; 输出:console.writeLine(mo ...
- [转]用Python做一个自动生成读表代码的小脚本
写在开始(本片文章不是写给小白的,至少你应该知道一些常识!) 大家在Unity开发中,肯定会把一些数据放到配置文件中,尤其是大一点的项目,每次开发一个新功能的时候,都要重复的写那些读表代码.非常烦.来 ...
- JS日期格式化(网上转载)
JS日期格式化(网上转载) <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <ht ...
- Mapped Statements collection does not contain value for
这是由pojo的映射文件的命名空间引起的错误. 按照以下格式即可:命名空间中一定要含有mapper.xxMapper这样的格式,否则出现以上错误. <?xml version="1.0 ...
- js的基本概念详解
来自<javascript高级程序设计 第三版:作者Nicholas C. Zakas>的学习笔记(三) 如果你刚学js,想快速了解到js的基本概念,以下将会是一篇不错的引导文章: 语法 ...
- 宏 #,##,_ _VA_ARGS_ _
宏里面使用: 一.# 转为字符串 #define PSQR(x) printf("the square of" #x "is %d.\n",(x)*(x)) ...
- hibernate 数据行数统计 count(*)
Hibernate关于sql中的count(*)数据统计: ①如果使用的是HQL: 直接在HQL中使用count(*)即可获取行数 Long count = (Long)HibernateUtil.g ...