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)地图 ...
随机推荐
- Python修改文件权限
os.chmod()方法 此方法通过数值模式更新路径或文件权限.该模式可采取下列值或按位或运算组合之一: stat.S_ISUID: Set user ID on execution. stat.S_ ...
- UVa 437 The Tower of Babylon(DP 最长条件子序列)
题意 给你n种长方体 每种都有无穷个 当一个长方体的长和宽都小于还有一个时 这个长方体能够放在还有一个上面 要求输出这样累积起来的最大高度 由于每一个长方体都有3种放法 比較不好控制 ...
- maven初始搭建一个基础项目(spring mvc+spring+jdbc mysql+jstl)
技术选型: 一.项目搭建: 1)创建maven项目 (我博客里面有介绍) 选择aptach的maven-archetype-webapp 填入groupIDhe artifactId等 确认项目名称 ...
- javascript判断浏览器核心
20 21 22 23 24 /** * 判断浏览器核心 * @return IE6.0/IE7.0/IE8.0/FireFox/Opera/other * @author ypz */ functi ...
- 4Sum_leetCode
Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = tar ...
- 【SQLServer2008】之Telnet以及1433端口设置
Telnet步骤: 一.首先进入Win7控制面板,可以从开始里找到或者在桌面上找到计算机,点击进入里面也可以找到控制面板,如下图: 二.进入控制面板后,我们再找到“程序和功能”并点击进入,如下图所示: ...
- 探究 Redis 4 的 stream 类型
redis 2 10 月初,Redis 搞了个大新闻.别紧张,是个好消息:Redis 引入了名为 stream 的新数据类型和对应的命令,大概会在年底正式发布到 4.x 版本中.像引入新数据类型这样的 ...
- python 基础 9.11 更改数据
#/usr/bin/python #-*- coding:utf-8 -*- #@Time :2017/11/24 4:45 #@Auther :liuzhenchuan #@File :更改 ...
- maven;tomcat配置
[说明]今天呀,上午刚刚打算写javaweb项目,服务器就出现了问题,就花了点时间搞定了:下午听老大说了任务的大致内容和意义,收获颇多:晚上去服务器上部署了maven,听说可以实现热部署 一:今天完成 ...
- 小程序JSON数组操作