poj 3083 Children of the Candy Corn 【条件约束dfs搜索 + bfs搜索】【复习搜索题目一定要看这道题目】
题目地址:http://poj.org/problem?id=3083
Sample Input
2
8 8
########
#......#
#.####.#
#.####.#
#.####.#
#.####.#
#...#..#
#S#E####
9 5
#########
#.#.#.#.#
S.......E
#.#.#.#.#
#########
Sample Output
37 5 5
17 17 9
题目分析:T组数据,每组都会有一个起点S,一个终点E。 分别输出:左边优先搜索到E的步数 右边优先搜索到E的步数 最短步数到E
dfs搜索时候,控制好搜索下一个节点时的先后顺序。bfs找最短路最简单。
代码:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <math.h>
#include <iostream>
#include <string>
#include <queue>
#include <stack>
#include <vector>
#include <algorithm> using namespace std;
int n, m;
char g[50][50]; struct node
{
int x,y;
}S, E; bool ok(int x, int y) //判断是否出了边界
{
if(x>=0 && x<n && y>=0 && y<m) return true;
else return false;
} //定义转向序列
int d[4][2]={ {0,-1},{-1,0},{0,1},{1,0} }; //定义0 1 2 3的运动位置
// 左 上 右 下
//上行0 右行1 下行2 左行3
int left_dfs(node S, int come, int path)//左优先dfs
{
node cur=S; int e; for(int i=0; i<4; i++){
e=(i+come)%4;
int x=cur.x+d[e][0];
int y=cur.y+d[e][1]; if(!ok(x,y)) continue;
if(x==E.x && y==E.y){
//printf("dao da le -----------\n\n");
return path+1;
}
if(g[x][y]=='#') continue; if(x==cur.x && y!=cur.y)
{ if(y>cur.y) come=1; else come=3; }
else if(x!=cur.x && y==cur.y)
{ if(x>cur.x)come=2; else come=0; } node cc; cc.x=x; cc.y=y;
return left_dfs(cc, come, path+1);
}
} int d2[4][2]={ {0,1},{-1,0},{0,-1},{1,0} }; //定义0 1 2 3的运动位置
// 右 上 左 下
int right_dfs(node S, int come, int path)
{
node cur=S; int e;
for(int i=0; i<4; i++){
e=(i+3+come)%4;
int x=cur.x+d2[e][0];
int y=cur.y+d2[e][1];
//printf("x=%d y=%d\n",x+1, y+1); if(!ok(x,y) ) continue;
if(x==E.x && y==E.y){
//printf("*****");
return path+1;
}
if(g[x][y]=='#') continue; if(x==cur.x && y!=cur.y)
{if(y>cur.y) come=0; else come=2; }
else if(x!=cur.x && y==cur.y)
{if(x>cur.x) come=3; else come=1; } node cc; cc.x=x; cc.y=y;
//printf("come=%d x=%d y=%d\n",come, x+1, y+1);
return right_dfs(cc, come, path+1);
}
} int dir[4][2]={
{-1,0}, {1,0}, {0,-1}, {0,1} //定义 上下左右
};
int bfs_sp()
{
queue<node>q;
int path[50][50]; memset(path, 0, sizeof(path));
bool vis[50][50]; memset(vis, false, sizeof(vis)); //标记节点是否走过
q.push(S);//将起点入队列
vis[S.x][S.y]=true;//标记访问
path[S.x][S.y]++;
node cur;
while(!q.empty())
{
cur=q.front(); q.pop();//取出队首元素
//printf("%d--%d ", cur.x, cur.y);
for(int i=0; i<4; i++){
int x=cur.x+dir[i][0];
int y=cur.y+dir[i][1];
if(ok(x, y) && (g[x][y]=='.'||g[x][y]=='E') && vis[x][y]==false )
{ node cc; cc.x=x; cc.y=y;
q.push(cc); path[x][y]=path[cur.x][cur.y]+1;
vis[x][y]=true;
if(g[x][y]=='E')
{ return path[x][y];}
}
}
}
} int main()
{
int tg; scanf("%d", &tg);
int i, j;
//输出左优先搜索+右优先搜索+最短路径
int ans1, ans2, ans3; while(tg--){
scanf("%d %d", &m, &n); //n行 m列
for(i=0; i<n; i++){
scanf("%s", g[i]);
for(j=0; j<m; j++){
if(g[i][j]=='S'){ S.x=i; S.y=j; } //找到起点
else if(g[i][j]=='E') { E.x=i; E.y=j; } //找到终点
}
} //建图完毕
//printf("s=%d %d\n\n", S.x, S.y ); //printf("s=%d %d\n\n", E.x, E.y );
//一开始S可以走的方向
int come; //标记运动方向
for(i=0; i<4; i++){
int x=S.x+d[i][0];
int y=S.y+d[i][1];
if(ok(x,y)&& g[x][y]!='#'){
if(i==0) come=3; //左
else if(i==1) come=0;//上
else if(i==2) come=1;//右
else come=2;//下
}
}
//printf("come=%d\n", come ); ans1=left_dfs(S, come, 1); if(come==0) come=1;
else if(come==1) come=0;
else if(come==2) come=3;
else if(come==3) come=2;
//printf("come=%d\n", come ); ans2=right_dfs(S, come, 1); ans3=bfs_sp(); printf("%d %d %d\n", ans1, ans2, ans3); }
return 0;
}
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
点击打开链接 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(DFS+BFS)
做了1天,总是各种错误,很无语 最后还是参考大神的方法 题目:http://poj.org/problem?id=3083 题意:从s到e找分别按照左侧优先和右侧优先的最短路径,和实际的最短路径 DF ...
- 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 + 模拟)
题目链接:http://poj.org/problem?id=3083 题意: 这里有一个w * h的迷宫,给你入口和出口,让你分别求以下三种情况时,到达出口的步数(总步数包括入口和出口): 第一种: ...
- poj 3083 Children of the Candy Corn (广搜,模拟,简单)
题目 靠墙走用 模拟,我写的是靠左走,因为靠右走相当于 靠左走从终点走到起点. 最短路径 用bfs. #define _CRT_SECURE_NO_WARNINGS #include<stdio ...
- POJ 3083 Children of the Candy Corn 解题报告
最短用BFS即可.关于左手走和右手走也很容易理解,走的顺序是左上右下. 值得注意的是,从起点到终点的右手走法和从终点到起点的左手走法步数是一样. 所以写一个左手走法就好了.贴代码,0MS #inclu ...
- POJ 3083 Children of the Candy Corn (DFS + BFS)
POJ-3083 题意: 给一个h*w的地图. '#'表示墙: '.'表示空地: 'S'表示起点: 'E'表示终点: 1)在地图中仅有一个'S'和一个'E',他们为位于地图的边墙,不在墙角: 2)地图 ...
随机推荐
- mysql时间操作(时间差和时间戳和时间字符串的互转)
mysql时间操作(时间差和时间戳和时间字符串的互转) 两个时间差: MySQL datediff(date1,date2):两个日期相减 date1 - date2,返回天数. select dat ...
- Android Studio 2.0 正式版公布啦 (首次中文翻译)
Android Studio 2.0 公布了,添加了一些新特性: 1. 更加完好的 Instant Run 2. 更快的 Android Emulator 3.GPU Debugger Preview ...
- ApplicationContextRunner如何简化自动配置测试
1. 概览 众所周知,自动配置是Spring Boot的关键功能之一, 但测试自动配置可能会很棘手. 在以下部分中,我们将展示ApplicationContextRunner如何简化自动配置测试. 2 ...
- Cannot open channel to 3 at election address :3888 java.net.ConnectException: Connection refused (Connection refused)
关于Linux中搭建分布式时可能遇到的问题 这个问题来自于今天安装zookeeper时踩的一个大坑,害的我花了一天时间.在搭建zookeeper的分布式时,往往要进行这样的配置: server.1=h ...
- Maven学习----dependencies与dependencyManagement的区别(转)
转自:http://blog.csdn.net/liutengteng130/article/details/46991829 1.DepencyManagement应用场景 当我们的项目模块很多的时 ...
- eclipse配置python插件
eclipse配置python主要可以分为以下几个步骤完成: 1. 安装python,python主要有两个版本,python2和python3,这里安装的是python2.7.主要考虑python使 ...
- 通过Lock对象以及Condition对象实现多线程同步
通过Lock对象以及Condition对象实现多线程同步: 在之前的学习中,无论是通过synchronized建立同步代码块,还是通过synchronized建立同步函数,都是把对象看成一把锁来实现同 ...
- 编辑器模式下如何实例化Prefab
当我们在EditMode下需要用脚本批量添加prefab时,可以用 PrefabUtility.InstantiatePrefab(prefab) as GameObject; 注意:如果用GameO ...
- 【iOS开发-80】Quartz2D绘图简介:直线/圆形/椭圆/方形以及上下文栈管理CGContextSaveGState/CGContextRestoreGState
本文转载至 http://blog.csdn.net/weisubao/article/details/41282457 - (void)drawRect:(CGRect)rect { //获得当前上 ...
- EasyNVR无插件H5/HLS/m3u8直播解决方案中Windows系统服务启动错误问题的修复:EasyNVR_Service 服务因 函数不正确。 服务特定错误而停止。
最近在做某地市移动公司景观直播的项目时,遇到一个问题,当我们部署EasyNVR为系统服务后,居然出现了无法启动服务的现象,表面上看,提示是系统服务启动失败,实际通过查看windows 系统日志: 查找 ...