题目地址: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搜索】【复习搜索题目一定要看这道题目】的更多相关文章

  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

    点击打开链接 Children of the Candy Corn Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8288 ...

  3. POJ 3083 Children of the Candy Corn bfs和dfs

      Children of the Candy Corn Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8102   Acc ...

  4. poj 3083 Children of the Candy Corn(DFS+BFS)

    做了1天,总是各种错误,很无语 最后还是参考大神的方法 题目:http://poj.org/problem?id=3083 题意:从s到e找分别按照左侧优先和右侧优先的最短路径,和实际的最短路径 DF ...

  5. 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 ...

  6. POJ 3083 Children of the Candy Corn (DFS + BFS + 模拟)

    题目链接:http://poj.org/problem?id=3083 题意: 这里有一个w * h的迷宫,给你入口和出口,让你分别求以下三种情况时,到达出口的步数(总步数包括入口和出口): 第一种: ...

  7. poj 3083 Children of the Candy Corn (广搜,模拟,简单)

    题目 靠墙走用 模拟,我写的是靠左走,因为靠右走相当于 靠左走从终点走到起点. 最短路径 用bfs. #define _CRT_SECURE_NO_WARNINGS #include<stdio ...

  8. POJ 3083 Children of the Candy Corn 解题报告

    最短用BFS即可.关于左手走和右手走也很容易理解,走的顺序是左上右下. 值得注意的是,从起点到终点的右手走法和从终点到起点的左手走法步数是一样. 所以写一个左手走法就好了.贴代码,0MS #inclu ...

  9. POJ 3083 Children of the Candy Corn (DFS + BFS)

    POJ-3083 题意: 给一个h*w的地图. '#'表示墙: '.'表示空地: 'S'表示起点: 'E'表示终点: 1)在地图中仅有一个'S'和一个'E',他们为位于地图的边墙,不在墙角: 2)地图 ...

随机推荐

  1. ASP.NET CORE RAZOR :向 Razor 页面应用添加模型

    本文来自:https://docs.microsoft.com/zh-cn/aspnet/core/tutorials/razor-pages/model 在本部分中将添加用于管理数据库中的电影的类. ...

  2. Linux服务器重启后启动Oracle服务

    目录 1. 启动Oracle服务 2. 启动Oracle监听服务 © 版权声明:本文为博主原创文章,转载请注明出处 1. 启动Oracle服务 重启Linux服务器后,Oracle服务还需要手动启动. ...

  3. org.springframework.beans.factory.UnsatisfiedDependencyException

    © 版权声明:本文为博主原创文章,转载请注明出处 1.问题描述: 搭建SSH框架,启动时报错如下: 严重: Context initialization failed org.springframew ...

  4. Spring事务管理之编程式事务管理

    © 版权声明:本文为博主原创文章,转载请注明出处 案例:利用Spring的编程式事务管理模拟转账过程 数据库准备 -- 创建表 CREATE TABLE `account`( `id` INT NOT ...

  5. 解决Windows平台通过cURL上传APP到蒲公英pgyer平台时无法使用中文升级描述的问题

    解决Windows平台通过cURL上传APP到蒲公英pgyer平台时无法使用中文升级描述的问题 官方上传命令 curl -F file=@"315.apk" -F uKey=XXX ...

  6. Win7 设置、访问共享文件夹

    一.设置共享文件夹 右键点击文件夹,打开“属性”窗口,选择“共享”选项卡 点击“共享”按钮,打开“文件共享”窗口,在下拉列表中选择账户,点“添加”,最后点“共享”按钮. 二.访问 \\192.168. ...

  7. Java设计模式透析之 —— 策略(Strategy)

    今天你的leader兴致冲冲地找到你,希望你能够帮他一个小忙.他如今急着要去开会.要帮什么忙呢?你非常好奇. 他对你说.当前你们项目的数据库中有一张用户信息表.里面存放了非常用户的数据.如今须要完毕一 ...

  8. 2013 年最好的 20 款免费 jQuery 插件

    2013 年最好的 20 款免费 jQuery 插件 oschina 发布于: 2014年01月11日 (8评) 分享到  新浪微博腾讯微博 收藏+99 互联网上面有很多 jQuery 插件,这里我们 ...

  9. Centos7 install RabbitMQ

    安装rabbitmq 需要环境上有erlang,没有安装的可以参照下面的内容进行安装: https://www.erlang-solutions.com/resources/download.html ...

  10. Python中使用 Selenium 实现网页截图实例

    Selenium 是一个可以让浏览器自动化地执行一系列任务的工具,常用于自动化测试.不过,也可以用来给网页截图.目前,它支持 Java.C#.Ruby 以及 Python 四种客户端语言.如果你使用 ...