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 ...
随机推荐
- css3 设置背景图片大小(缩略图形式缩小)
废话当然不说了. 直接上代码 <style> #mycon { background:url('Tpl/1.jpg'); background-size:400px 400px; back ...
- Oracle、MySql、Sql Server比对
1. 价格 MySql:廉价(部分免费):当前,MySQL採用双重授权(DualLicensed),他们是GPL和MySQLAB制定的商业许可协议.假设你在一个遵循GPL的自由(开源)项目中使用 ...
- nandflash中oob、ecc分析
1.为何需要分析? 最近一直接触这类驱动,如果对它的原理不懂的话,驱动调试会很麻烦!!!!!! 2.ecc? nand的纠错能力,目前有1位.4位和8位,也就是说在512字节中如果是4位的ecc那就可 ...
- Innodb_buffer_pool_pages_dirty [一个故事@MySQL DBA]MYSQL
http://www.orczhou.com/index.php/2010/12/more-about-mysql-innodb-shutdown/http://www.orczhou.com/ind ...
- iOS 使用Charts框架 折线,柱状,K线,饼状,雷达全攻略
我是前言: 大约几个月前我在某平台写了一篇文章, 文中简单地介绍了Charts两种图表的样式的使用, 不过有种意犹未尽的感觉, 利用周末的空闲时间再次看了看, 有了新的收获, 今天发出来,分享给大家, ...
- FastDFS问题汇总
问题1: 增加分组后,新的storge不可用. 增加一个分组group2,发现上传文件失败.在group2中的storage中使用netstat -anp|grep fdfs,发现端口状态为CLOSE ...
- CentOS 6.7编译安装MySQL 5.6
1.安装前准备 yum install make gcc gcc-c++ ncurses-devel perl bison-devel yum groupinstall "Developme ...
- Wpf 鼠标拖动元素实例
1.Wpf中鼠标捕获和释放 //以矩形为例 //创建鼠标捕获 Mouse.Capture(rectOne); //释放鼠标捕获 rectOne.ReleaseMouseCapture(); 2.Wpf ...
- (转)smarty实现多级分类的方法
--http://www.aspku.com/kaifa/php/44679.html 这篇文章主要介绍了smarty实现多级分类的方法,涉及循环读取的技巧,非常具有实用价值,需要的朋友可以参考下 ...
- ASP.NET Web API 文件產生器 - 使用 Swagger
转帖:http://kevintsengtw.blogspot.hk/2015/12/aspnet-web-api-swagger.html Swagger 是一套 API 互動文件產生器,使用 HT ...