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 ######## #......# #.####.# #.####.# #.####.# ...
随机推荐
- HttpClient 建立http连接,https连接,传输数据文件
package com.bluedon.bsmon.http; import java.io.File; import java.nio.charset.Charset; import java.se ...
- ARM-Linux S5PV210 UART驱动(5)----串口的open操作(tty_open、uart_open)
串口驱动初始化后,串口作为字符驱动也已经注册到系统了,/dev目录下也有设备文件节点了. 那接下来uart的操作是如何进行的呢? 操作硬件之前都是要先open设备,先来分析下这里的open函数具体做了 ...
- H264学习第一篇(编码结构分析)
学习H264之前,最好阅读一下维基百科中有关H264的相关介绍,里面包含了其的发展历程.主要特点.参考文献.参考网站等. 研究H264的主要文件包括两份参考手册(一份是语法结构参考手册,一份是JM开发 ...
- WPF中禁止WebBrowser控件打开新窗口
一.针对纯WPF的WebBrowser控件: <summary> Suppress Script Errors In WPF WebBrowser </summary> pub ...
- 转:深入学习Oracle分区表及分区索引
转自:http://database.ctocio.com.cn/tips/286/8104286.shtml 关于分区表和分区索引(About Partitioned Tables and Inde ...
- WCF 动态生成 不用增加引用两种方式
一.fromwork2.0低版本方式 1,打开vs的命令工具 输入:wsdl wcf地址 + /l:cs /out:文件名 上面红色部分替换掉就行,文件名,你想叫什么文件名都行. 2,回车,生成的文件 ...
- PowerDesigner 非数值默认值时会自动增加单引单
在PowerDesigner中,如果默认值是非数值型的,那么 PowerDesigner 会默认加上单引号 因此我们需要把这个默认的单引号干掉,如果是需要设置字符串默认值的时候,就手工加上 单引号 即 ...
- Keil V4.72升级到V5.1X之后
问题描述 Keil V4.72升级到V5.1x之后,原来编译通过的工程,出现了如下错误: .\Libraries\CMSIS\CM3\DeviceSupport\ST\STM32F10x\STM32f ...
- C# Windows - TabControl
TabControl控件的属性 - 一般用于控制TabPages对象容器的外观,特别是显示的选项卡的外观 属性 说明 Alignment 控制选项卡在选项卡控件的什么位置显示 Appearance 控 ...
- 3D游戏常用技巧Normal Mapping (法线贴图)原理解析——高级篇
1.概述 上一篇博客,3D游戏常用技巧Normal Mapping (法线贴图)原理解析——基础篇,讲了法线贴图的基本概念和使用方法.而法线贴图和一般的纹理贴图一样,都需要进行压缩,也需要生成mipm ...