poj 3083 Children of the Candy Corn(DFS+BFS)
做了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)的更多相关文章
- POJ 3083 -- Children of the Candy Corn(DFS+BFS)TLE
POJ 3083 -- Children of the Candy Corn(DFS+BFS) 题意: 给定一个迷宫,S是起点,E是终点,#是墙不可走,.可以走 1)先输出左转优先时,从S到E的步数 ...
- POJ 3083:Children of the Candy Corn(DFS+BFS)
Children of the Candy Corn Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9311 Accepted: ...
- poj 3083 Children of the Candy Corn (广搜,模拟,简单)
题目 靠墙走用 模拟,我写的是靠左走,因为靠右走相当于 靠左走从终点走到起点. 最短路径 用bfs. #define _CRT_SECURE_NO_WARNINGS #include<stdio ...
- poj 3083 Children of the Candy Corn
点击打开链接 Children of the Candy Corn Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8288 ...
- POJ 3083 Children of the Candy Corn bfs和dfs
Children of the Candy Corn Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8102 Acc ...
- 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 ...
- POJ 3083 Children of the Candy Corn (DFS + BFS)
POJ-3083 题意: 给一个h*w的地图. '#'表示墙: '.'表示空地: 'S'表示起点: 'E'表示终点: 1)在地图中仅有一个'S'和一个'E',他们为位于地图的边墙,不在墙角: 2)地图 ...
- POJ 3083 Children of the Candy Corn (DFS + BFS + 模拟)
题目链接:http://poj.org/problem?id=3083 题意: 这里有一个w * h的迷宫,给你入口和出口,让你分别求以下三种情况时,到达出口的步数(总步数包括入口和出口): 第一种: ...
- poj 3083 Children of the Candy Corn 【条件约束dfs搜索 + bfs搜索】【复习搜索题目一定要看这道题目】
题目地址:http://poj.org/problem?id=3083 Sample Input 2 8 8 ######## #......# #.####.# #.####.# #.####.# ...
随机推荐
- spring-cloud-turbine
turbine主要用于聚合hystrix的监控数据 依赖pom <dependencyManagement> <dependencies> <dependency> ...
- 彻底删除sql server的方法
请先确定是否把sql相关的东西删了,建议进行如下操作. 1.先下个Windows Install Clean Up,清理sql相关东西,要全部清理. 2.到控制面板--添加删除程序中看是否还有未删的. ...
- (转)Mac OS X内核编程,MAC驱动开发资源汇总
一.Mac OS X内核编程开发官方文档: I/O Kit Fundamentals: I/O Kit基础 - Mac OS X系统内核编程 https://developer.apple.com ...
- wancms从apache迁移至nginx
首先解决nginx1.2不支持pathinfo问题,见上一篇博文 其次是数据库的用户名变了之后,修改各种配置,wancms的,ucenter的,bbs的 还有一个是wacms的后台站点管理里面的uc配 ...
- C#微信登录-手机网站APP应用
要求:公众号必须先认证,认证费用¥300/年,比较黑 一.微信登录核心代码 //核心代码,没判断异常 1.登录页面 protected void Page_Load(object sender, Ev ...
- springMVC上传图片
http://blog.csdn.net/cheung1021/article/details/7084673/ http://toutiao.com/a6293854906445021442/ 工程 ...
- Firefly的角色跳转场景简单示例
源地址:http://bbs.9miao.com/thread-45790-1-2.html 本例演示的是模拟游戏服务端,让角色在场景1中跳转到场景2中.在实际游戏中,client将要跳转的角色id和 ...
- ValueError: Attempted relative import in non-package
执行:python deom/scripts/populate.py ValueError: Attempted relative import in non-package solve:python ...
- Android ListView相关 fastScrollEnabled
1.android:fastScrollEnabled="true" 当数据量比较多的时候,右侧会出现一个可以拉动的滚动条,这样可以很快的拉动,如图:
- http://jingyan.baidu.com/article/a378c960630e61b329283045.html
http://jingyan.baidu.com/article/a378c960630e61b329283045.html