poj 3083 Children of the Candy Corn (广搜,模拟,简单)
靠墙走用 模拟,我写的是靠左走,因为靠右走相当于 靠左走从终点走到起点。
最短路径 用bfs。
#define _CRT_SECURE_NO_WARNINGS #include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#include<queue>
using namespace std;
#define MAXN 110
char map[MAXN][MAXN];
int n,m;
int xx[]={,,,-};
int yy[]={,,-,};
struct tt
{
int x,y,step;
}; int bfs(int x,int y,int x1,int y1)
{
int ans=,i;
tt front,rear,temp;
queue<tt>q;
while(!q.empty())
q.pop();
front.x=x,front.y=y,front.step=;
ans=;
q.push(front);
i=;
while(!q.empty())
{
temp=q.front();
if(temp.x==x1&&temp.y==y1)return ans;
q.pop();
ans++;
i=(i+)%;
rear.x=temp.x+xx[i];
rear.y=temp.y+yy[i];
if(rear.x>=&&rear.x<n&&rear.y>=&&rear.y<m&&map[rear.x][rear.y]!='#')
{
q.push(rear);
}
else
for(;;i=(i+)%)
{
rear.x=temp.x+xx[i];
rear.y=temp.y+yy[i];
if(rear.x>=&&rear.x<n&&rear.y>=&&rear.y<m&&map[rear.x][rear.y]!='#')
{
q.push(rear);
break;
}
}
}
return ans;
} int bfs1(int x,int y,int x1,int y1)
{
tt front,rear,temp;
queue<tt>q;
while(!q.empty())
q.pop();
front.x=x,front.y=y,front.step=;
q.push(front);
map[x][y]='#';
while(!q.empty())
{
temp=q.front();
if(temp.x==x1&&temp.y==y1)return temp.step;
q.pop();
for(int i=;i<;i++)
{
rear.x=temp.x+xx[i];
rear.y=temp.y+yy[i];
if(rear.x>=&&rear.x<n&&rear.y>=&&rear.y<m&&map[rear.x][rear.y]!='#')
{
rear.step=temp.step+;
q.push(rear);
map[rear.x][rear.y]='#';
}
}
}
return ;
}
int main()
{
int i,t,j,sx,sy,ex,ey,a,b,c;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&m,&n);
for(i=;i<n;i++)
{
scanf("%s",map[i]);
for(j=;j<m;j++)
{
if(map[i][j]=='S')
{
sx=i;sy=j;
}
else if(map[i][j]=='E')
{
ex=i;ey=j;
}
}
}
a=bfs(sx,sy,ex,ey);
b=bfs(ex,ey,sx,sy);
c=bfs1(sx,sy,ex,ey);
printf("%d %d %d\n",a,b,c);
}
return ;
}
poj 3083 Children of the Candy Corn (广搜,模拟,简单)的更多相关文章
- POJ 3083 Children of the Candy Corn (DFS + BFS + 模拟)
题目链接:http://poj.org/problem?id=3083 题意: 这里有一个w * h的迷宫,给你入口和出口,让你分别求以下三种情况时,到达出口的步数(总步数包括入口和出口): 第一种: ...
- 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: 8288 ...
- 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)
做了1天,总是各种错误,很无语 最后还是参考大神的方法 题目:http://poj.org/problem?id=3083 题意:从s到e找分别按照左侧优先和右侧优先的最短路径,和实际的最短路径 DF ...
- POJ:3083 Children of the Candy Corn(bfs+dfs)
http://poj.org/problem?id=3083 Description The cornfield maze is a popular Halloween treat. Visitors ...
- poj 3083 Children of the Candy Corn 【条件约束dfs搜索 + bfs搜索】【复习搜索题目一定要看这道题目】
题目地址:http://poj.org/problem?id=3083 Sample Input 2 8 8 ######## #......# #.####.# #.####.# #.####.# ...
- POJ 3083 Children of the Candy Corn 解题报告
最短用BFS即可.关于左手走和右手走也很容易理解,走的顺序是左上右下. 值得注意的是,从起点到终点的右手走法和从终点到起点的左手走法步数是一样. 所以写一个左手走法就好了.贴代码,0MS #inclu ...
- POJ 3083 Children of the Candy Corn (DFS + BFS)
POJ-3083 题意: 给一个h*w的地图. '#'表示墙: '.'表示空地: 'S'表示起点: 'E'表示终点: 1)在地图中仅有一个'S'和一个'E',他们为位于地图的边墙,不在墙角: 2)地图 ...
随机推荐
- 对象属性封装到map中
import java.beans.PropertyDescriptor; import java.lang.reflect.Method; import java.lang.reflect.Modi ...
- POJ 2837 Til the Cows Come Home
Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 45515 Accepted: 15434 Description Bes ...
- bzoj 1006: [HNOI2008]神奇的国度
这是个标准的弦图,但如果不知道弦图就惨了=_= 趁着这个机会了解了一下弦图,主要就是完美消除序列,求出了这个就可以根据序列进行贪心染色. 貌似这个序列很神,但是具体应用不了解…… 这道题为什么可以这么 ...
- Java设计模式之--代理模式学习
1.代理模式定义 为其他对象提供一种代理以控制对这个对象的访问.代理对象起到中介服务,可以去掉功能服务和增加额外的服务. 其实按照官方的说法可能不太好理解,代理模式就好比我们生活中买票,一般情况下我们 ...
- iOS开发零基础教程之生成git所需的SSH keys
在我们github看到了一个不错的第三方库时,可能我们想把他git clone到本地,我们需要复制他的SSH URL,如下图: 复制完地址之后,我们需要打开终端,然后输入命令: git clone + ...
- 转:Java HashMap实现详解
Java HashMap实现详解 转:http://beyond99.blog.51cto.com/1469451/429789 1. HashMap概述: HashMap是基于哈希表的M ...
- SQL允许远程访问
1.打开sqlserver对象资源管理器 右键 方面 常规 服务器配置 RemoteAccessEnabled true RemoteDacEnabled true 2.打开SQL SERVER管理 ...
- nginx配置:支持phpfastcgi,nginx和php-cgi通信,部分nginx常量解释
支持phpfastcgi的配置如下: server { listen 8000; server_name localhost; root F:/home/projects/test; i ...
- WCF全面解析第二章 地址(Adress)
2.1 统一资源标识(URL) 2.1.1 Http/Https 2.1.2 Net.TCP 2.1.3 Net.Pipe WCF只将命名管道专门用于同一台机器的跨进程通信. 2.1.4 Net.Ms ...
- 【jquery】javaScript中prototype的妙用 巧妙运用prototype属性原型链创建对象
prototype 可以有好多有优化实现方法 http://blog.csdn.net/liuqiwen0512/article/details/8089690 在 JavaScript 中,每个函 ...