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)地图 ...
随机推荐
- Android+git+hudson+gradle持续集成
linux 主机 android sdk安装忽略 jdk安装忽略 hudson安装忽略 gradle安装 1:下载相应的gradle(这里是gradle-2.10-all.zip) 2 : 解 ...
- hdu 4601 Letter Tree
不easy啊.. 一个小错误让我wa死了.找了一个晚上,怎么都找不到 最后是对拍代码找到的错误.发现当步数比較小的时候答案就是对的,比較大的时候就不正确了 想到一定是什么地方越界了.. . power ...
- Google Code Jam 2014 Round 1 A:Problem C. Proper Shuffle
Problem A permutation of size N is a sequence of N numbers, each between 0 and N-1, where each numbe ...
- mysql导出数据库提示警告在GTID模式下面
[root@db02 tmp]# mysqldump -S /tmp/mysql.sock -A -R --triggers --master-data=2 --single-transaction ...
- 基于树莓派3B+Python3.5的OpenCV3.4的配置教程
https://www.cnblogs.com/Pyrokine/p/8921285.html
- 谨慎使用ArrayList中的subList方法
转自:https://www.toutiao.com/a6705958780460335619/?tt_from=weixin&utm_campaign=client_share&wx ...
- PHP里的socket_recv方法解释
以前一直经为PHP里没有低级的socket帧接收函数,看来是没看仔细,不过那些说明也太少了,(更令人气的里在英文版说明里的例子下有一句话:这个程序不能运行,因为没用listen函数,但在中文版里却没了 ...
- PHP 和 AJAX MySQL 数据库实例
HTML 表单 <!DOCTYPE html> <html lang="en"> <head> <meta charset="U ...
- vscode 全局安装和配置 stylelint 像 webstorm 等 ide 一样来检查项目
商店里安装完插件以后全局安装或者在项目中(记得加--save-dev)安装配置: npm install stylelint-config-recommended -g 然后在vscode setti ...
- iostat命令简单使用
1.iostat使用范围 iostat命令可以生成3种类型的报告: (1)CPU使用情况的报告 (2)设备使用情况的报告 (3)网络文件系统(NFS)使用情况的报告 2.每种报告的格式说明 关于CPU ...