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)再输出右转优先时,从S到E的步数
3)最后输出S到E的最短步数
解题思路:
前两问DFS,转向只要控制一下旋转方向就可以
首先设置前进方向对应的数字
向上——N——0
向右——E——1
向下——S——2
向左——W——3
比如说右转优先,即为向右,向前,向左,向后,即逆时针方向for(int i=1;i>=-2;i--)
左转优先,即为向左,向前,向右,向后,即顺时针方向for(int i=-1;i<=3;i++)
第三问最短路,BFS
普通递归(TLE)
#include<iostream>
#include<cstring>
#include<queue>
#include<cstdio>
using namespace std;
int r,c;///行r,列c
int r0,c0,r3,c3;///r0,c0用来标记入口,r3,c3用来标记出口
const char *dirs = "NESW";
const int maxn = ;
char square[maxn][maxn];
const int dr[] = {-,,,};
const int dc[] = { ,,,-};
struct node{
int row,col,deep;
int dir;///0123对应NESW
node(int row=,int col=,int dir=,int deep=):row(row),col(col),dir(dir),deep(deep){}
};
bool inside(int xx,int yy)
{
return xx>= && xx<=r && yy>= && yy<=c;
} bool flag1;
int step1;
void dfs1(node *way,int x,int y,int d,int step)
{///左转优先
way[step].row = x;
way[step].col = y;
way[step].dir = d;
if(x==r3 && y==c3)///走到出口
{
step1 = step;
flag1 = true;
return;
}
for(int i=-;i<=;i++)
{
int tempDir = (way[step].dir + + i)%;///进行旋转
int xx = way[step].row + dr[tempDir];
int yy = way[step].col + dc[tempDir];
if(inside(xx,yy) && square[xx][yy]!='#')
{///没有出界,可以行走
dfs1(way,xx,yy,tempDir,step+);
}
if(flag1)
return;
}
return;
} int step2;bool flag2;
void dfs2(node *way,int x,int y,int d,int step)
{///右转优先
way[step].row = x;
way[step].col = y;
way[step].dir = d;
if(x==r3 && y==c3)///走到出口
{
step2 = step;
flag2 = true;
return;
}
for(int i=;i>=-;i--)
{
int tempDir = (way[step].dir + + i)%;///进行旋转
int xx = way[step].row + dr[tempDir];
int yy = way[step].col + dc[tempDir];
if(inside(xx,yy) && square[xx][yy]!='#')
{///没有出界,可以行走
dfs2(way,xx,yy,tempDir,step+);
}
if(flag2)
return;
}
return;
}
node d[maxn][maxn][]; void bfs(int x,int y,int d)
{
queue<node> q;
node u(x,y,d,);///入口结点
q.push(u);
while(!q.empty())
{
node u = q.front();q.pop();
if(u.row == r3 && u.col == c3)
{
cout<<u.deep<<endl;
return;
}
for(int i=;i<=;i++)
{
int tempDir = (u.dir +i)%;///进行旋转
int xx = u.row + dr[tempDir];
int yy = u.col + dc[tempDir];
if(inside(xx,yy) && square[xx][yy]!='#')
{///没有出界,可以行走
node v(xx,yy,tempDir,u.deep+);
q.push(v);
}
}
}
} int startDir()
{///计算从入口进入之后的方向
if(r0 == ) return ;
else if(r0 == r) return ;
else if(c0 == ) return ;
else return ;
}
int main()
{
int n;
cin>>n;
while(n--)
{
cin>>c>>r;///输入为先输入列数,在输入行数
char temp;
for(int i=;i<=r;i++)
for(int j=;j<=c;j++)
{
temp = getchar();
while(temp == '\n') temp = getchar();
square[i][j] = temp;
if(temp == 'S'){r0 = i;c0 = j;}
if(temp == 'E'){r3 = i;c3 = j;}
}
node *way = new node[maxn*maxn];
///求解左转优先
flag1 = false;step1 = ;
int startdir = startDir();
dfs1(way,r0,c0,startdir,);
if(flag1) cout<<step1<<" ";
///求解右转优先
flag2 = false;step2 = ;
dfs2(way,r0,c0,startdir,);
if(flag2) cout<<step2<<" ";
///求解最短路径
bfs(r0,c0,startdir);
}
return ;
}
最终成功代码
Time Limit Exceeded原因:POJ对STL兼容性不高,使用queue超时
其次,DFS使用尾递归形式,遇到可行解,直接向下一层搜索
最后,BFS不能走重复路线,否则会陷入死循环,Runtime Error
仅此告诫自己
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
int r,c;///行r,列c
int r0,c0,r3,c3;///r0,c0用来标记入口,r3,c3用来标记出口 const int maxn = ;
char square[maxn][maxn];
const int dr[] = {-,,,};
const int dc[] = { ,,,-}; struct node{
int row,col,deep;
int dir;///0123对应NESW
node(int row=,int col=,int dir=,int deep=):row(row),col(col),dir(dir),deep(deep){}
};
bool inside(int xx,int yy)
{
return xx>= && xx<=r && yy>= && yy<=c;
}
int dfs12(int x,int y,int d)
{///左转优先
if(square[x][y] == 'E')
return ;
int tempDir,xx,yy;
for(int i=-;i<=;i++)
{
tempDir = (d + + i)%;///进行旋转
xx = x + dr[tempDir];
yy = y + dc[tempDir];
if(inside(xx,yy) && square[xx][yy]!='#')
{///没有出界,可以行走
break;
}
}
return dfs12(xx,yy,tempDir)+;
}
int dfs22(int x,int y,int d)
{///you转优先
if(square[x][y] == 'E')
return ;
int tempDir,xx,yy; for(int i=;i>=-;i--)
{
tempDir = (d + + i)%;///进行旋转
xx = x + dr[tempDir];
yy = y + dc[tempDir];
if(inside(xx,yy) && square[xx][yy]!='#')
{///没有出界,可以行走
break;
}
}
return dfs22(xx,yy,tempDir)+;
} node queue[];
bool has_walk[maxn][maxn];
void bfs(int x,int y,int d)
{
int head=,tail=;
node u(x,y,d,);
has_walk[x][y] = false;
queue[head] = u;///入口结点
while(head<tail)
{
node u = queue[head++];
if(u.row == r3 && u.col == c3)
{
cout<<u.deep<<endl;
return;
}
for(int i=;i<=;i++)
{
int tempDir = (u.dir +i)%;///进行旋转
int xx = u.row + dr[tempDir];
int yy = u.col + dc[tempDir];
if(inside(xx,yy) && square[xx][yy]!='#' && has_walk[xx][yy])
{///没有出界,可以行走,没有走过true
node v(xx,yy,tempDir,u.deep+);
has_walk[xx][yy] = false;
queue[tail] = v;
tail++;
}
}
}
} int startDir()
{///计算从入口进入之后的方向
if(r0 == ) return ;
else if(r0 == r) return ;
else if(c0 == ) return ;
else return ;
}
int main()
{
int n;
while(cin>>n)
while(n--)
{
cin>>c>>r;///输入为先输入列数,在输入行数
memset(has_walk,true,sizeof(has_walk));///true表示当前格子没有走过,可以走
char temp;
for(int i=;i<=r;i++)
for(int j=;j<=c;j++)
{
temp = getchar();
while(temp == '\n') temp = getchar();
square[i][j] = temp;
if(temp == 'S'){r0 = i;c0 = j;}
if(temp == 'E'){r3 = i;c3 = j;}
}
///求解左转优先
int startdir = startDir();
cout<<dfs12(r0,c0,startdir)<<" ";
///求解右转优先
cout<<dfs22(r0,c0,startdir)<<" ";
///求解最短路径
bfs(r0,c0,startdir);
}
return ;
}
最后给大家提供点测试样例:
########
#......#
#.####.#
#.####.#
#.####.#
#.####.#
#...#..#
#S#E#### #########
#.#.#.#.#
S.......E
#.#.#.#.#
######### ########
#.#.#..#
S......E
#.#.#..#
######## ##
SE
## ######E#
#......#
#.####.#
#.####.#
#.####.#
#.####.#
#...#..#
#S######
结果:
POJ 3083 -- Children of the Candy Corn(DFS+BFS)TLE的更多相关文章
- 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(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 ######## #......# #.####.# #.####.# #.####.# ...
随机推荐
- struts-2.5.14.1 中web.xml的基本配置
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http:// ...
- 数据库 master拒绝了 create database 权限
1.通过windows身份验证方式登录 2.为登录名赋予服务器角色权限,其中dbcreator权限表示允许新增和修改权限,sysadmin权限是管理员权限,包含dbcreator范围,若不追求权限精准 ...
- Nginx如何配置禁止访问某个目录
location ~* \.(txt|doc)${ root /data/www/wwwroot/test; deny all; }
- Golang之初探
什么是Go语言 Go语言介绍 产生背景: 超级复杂的C++11特性的吹捧报告的鄙视以及最终的目标是具备动态语言的开发速度的同时并要有C/C++编译语言的性能与安全性以及设计网络和多核时代的C语言 Go ...
- 基于docker搭建elasticsearch集群
es集群的搭建 - 基于单机搭建elasticsearch集群见官网 https://www.elastic.co/guide/en/elasticsearch/reference/current/d ...
- Delphi 抽象方法
- 树的总结(遍历,BST,AVL原型,堆,练习题)
目录 树 一.抽象数据类型 二.二叉树的性质 三.二叉树的遍历 三.活用树的遍历 四.BST树 五.AVL树 六.BST树和AVL树练习 七.堆 树 @ 一.抽象数据类型 1.顺序存储 使用数组存储 ...
- ICPC2019徐州站游记
day0 出师不利 差两分钟没赶上高铁回去我一定每天都到下边玩跑步 改签成卧铺,原来3点发7点到现在11点发9点到 本来计划火车上3个人练习一场,晚上宾馆还可以cf 结果全泡汤了,在火车站浪了一晚上 ...
- sqlite3.OperationalError: no such table: account_user
你可能是在项目中安装了多个app, 首先删除相关app的migration文件中的子文件 执行建表的时候使用: python manage.py makemigrations appname pyth ...
- python打开excel跳转对应分页
需求: 在python脚本层,直接打开excel,跳转特定分页,方便策划编辑 尝试了几种不同的方法,最终选择了方法4.4种方法都可以实现打开外部文件,3.4可以实现跳转特定分页.3和4的区别是,3通过 ...