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 ...
随机推荐
- OCCI处理CHAR类型字符串变量的不同
问题背景: 一个旧应用,原先应用是用proc写的,9i的库,如今应用须要改为使用OCCI,当中有一段查询逻辑:select ... where upper(state)=upper(:1). (此处请 ...
- spring 自定义schema
扩展schema,定义自己的bean属性..不错! 主要: 1,定义META-INF下.xsd文件,这里是people.xsd;定义spring.handlers;定义spring.schemas 2 ...
- [React + Mobx] Mobx and React intro: syncing the UI with the app state using observable and observer
Applications are driven by state. Many things, like the user interface, should always be consistent ...
- Windows如何打包Qt程序
很多Qt爱好者想发布自己的Qt软件,但却发现在其他没有安装Qt SDK的机器上无法运行,这就是本文想要说明的问题.现在网上大部分软件都要发布自己开发的应用程序,都会打包到exe文件中,待安装完exe文 ...
- R cannot be resolved to a variable
1. 检查Android 的SDK是否丢失需要重新下载,检查build path,把需要导入的JAR包确认都导入成功 2. 确保class没有import Android.R,注意是不能有Androi ...
- img标签块状与内联的博弈
新手,请前辈们不吝赐教 说到html中img标签是内联还是块状元素,我们首先要知道什么是内联(inline),什么又是块状(block)? 我也在网上查看了一些别人分享的经验,有一个讲到了文档流的概念 ...
- DataTbale取值
有一个DataTable数据 //创建DataTable对象 DataTable dt = new DataTable("Table_AX"); //为DataTable创建列 / ...
- eclipse build很慢的时候,有可能是js文件编译验证慢的问题
第一步: 去除eclipse的JS验证: 将windows->preference->Java Script->Validator->Errors/Warnings-> ...
- 解决Xcode7多个模拟器的方法
xcode模拟器都这样显示,没办法判断是哪个系统,解决办法是 1.关闭xcode 2.终端输入 sudo killall -9 com.apple.CoreSimulator.CoreSimulato ...
- 一条sql语句循环插入N条不同记录(转)
SET NOCOUNT ON IF (OBJECT_ID('TB' ) IS NOT NULL ) DROP TABLE TB GO CREATE TABLE TB(ID INT IDENTITY ( ...