Children of the Candy Corn
poj3083:http://poj.org/problem?id=3083
题意:给你一个迷宫,然后给你一个起点和终点,现在给你种规则,一种是先向左,无法向左则向前,无法向前则向右,否则则向后,另外一种就是求最短路程,然后一种就先向右,向前,向左,向后,分别求出这三种情况下所走的路程。
题解:求最短的路程只需BFS即可,先向左可以DFS,每次DFS记录来自的方向,对于不同的方向,采取不同的搜索顺序,即可。向右的同理。
#include<cstring>
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
char map1[][];
int counts[][];//BFS记录最短距离
int n,m;//长,宽
bool flag1,flag2;//深搜的找到的标记
struct Node {
int x;
int y;
int step;
}node[][];
int startx,starty;//起点
int endx,endy;//终点
int BFS(int x,int y){
int dir[][]={{,},{-,},{,},{,-}};//四个方向
for(int i=;i<=;i++)
for(int j=;j<=;j++)//初始化
counts[i][j]=;
queue<Node>Q;
Node tt;
tt.x=x;tt.y=y;tt.step=;
Q.push(tt);
counts[x][y]=;//注意这里
while(!Q.empty()){
Node temp=Q.front();
Q.pop();
int xx=temp.x;
int yy=temp.y;
int step=temp.step;
for(int i=;i<;i++){//四个方向进行收索
if(xx+dir[i][]<=n&&xx+dir[i][]>=&&yy+dir[i][]<=m&&yy+dir[i][]>=){
if(map1[xx+dir[i][]][yy+dir[i][]]!='#'&&step+<counts[xx+dir[i][]][yy+dir[i][]]){
counts[xx+dir[i][]][yy+dir[i][]]=step+;
Node ttt;
ttt.x=xx+dir[i][];
ttt.y=yy+dir[i][];
ttt.step=step+;
Q.push(ttt);
}
} }
}
return counts[endx][endy];
}
int DFSL(int x,int y,int dir,int num){//dir表示方向,num表示当前的深度
if(x==endx&&y==endy){
flag1=true;
return num;
}
if(!flag1){
if(dir==){//这里定义向上为1,左边为2,下边3,右边4
if(!flag1&&y->=&&map1[x][y-]!='#')//如果来自上边,则这次首先考录左边,就是2
return DFSL(x,y-,,num+);
if(!flag1&&x->=&&map1[x-][y]!='#')//一下同理
return DFSL(x-,y,,num+);
if(!flag1&&y+<=m&&map1[x][y+]!='#')
return DFSL(x,y+,,num+);
if(!flag1&&x+<=n&&map1[x+][y]!='#')
return DFSL(x+,y,,num+);
}
if(dir==){ if(!flag1&&x+<=n&&map1[x+][y]!='#')
return DFSL(x+,y,,num+);
if(!flag1&&y->=&&map1[x][y-]!='#')
return DFSL(x,y-,,num+);
if(!flag1&&x->=&&map1[x-][y]!='#')
return DFSL(x-,y,,num+);
if(!flag1&&y+<=m&&map1[x][y+]!='#')
return DFSL(x,y+,,num+);
}
if(dir==){ if(!flag1&&y+<=m&&map1[x][y+]!='#')
return DFSL(x,y+,,num+);
if(!flag1&&x+<=n&&map1[x+][y]!='#')
return DFSL(x+,y,,num+);
if(!flag1&&y->=&&map1[x][y-]!='#')
return DFSL(x,y-,,num+);
if(!flag1&&x->=&&map1[x-][y]!='#')
return DFSL(x-,y,,num+);
}
if(dir==){
if(!flag1&&x->=&&map1[x-][y]!='#')
return DFSL(x-,y,,num+);
if(!flag1&&y+<=m&&map1[x][y+]!='#')
return DFSL(x,y+,,num+);
if(!flag1&&x+<=n&&map1[x+][y]!='#')
return DFSL(x+,y,,num+);
if(!flag1&&y->=&&map1[x][y-]!='#')
return DFSL(x,y-,,num+);
}
}
return ;
}
int DFSR(int x,int y,int dir,int num){//向右搜索一样,同向左的同理。
if(x==endx&&y==endy){
flag2=true;
return num;
}
if(!flag2){
if(dir==){
if(!flag2&&y+<=m&&map1[x][y+]!='#')
return DFSR(x,y+,,num+);
if(!flag2&&x->=&&map1[x-][y]!='#')
return DFSR(x-,y,,num+);
if(!flag2&&y->=&&map1[x][y-]!='#')
return DFSR(x,y-,,num+);
if(!flag2&&x+<=n&&map1[x+][y]!='#')
return DFSR(x+,y,,num+);
}
if(dir==){
if(!flag2&&x->=&&map1[x-][y]!='#')
return DFSR(x-,y,,num+);
if(!flag2&&y->=&&map1[x][y-]!='#')
return DFSR(x,y-,,num+);
if(!flag2&&x+<=n&&map1[x+][y]!='#')
return DFSR(x+,y,,num+);
if(!flag2&&y+<=m&&map1[x][y+]!='#')
return DFSR(x,y+,,num+);
}
if(dir==){
if(!flag2&&y->=&&map1[x][y-]!='#')
return DFSR(x,y-,,num+);
if(!flag2&&x+<=n&&map1[x+][y]!='#')
return DFSR(x+,y,,num+);
if(!flag2&&y+<=m&&map1[x][y+]!='#')
return DFSR(x,y+,,num+);
if(!flag2&&x->=&&map1[x-][y]!='#')
return DFSR(x-,y,,num+);
}
if(dir==){
if(!flag2&&x+<=n&&map1[x+][y]!='#')
return DFSR(x+,y,,num+);
if(!flag2&&y+<=m&&map1[x][y+]!='#')
return DFSR(x,y+,,num+);
if(!flag2&&x->=&&map1[x-][y]!='#')
return DFSR(x-,y,,num+);
if(!flag2&&y->=&&map1[x][y-]!='#')
return DFSR(x,y-,,num+);
}
}
return ;
}
int main(){
int t;
scanf("%d",&t);
while(t--){
scanf("%d%d",&m,&n);
for(int i=;i<=n;i++){
for(int j=;j<=m;j++){
cin>>map1[i][j];
if(map1[i][j]=='S'){
startx=i;
starty=j;
}
if(map1[i][j]=='E'){
endx=i;
endy=j;
}
}
}
flag1=flag2=;
printf("%d %d %d\n", DFSL(startx,starty,,),DFSR(startx,starty,,) ,BFS(startx,starty)+);
}
}
Children of the Candy Corn的更多相关文章
- poj 3083 Children of the Candy Corn
点击打开链接 Children of the Candy Corn Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8288 ...
- 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 ...
- POJ3083——Children of the Candy Corn(DFS+BFS)
Children of the Candy Corn DescriptionThe cornfield maze is a popular Halloween treat. Visitors are ...
- 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)
Children of the Candy Corn Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9311 Accepted: ...
- K - Children of the Candy Corn(待续)
K - Children of the Candy Corn Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d ...
- poj3083 Children of the Candy Corn BFS&&DFS
Children of the Candy Corn Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11215 Acce ...
- 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: 11015 Acce ...
- poj 3083 Children of the Candy Corn (广搜,模拟,简单)
题目 靠墙走用 模拟,我写的是靠左走,因为靠右走相当于 靠左走从终点走到起点. 最短路径 用bfs. #define _CRT_SECURE_NO_WARNINGS #include<stdio ...
随机推荐
- 数据存储(一)--SharedPreferences之你不知道的事
一.SharedPreferences将数据文件保存在指定路径上 SharedPreferences原则上是仅仅能保存在当前应用程序私有的shared_prefs文件夹中,只是也不是绝对的,我们能够用 ...
- agentzh --春哥--调试专家
https://github.com/agentzh/perl-systemtap-toolkit https://github.com/openresty http://openresty.org/ ...
- empty函数PHP
empty译为: adj.空的,空虚的,空洞的;空闲的,无效的,徒劳的;无聊的,愚蠢的;言语或行动空洞的 vt.(使)成为空的, 把…弄空;把…腾出来 vi.成为空的;流空 n.空车;空的东西 是PH ...
- sqlserver 2008表分区操作
表分区操作步骤 1.设计表进行分区的方案,水平分区.垂直分区 a.水平切割将减少表的行数,这样可以将历史数据归档,减少表大小,提高访问速度. b.垂直切割将分为主表和从表方式,将主要的列字段存放在主表 ...
- PhoneGap 在eclipse上开发Android程序
本文将记录在Eclipes上开发Android App,在使用的方法是Hybrid App(混合模式移动应用), 由于本人的工作需要,将要开发在车间使用的数据录入程序,但是其中有非常多的逻辑验证和判断 ...
- CakePHP的blog教程三
简单的身份验证和授权应用 接着我们blog教程的例子,如果我们想要建立一个根据登录的用户身份来决定其安全访问到正确的urls. 同时我们还有其他的需求: 允许我们的blog有多个作者,每一个作者都可以 ...
- cocos2dx Hello world 创建
环境搭建好后,就要开始创建自己的第一个hello world项目了 因为没有安装其他的插件,所以最开始只能手动创建 首先通过cmd 进入你的cocos2dx的路径下: D:\soft\cocos2d- ...
- xpath 操作XML
1.xpath 操作XML,底下部分代码被注释了,但是是完整功能,去除注释是正常使用的(有写命名和其他冲突,所以注释了) 总体有:完整读取xml,对xml的增删改查,对xml的特定操作 using S ...
- HTTP请求返回的NSData无法转换为NSString
最近在做的一个项目中有一个功能是有一个网页,模拟http请求获取到这个网页返回的相应的数据. 在请求完成后获取到的数据为NSData类型,按照我们通常的转换为NSString的方法: NSString ...
- C++ Reference 的“三位一体”诠释
C++ 是介于汇编语言与高级语言之间的一种“全能”语言.它的能力是其他任何基于VMA(冯-诺曼架构)计算机的高级程序设计语言无法望其项背的,而性能也只有C语言可与之伯仲. 然而长期以来,喜欢C++和憎 ...