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 ...
随机推荐
- submit与button区别提交区别
提交表单时使用submit会自动提交form表单数据, 如果使用jquery的form表单插件时需要将提交按钮改为button时$("#表单id").ajaxSubmit({}); ...
- java 取小数点后两位 不四舍五入,怎么做
java 取小数点后两位 不四舍五入,怎么做 正常版: //正常版: import java.text.DecimalFormat; import java.math.RoundingMode; De ...
- HibernateTemplate、HibernateDaoSupport两种方法实现增删改查Good(转)
Spring+Hibernate两种方法实现增删改查 首先,定义一个Customer的bean类,设置好Customer.hbm.xml文件.再定义好一个Dao接口.准备好一个jdbc.propert ...
- js验证中英文
// 验证中英文 function check_en_ch(_value){ var reg_en_num = /^[0-9A-Za-z\'\"\,\.\!\?\:\s|“|”|‘|’|!| ...
- js和HTML结合(补充知识:如何防止文件缓存的js代码)
来自<javascript高级程序设计 第三版:作者Nicholas C. Zakas>的学习笔记(二) 使用html标签<script>可以把js嵌入到html页面中,让脚本 ...
- UNITY3D使用NGUI制作自适应UI的总结
原地址:http://www.cnitblog.com/updraft/archive/2013/11/12/88801.html 制作自适应的几个方法1. 使用 UIROOT 里设置自定义高度的方法 ...
- 极客范:如何使用 Cloud Insight 来监控闭路电视?
最近新上线支持 Windows 系统及其组件 监控功能的 Cloud Insight,在系统监控领域基本囊括了对所有主流和部分非主流平台的支持.但是这还不够,Cloud Insight 可不仅仅是一个 ...
- zoj 3640 Help Me Escape 概率DP
记忆化搜索+概率DP 代码如下: #include<iostream> #include<stdio.h> #include<algorithm> #include ...
- 【转】Java自动装箱、拆箱、缓冲池
JDK5以后 Integer a = 3; 这是自动装箱int i = new Integer(2); 这是自动拆箱就是基本类型和其对应的包装类型在需要的时候可以互相 ...
- linux ubuntu关于U盘的安装 开机启动u盘的时候出现/casper/vmlinuz.efi: file not found
将u盘下的/casper/vmlinuz文件添加一个后缀.efi即可. 重启再装.