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的更多相关文章

  1. poj 3083 Children of the Candy Corn

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

  2. 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 ...

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

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

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

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

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

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

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

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

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

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

  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 ...

  10. poj 3083 Children of the Candy Corn (广搜,模拟,简单)

    题目 靠墙走用 模拟,我写的是靠左走,因为靠右走相当于 靠左走从终点走到起点. 最短路径 用bfs. #define _CRT_SECURE_NO_WARNINGS #include<stdio ...

随机推荐

  1. Java和JavaScript中使用Json方法大全

    林炳文Evankaka原创作品. 转载请注明出处http://blog.csdn.net/evankaka   摘要:JSON(JavaScript Object Notation) 是一种轻量级的数 ...

  2. iOS NavigaitonController详解(code版)

    参考文章:http://blog.csdn.net/totogo2010/article/details/7681879,参考了这篇文章,写的超级好,自己他的基础上加上了自己的理解. 下面的图显示了导 ...

  3. [转] 「指尖上的魔法」 - 谈谈 React Native 中的手势

    http://gold.xitu.io/entry/55fa202960b28497519db23f React-Native是一款由Facebook开发并开源的框架,主要卖点是使用JavaScrip ...

  4. Block之变量作用域

    在使用block的过程中经常会调用不同类型.不同作用域的变量,如果对这些变量作用域的理解稍有偏差,就会出现问题.故此特意整理出block中会经常使用到的几种变量,如有补充,欢迎指出. 1. 局部变量 ...

  5. Android 开发实践 ViewGroup 实现左右滑出窗口(一)

    利用假期把以前做的东西总结整理一下,先从简单的开始吧.实现的效果是这样的:   做了个截屏动画,比例有点不对了,凑合着看吧. 整个窗口有3部分组成,中间的主界面是个列表,左边的滑出界面是个菜单,右边的 ...

  6. hibernate通过判断参数动态组合Hql语句,生成基本通用查询

    // public List find(Station entity) { List reuslt = null; // 字符串辅助类 StringBuffer hql = new StringBuf ...

  7. object标签参考(转载)

    <object> 元素可支持多种不同的媒介类型,比如: 图片 音频 视频 Other 对象 显示图片 你可以显示一幅图片: <object height="100%&quo ...

  8. PHP上传原理及应用

    概要 1.FORM表现enctype属性 2.$_FILES系统函数 3.move_uploaded_file函数 4.is_uploaded_file函数 1.FORM标签的enctype属性 只有 ...

  9. Python:if-while-for

    #!/usr/bin/python3 #if elif else print("开始猜数字游戏") num = int(input("请输入数字")) stan ...

  10. Java线程(学习整理)--2---加入另一个线程join

    1.join简介: 今天刚学的,这里我简单总结一下,join本身就是“加入”的意思,那么在线程中是什么意思呢?是在一个线程的run方法执行过程中,当特殊情况下需要执行一些其他的操作的时候,我们会用到j ...