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 ######## #......# #.####.# #.####.# #.####.# ...
随机推荐
- asp.net mvc4 学习1
1 简介:微软在很早就看到了基于windows系统的web开发平台的需求,这时便开始提出自己的解决方案即微软的第一个基于web开发的平台ASP.再后来随着需求和性能的要求再2002年推出第二个解决方案 ...
- impala 建表时报错,不支持中文
1.错误信息 (1366, "Incorrect string value: '\\xE6\\x8E\\x88\\xE6\\x9D\\x83...' for column 'search' ...
- java lambda 所有列求和
今天做东西的时候遇到一个需求,求list集合所有列的求和.折腾半天也没有搞出来,网上大部分都是单列求和就像下面这样的,其他都差多,什么 min,max avg count 只得到了number这个属性 ...
- shell 脚本检测端口状态
方法一: # cat check_port.sh #!/bin/bash cat ip.txt|while read line do /usr/bin/nc -w 1 -z $line > /d ...
- kotlin变量基础
kotlin变量与输出 这里看一下在Kotlin中的变量是如何定义的? 编译运行: 对比下我们常用的JAVA语言,定义上的区别: 1.Kotlin变量是统一以var开头,而不区分具体类型,而Java在 ...
- Appium Desired Capabilities-General Capabilities
Desired Capabilities are keys and values encoded in a JSON object, sent by Appium clients to the ser ...
- windows10下成功安装docker splash及遇到问题的解决方案
转载出处:http://www.cnblogs.com/321lxl/p/9536616.html
- python+Appium自动化:屏幕截图
屏幕截图 主要是为了程序出现错误时,开发除了可以分析日志之外,还可以进行截图更好地去定位问题. 截图一般有两种方法: 第一种save_screenshot(self,filename) driver. ...
- 自己写的一个用于往文件中插入字符串及空格的bat
@echo off echo pleas input the filename:set /p file= :while rem set j=0 非得写这儿 写下面:check 上面不行 希望大神指点s ...
- vscode+TCC快捷编译c语言
1.安装vscode. 2.安装插件tcc.小巧,快速,符合ansi c, 法国大神级别程序员开发的编译器. 3.编写C代码,F10运行,即可得结果,对于验证一些语法很快捷,很方便.只需三步!!