做了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. PHP实现中文简体字和繁体字互转

    function convert($str, $action='S'){ if($action != 'S' && $action != 'T'){ return $str; } $s ...

  2. FireFox不支持InnerText的解决方法

    innerText和InnerHTML是非常实用的一个属性,然而在FireFox中不支持此属性,可能是因为考虑到网页的安全性.这样一来为开发者带来了不少麻烦.FireFox中也提供了另外一个属性inn ...

  3. (转载)用SQL语句创建Access表

    <来源网址:http://www.delphifans.com/infoview/Article_220.html>用SQL语句创建Access表 很久以前弄的,用了一天的时间,没有什么技 ...

  4. 【CSDN人物访谈】蒋守壮分享他的技术成长之路以及对Hive技术的解读与思考

    结缘大数据技术 CSDN:请简单地介绍一下自己. 蒋守壮:首先非常感谢CSDN能够给我这次被专访的机会,可以让我重新审视自己的职业发展历程,也希望能够帮助一些同行的朋友们.目前就职万达网络科技集团有限 ...

  5. Random类(java.util)

    转自 Random类中实现的随机算法是伪随机,也就是有规则的随机.在进行随机时,随机算法的起源数字称为种子数(seed),在种子数的基础上进行一定的变换,从而产生需要的随机数字. 相同种子数的Rand ...

  6. 配置node与express初试

    http://www.nodejs.org/下载对应系统的node版本并安装 用npm包管理器安装需要的包 sudo npm install -g express sudo npm install - ...

  7. iframe 刷新

    iframe刷新父页面 parent.location.reload(); iframe 一个子页面操作过后,刷新指定子页面 parent.frames('ifrmname').location.re ...

  8. 【git】借助github学习成果

    1.创建分支 git branch dev   //创建分支 git checkout dev  //切换分支 或者用一句  git checkout -b dev 删除一个分支  git check ...

  9. response 返回 带双引号 的字符串解决办法 springmvc

    springmvc json配置 返回的时候给你加上了

  10. Extjs4.2——bbar的默认类型(xtype)

    bbar:在Panel经常使用的工具栏 如下面的示例——这将牵涉本文要追寻的问题:在下面的Panel中的bbar第一个组件明确指定的xtype:'button',第二个没有明确指出,那么它是何类型,为 ...