本题传送门

本题知识点:深度优先搜索 + 宽度优先搜索

本题题意是求三个路径长度,第一个是一直往左手边走的距离,第二个是一直往右手边走的距离,第三个是最短距离。

第三个很好办,就是一个简单的bfs的模板,问题出在第一二个。

但第一二个只是方向的丝丝不同,所以会其中一个也就解决了。

读懂题意后很简单,问题是怎么简单高效地实现。

推荐这篇博客

一些感慨:该题做了我差不多4个小时吧,前前后后de了bug又发现有新bug,包括调试的代码,旧代码起码达到了300多行。已经有东西南北方向的想法了,可就是差那么一点点。还是刷题不够吧。总之这4个小时里感到兴奋的心情比难过迷茫的心情要多很多。还是要以学习算法为前提去A题才是快乐A题呀!

// 3083
#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
using namespace std; char maze[50][50];
bool take[50][50];
int len[50][50];
int T, W, H, now;
int bh, bw, eh, ew;
int left_len, right_len, min_len;
bool ok;
struct node{
int h, w;
};
queue<node> que;
int rw[] = { -1, 0, 1, 0 };
int rh[] = { 0, 1, 0, -1 }; bool check(int h, int w){
if(0 <= h && h < H && 0 <= w && w < W && maze[h][w] != '#' && !take[h][w]) return true;
return false;
} void build(int W, int H){
memset(take, false, sizeof(take));
left_len = right_len = min_len = 0;
for(int i = 0; i < H; i++){
scanf("%s", maze[i]);
for(int j = 0; j < W; j++){
if(maze[i][j] == 'S'){
bh = i; bw = j;
// maze[i][j] = '#';
}
if(maze[i][j] == 'E'){
eh = i; ew = j;
}
}
}
if(bh == 0) now = 2;
else if(bh == H - 1) now = 0;
else if(bw == 0) now = 3;
else if(bw == W - 1) now = 1;
} void l_dfs(int h, int w, int go){
left_len++;
// take[h][w] = true;
if(h == eh && w == ew){
ok = true;
return ;
} switch(go)
{
case 0:
{
if(check(h, w - 1)) l_dfs(h, w - 1, 1);
else if(check(h - 1, w)) l_dfs(h - 1, w, 0);
else if(check(h, w + 1)) l_dfs(h, w + 1, 3);
else if(check(h + 1, w)) l_dfs(h + 1, w, 2);
break;
}
case 1:
{
if(check(h + 1, w)) l_dfs(h + 1, w, 2);
else if(check(h, w - 1)) l_dfs(h, w - 1, 1);
else if(check(h - 1, w)) l_dfs(h - 1, w, 0);
else if(check(h, w + 1)) l_dfs(h, w + 1, 3);
break;
}
case 2:
{
if(check(h, w + 1)) l_dfs(h, w + 1, 3);
else if(check(h + 1, w)) l_dfs(h + 1, w, 2);
else if(check(h, w - 1)) l_dfs(h, w - 1, 1);
else if(check(h - 1, w)) l_dfs(h - 1, w, 0);
break;
}
case 3:
{
if(check(h - 1, w)) l_dfs(h - 1, w, 0);
else if(check(h, w + 1)) l_dfs(h, w + 1, 3);
else if(check(h + 1, w)) l_dfs(h + 1, w, 2);
else if(check(h, w - 1)) l_dfs(h, w - 1, 1);
break;
}
} } void r_dfs(int h, int w, int go){
right_len++;
if(h == eh && w == ew){
ok = true;
return ;
} switch(go)
{
case 0:
{
if(check(h, w + 1)) r_dfs(h, w + 1, 3);
else if(check(h - 1, w)) r_dfs(h - 1, w, 0);
else if(check(h, w - 1)) r_dfs(h, w - 1, 1);
else if(check(h + 1, w)) r_dfs(h + 1, w, 2);
break;
}
case 1:
{
if(check(h - 1, w)) r_dfs(h - 1, w, 0);
else if(check(h, w - 1)) r_dfs(h, w - 1, 1);
else if(check(h + 1, w)) r_dfs(h + 1, w, 2);
else if(check(h, w + 1)) r_dfs(h, w + 1, 3);
break;
}
case 2:
{
if(check(h, w - 1)) r_dfs(h, w - 1, 1);
else if(check(h + 1, w)) r_dfs(h + 1, w, 2);
else if(check(h, w + 1)) r_dfs(h, w + 1, 3);
else if(check(h - 1, w)) r_dfs(h - 1, w, 0);
break;
}
case 3:
{
if(check(h + 1, w)) r_dfs(h + 1, w, 2);
else if(check(h, w + 1)) r_dfs(h, w + 1, 3);
else if(check(h - 1, w)) r_dfs(h - 1, w, 0);
else if(check(h, w - 1)) r_dfs(h, w - 1, 1);
break;
}
}
} void bfs(){
len[bh][bw] = 1;
while(!que.empty()){
node now = que.front(), next; que.pop();
int d = len[now.h][now.w];
// printf("d:%d\n", d);
if(now.h == eh && now.w == ew) break;
for(int i = 0; i < 4; i++){
next.h = now.h + rh[i]; next.w = now.w + rw[i];
if(check(next.h, next.w)) {
take[next.h][next.w] = true;
len[next.h][next.w] = d + 1;
que.push(next);
}
}
}
} int main()
{
scanf("%d", &T);
while(T--){
scanf("%d %d", &W, &H);
build(W, H); // right
ok = false;
memset(take, false, sizeof(take));
l_dfs(bh, bw, now);
// cout << endl; ok = false;
memset(take, false, sizeof(take));
r_dfs(bh, bw, now); while(!que.empty()) que.pop();
memset(take, false, sizeof(take));
memset(len, 0, sizeof(len));
node a; a.h = bh; a.w = bw;
que.push(a);
bfs(); printf("%d %d %d\n", left_len, right_len, len[eh][ew]);
} return 0;
} //6
//8 8
//########
//#......#
//#.####.#
//#.####.#
//#.####.#
//#.####.#
//#...#..#
//#S#E####
//9 5
//#########
//#.#.#.#.#
//S.......E
//#.#.#.#.#
//#########
//8 8
//########
//#......#
//#.####.#
//#.####.#
//#.####.#
//#.####.#
//#..#...#
//####E#S#
//9 5
//#########
//#.#.#.#.#
//E.......S
//#.#.#.#.#
//#########
//9 9
//####E####
//#.......#
//#..#.#..#
//#..###..#
//#.......#
//#..#.#..#
//#..#.#..#
//#..#.#..#
//####S####
//9 9
//####S####
//#.......#
//#..#.#..#
//#.#####.#
//#.......#
//#..#.#..#
//#..#.#..#
//#..#.#..#
//####E####

