做了1天,总是各种错误,很无语

最后还是参考大神的方法

题目:http://poj.org/problem?id=3083

题意:从s到e找分别按照左侧优先和右侧优先的最短路径,和实际的最短路径

DFS找左右侧 的最短路径

 #include <iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<stack>
#include<queue>
#include<cmath>
#include<algorithm>
using namespace std;
char G[][];
int vis[][];
int c,r;
struct node
{
int x,y,dis;
}s,pos,next; int dx[]= {,-,,};
int dy[]= {-,,,};
int dfs(int x,int y,int f,char ch)//这题dfs是重点,f表示当前的朝向
{
int i,j,nx,ny,nf;
if(G[x][y]=='E')
return ;
if(ch=='l')
{
for(i=f-; i<f+; i++)
{
nx=x+dx[(i+)%];
ny=y+dy[(i+)%];
nf=(i+)%;
if(nx>=&&nx<r&&ny>=&&ny<c&&G[nx][ny]!='#')
return dfs(nx,ny,nf,ch)+;
}
}
else if(ch=='r')
{
for(i=f+; i>f-; i--)
{
nx=x+dx[(i+)%];
ny=y+dy[(i+)%];
nf=(i+)%;
if(nx>=&&nx<r&&ny>=&&ny<c&&G[nx][ny]!='#')
return dfs(nx,ny,nf,ch)+;
}
}
return ;
} int bfs(int x,int y)
{
queue<node>q;
int i;
memset(vis,,sizeof(vis));
next.dis=; next.x=x; next.y=y;
vis[x][y]=;
q.push(next);
while(!q.empty())
{
pos=q.front();
q.pop();
for(i=; i<; i++)
{
next.x=pos.x+dx[i]; next.y=pos.y+dy[i];
next.dis=pos.dis+;
if(next.x>=&&next.x<r&&next.y>=&&next.y<c&&G[next.x][next.y]!='#'&&vis[next.x][next.y]==)
{
q.push(next);
vis[next.x][next.y]=;
if(G[next.x][next.y]=='E')
return next.dis;
}
}
}
return ;
}
int main()
{
int i,j,t;
int le,ri,f,ans,flag;
cin>>t;
while(t--)
{
cin>>c>>r;
for(i=; i<r; i++)
cin>>G[i];
flag=;
for(i=; i<r; i++)
{
for(j=; j<c; j++)
if(G[i][j]=='S')
{
s.x=i;
s.y=j;
s.dis=;
if(i==)
f=;
else if(i==r-)
f=;
else if(j==)
f=;
else if(j==c-)
f=;
flag=;
break;
}
if(flag)
break;
}
le=dfs(s.x,s.y,f,'l');
ri=dfs(s.x,s.y,f,'r');
ans=bfs(s.x,s.y);
printf("%d %d %d\n",le,ri,ans);
}
return ;
}

poj 3083 Children of the Candy Corn(DFS+BFS)的更多相关文章

  1. POJ 3083 -- Children of the Candy Corn(DFS+BFS)TLE

    POJ 3083 -- Children of the Candy Corn(DFS+BFS) 题意: 给定一个迷宫,S是起点,E是终点,#是墙不可走,.可以走 1)先输出左转优先时,从S到E的步数 ...

  2. POJ 3083:Children of the Candy Corn(DFS+BFS)

    Children of the Candy Corn Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9311 Accepted: ...

  3. poj 3083 Children of the Candy Corn (广搜,模拟,简单)

    题目 靠墙走用 模拟,我写的是靠左走,因为靠右走相当于 靠左走从终点走到起点. 最短路径 用bfs. #define _CRT_SECURE_NO_WARNINGS #include<stdio ...

  4. poj 3083 Children of the Candy Corn

    点击打开链接 Children of the Candy Corn Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8288 ...

  5. POJ 3083 Children of the Candy Corn bfs和dfs

      Children of the Candy Corn Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8102   Acc ...

  6. POJ:3083 Children of the Candy Corn(bfs+dfs)

    http://poj.org/problem?id=3083 Description The cornfield maze is a popular Halloween treat. Visitors ...

  7. POJ 3083 Children of the Candy Corn (DFS + BFS)

    POJ-3083 题意: 给一个h*w的地图. '#'表示墙: '.'表示空地: 'S'表示起点: 'E'表示终点: 1)在地图中仅有一个'S'和一个'E',他们为位于地图的边墙,不在墙角: 2)地图 ...

  8. POJ 3083 Children of the Candy Corn (DFS + BFS + 模拟)

    题目链接:http://poj.org/problem?id=3083 题意: 这里有一个w * h的迷宫,给你入口和出口,让你分别求以下三种情况时,到达出口的步数(总步数包括入口和出口): 第一种: ...

  9. poj 3083 Children of the Candy Corn 【条件约束dfs搜索 + bfs搜索】【复习搜索题目一定要看这道题目】

    题目地址:http://poj.org/problem?id=3083 Sample Input 2 8 8 ######## #......# #.####.# #.####.# #.####.# ...

随机推荐

  1. oracle分页与rownum

    Oracle分页(limit方式的运用) Oracle不支持类似于 MySQL 中的 limit. 但你还是可以rownum来限制返回的结果集的行数. 第一种 select * from a_matr ...

  2. WPF多线程演示

    WPF中的几种处理线程的工作方式: 1.简单的DispatcherTimer类似Timer控件 2.需要处理UI同步时,Dispatcher DispatcherOpertion 3.增强的Threa ...

  3. RCP学习笔记

    一些model特征: Trimmed Window: 带最小化最大化的窗体 Perspective Stack: 装载Perspective的容器 Perspective:一个透视,可以直接包含Par ...

  4. Java实现MySQL在线管理

    问题: 1.如何实现在线创建MySQL数据库.表: 2.如何实现在线执行sql语句? 思路: 1.可以通过java.sql.Statement类的executeUpdate(String sql)方法 ...

  5. Win7 64bit 成功安装ArcView3.X

    本人参考 链接 已在Win7 64Bit 笔记本上成功安装ArcView3.3,于是记录以下心得。 Win7 64Bit安装不了ArcView3.X的原因在于: 1,ArcView3.X属于16Bit ...

  6. 【cheerio】nodejs的抓取页面模块

    http://baike.baidu.com/link?url=8V1CZsEzNE05ujOzISquom_pvFj16sWu1rRb8js11pmd9HNq7ePW_aKfG9oyXj6Txuu5 ...

  7. 原生JS的对象常用操作总结

       前端时间写了篇怎么判断js对象相等的文章,一直在期待大神给点消息,无奈一直杳无音讯,还是自己写个函数来进行判断,下面总结一些常用的对象操作的方法.    咋们来个先抑后扬的方式,先放出几个基本的 ...

  8. python 开发webService

    最近在学习用python 开发webservice,费了半天时间把环境搭好,记录下具体过程,以备后用. 首先系统上要有python.其次要用python进行webservice开发,还需要一些库: 1 ...

  9. Delphi XE5 android popumenu

    实现下拉菜单式的效果,本代码是国外的网站上下载的..,不是原创. 源码下载地址 :  http://files.cnblogs.com/nywh2008/popumenu.rar

  10. 你不需要jQuery(二)

    完全没有否定jQuery的意思,jQuery是一个神奇的.非常有用的工具,可以节省我们大量的时间. 但是,有些时候,我们只需要jQuery的一个小功能,来完成一个小任务,完全没有必要加载整个jQuer ...