基础BFS+DFS poj3083
//满基础的一道题
//最短路径肯定是BFS。
//然后靠右,靠左,就DFS啦
//根据前一个状态推出下一个状态,举靠左的例子,如果一开始是上的话,那么他的接下来依次就是 左,上 , 右 , 下
//如果我们用数组存一下 左,上 , 右 , 下,
//而且你会发现他原来的状态是d,下一个状态就是=((d-1+4)%4+i)%4,其实你比划一下,就会知道左的话就是下开始变,上的话就是左开始变。
//同比靠右也是一样
//初始化,题目条件是
//they will always be located along one of the maze edges and never in a corner
//所以开始搞一下状态就好了
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <algorithm>
#include <iostream>
using namespace std;
#define LL __int64
#define N 50
char ma[N][N];
bool vis[N][N];
int n,m;
int bx,by;
int ex,ey;
int dx[4]={0,0,1,-1};
int dy[4]={1,-1,0,0};
int dl[][2]={{0,-1},{-1,0},{0,1},{1,0}};
int dr[][2]={{0,1},{-1,0},{0,-1},{1,0}};
struct asd{
int x,y;
int step;
};
asd q[1000010];
int head,tail;
int ans1,ans2,ans3;
int BFS()
{
head=0;tail=1;
q[head].x=bx;
q[head].y=by;
q[head].step=0;
memset(vis,0,sizeof(vis));
while(head<tail)
{
int a=q[head].x;
int b=q[head].y;
if(a==ex&&b==ey)
return q[head].step;
for(int i=0;i<4;i++)
{
int aa=a+dx[i];
int bb=b+dy[i];
if(aa<0||bb<0||aa>=n||bb>=m||vis[aa][bb]||ma[aa][bb]=='#')
continue;
vis[aa][bb]=1;
q[tail].x=aa;
q[tail].y=bb;
q[tail].step=q[head].step+1;
tail++;
}
head++;
}
}
int flag;
int dfs(int x,int y,int d,int step,int dir[][2])
{
for(int i=0;i<4;i++)
{
int j=((d-1+4)%4+i)%4;
int aa=x+dir[j][0];
int bb=y+dir[j][1];
if(aa<0||bb<0||aa>=n||bb>=m||ma[aa][bb]=='#')
continue;
if(aa==ex&&bb==ey)
{
return step+1;
}
return dfs(aa,bb,j,step+1,dir);
}
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&m,&n);
for(int i=0;i<n;i++)
scanf("%s",ma[i]);
int f1,f2;
f1=f2=0;
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
if(ma[i][j]=='S')
{
bx=i;by=j;
f1=1;
}
if(ma[i][j]=='E')
{
ex=i;ey=j;
f2=1;
}
if(f1&&f2)
break;
}
if(f1&&f2)
break;
}
int d1,d2;
if(bx == 0){
d1 = 3;
d2 = 3;
}
else if(bx == n-1){
d1 = 1;
d2 = 1;
}
else if(by == 0){
d1 = 2;
d2 = 0;
}
else if(by == m-1){
d1 = 0;
d2 = 2;
}
ans1=dfs(bx,by,d1,1,dl);
ans2=dfs(bx,by,d2,1,dr);
ans3=BFS();
printf("%d ",ans1);
printf("%d ",ans2);
printf("%d\n",ans3+1);
}
}
基础BFS+DFS poj3083的更多相关文章
- poj1979【基础bfs/dfs】
挑战习题搜索-1 题意: 给定起点,然后求一个可以到达的数量,位置"."都可以走.每次应该是上下左右都可以走. 思路: 这题应该DFS更好写,但是BFS也可以写吧. 好久没写了- ...
- POJ 2227 The Wedding Juicer (优先级队列+bfs+dfs)
思路描述来自:http://hi.baidu.com/perfectcai_/item/701f2efa460cedcb0dd1c820也可以参考黑书P89的积水. 题意:Farmer John有一个 ...
- 邻结矩阵的建立和 BFS,DFS;;
邻结矩阵比较简单,, 它的BFS,DFS, 两种遍历也比较简单,一个用队列, 一个用数组即可!!!但是邻接矩阵极其浪费空间,尤其是当它是一个稀疏矩阵的时候!!!-------------------- ...
- Collect More Jewels(hdu1044)(BFS+DFS)
Collect More Jewels Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Othe ...
- Cleaning Robot (bfs+dfs)
Cleaning Robot (bfs+dfs) Here, we want to solve path planning for a mobile robot cleaning a rectangu ...
- LeetCode:BFS/DFS
BFS/DFS 在树专题和回溯算法中其实已经涉及到了BFS和DFS算法,这里单独提出再进一步学习一下 BFS 广度优先遍历 Breadth-First-Search 这部分的内容也主要是学习了labu ...
- POJ3083 Children of the Candy Corn(Bfs + Dfs)
题意:给一个w*h的迷宫,其中矩阵里面 S是起点,E是终点,“#”不可走,“.”可走,而且,S.E都只会在边界并且,不会在角落,例如(0,0),输出的话,每组数据就输出三个整数,第一个整数,指的是,以 ...
- 数据结构基础(21) --DFS与BFS
DFS 从图中某个顶点V0 出发,访问此顶点,然后依次从V0的各个未被访问的邻接点出发深度优先搜索遍历图,直至图中所有和V0有路径相通的顶点都被访问到(使用堆栈). //使用邻接矩阵存储的无向图的深度 ...
- POJ-3083 Children of the Candy Corn (BFS+DFS)
Description The cornfield maze is a popular Halloween treat. Visitors are shown the entrance and mus ...
随机推荐
- python实现的websocket总结 —— wspy
之前曾有php版的websocket封装包.见Websocket--php实战,近期使用python做一些功能,须要用到对websocket的操作,因此,參照之前的实现,实现了这个python版本号. ...
- uboot 对 FAT 分区的解析
uboot 对 FAT 分区的解析 改写 UBOOT 从 U 盘读入固件,然后刷机.发现有的 U 盘无法正确读到分区,跟踪了一下发现自己写的代码有漏洞,只尝试解析分区表里的第一个分区.跟踪的过程中重温 ...
- HDOJ1006
#include <cstdio>#include <algorithm>using namespace std;const double UB=43200;const dou ...
- c++string 输入换行符
string 一次只能输入一行,不含换行符.可以自己添加换行符 和输入行数.例如:#include <iostream>#include <string>using names ...
- css hack (ie6-ie9+)
IE6 css hack: 1. *html Selector {} /* Selector 表示 css选择器 下同 */ 2. Selector { _property: value; } /* ...
- VMWare Workstation 配置docker多macvlan网络方法
VMWare Workstation 配置docker多macvlan网络方法 答案就是.....换VirtualBox 噗... VMWare Workstation host-only网络,三台虚 ...
- caioj1465&&poj1024: 【AC自动机】地图匹配
刷的第二题AC自动机,这题简直了.. 用询问的串建AC自动机,然后...爆搜! ACBB ACBBACCA A AABBC ...
- hdu 1400 Mondriaan's Dream 解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1400 题目意思:给出一个h * w的 大 矩形,需要用 1 * 2 的砖块去填充这个大矩形,问填充的方 ...
- SPOJ:Help BTW(二分)
BTW wants to buy a gift for her BF and plans to buy an integer array. Generally Integer arrays are c ...
- SPOJ:Lost and survived(multiset+并查集)
On September 22, 2004, Oceanic Flight 815 crashed on a mysterious island somewhere in the pacific. T ...