【POJ3083】Children of the Candy Corn的更多相关文章

  1. POJ3083——Children of the Candy Corn(DFS+BFS)

    Children of the Candy Corn DescriptionThe cornfield maze is a popular Halloween treat. Visitors are ...

  2. poj3083 Children of the Candy Corn BFS&&DFS

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

  3. poj 3083 Children of the Candy Corn

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

  4. Children of the Candy Corn 分类: POJ 2015-07-14 08:19 7人阅读 评论(0) 收藏

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

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

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

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

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

  7. K - Children of the Candy Corn(待续)

    K - Children of the Candy Corn Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d ...

  8. POJ 3083 -- Children of the Candy Corn(DFS+BFS)TLE

    POJ 3083 -- Children of the Candy Corn(DFS+BFS) 题意: 给定一个迷宫,S是起点,E是终点,#是墙不可走,.可以走 1)先输出左转优先时,从S到E的步数 ...

  9. POJ 3083:Children of the Candy Corn

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

随机推荐

  1. Hadoop问题解决:WARN util.NativeCodeLoader: Unable to load native-hadoop library for your platform... using builtin-java classes where applicable

    在配置好hadoop的环境之后,命令启动./start-all.sh发现经常出现这样的一个警告: WARN util.NativeCodeLoader: Unable to load native-h ...

  2. Springboot html vue.js 前后分离 跨域 Activiti6 工作流 集成代码生成器 shiro 权限

    官网:www.fhadmin.org 特别注意: Springboot 工作流  前后分离 + 跨域 版本 (权限控制到菜单和按钮) 后台框架:springboot2.1.2+ activiti6.0 ...

  3. Git下载安装及设置详细教程

    Git下载安装及设置详细教程 一.安装前准备   1. 廖雪峰老师Git教程 :推荐Git入门教程.  2. 按照自己的系统版本下载Git软件,我的操作系统:Windows7 64位,安装版本为Git ...

  4. float与position间的区别

    float与position间的区别:    个人理解为:脱离文档流不一定脱离文本流:但脱离文本流,则也脱离文档流.[如有更好的理解还望评论区一起探讨,共同学习进步]一.float 浮动(脱离文档流, ...

  5. 财政FINAUNCE英文FINAUNCE金融

    中文名金融 外文名Finance.Finaunce 概括为货币的发行与回笼 从事金融机构有银行.信托投资公司 目录 1 基本定义 2 关于概念 ? 概念新解 ? 概念现状 ? 熊德平新解 3 金融特征 ...

  6. day 03 作业 预科

    目录 作业 1.简述变量的组成 2.简述变量名的命名规范 3.简述注释的作用 4.使用turtle库构造一幅图,贴在markdown文档中 作业 1.简述变量的组成 变量由变量名.赋值符号.变量值所组 ...

  7. MySQL Index--BNL/ICP/MRR/BKA

    MySQL关联查询算法: BNL(Block Nested-Loop) ICP(Index Condition Pushdown) MRR(Multi-Range Read) BKA(Batched ...

  8. Deployment

    Deployment RC是kubernetes中的一个核心概念,Deployment 是新一代的RC,除了拥有RC的功能外,还具备一下特性: 支持事件和状态查看:可以查看Deployment升级的状 ...

  9. Ceph添加、删除osd及故障硬盘更换

    添加或删除osd均在ceph部署节点的cent用户下的ceph目录进行. 1. 添加osd 当前ceph集群中有如下osd,现在准备新添加osd: (1)选择一个osd节点,添加好新的硬盘: (2)显 ...

  10. 洛谷 P1443 马的遍历题解

    题目链接:https://www.luogu.org/problem/P1443 题目描述 有一个n*m的棋盘(1<n,m<=400),在某个点上有一个马,要求你计算出马到达棋盘上任意一个 ...