POJ 3083 Bfs+Dfs

注意求最短路的时候用Bfs。

#include<iostream>
#include<stdio.h>
using namespace std;
int w,h,ex,ey,sx,sy;
int map[100][100],can[100][100];
struct vid{

  int x,y,step;
}queue[5000];
int zan[4][2]={{-1,0},{0,1},{1,0},{0,-1}};
int dirl[4][2]={0,-1,-1,0,0,1,1,0},dirr[4][2]={0,1,1,0,0,-1,-1,0};
int dfsr(int dstep,int x,int y,int di )
{
  int i,temp1,temp2;

  for(i=0;i<4;i++)
  {
      temp1=x+dirr[i][0];
      temp2=y+dirr[i][1];

    if((temp1>=0)&&(temp1<h)&&(temp2>=0)&&(temp2<w))
    {
      if(dfsr(dstep+1,x+dirr[i][0],y+dirr[i][1],i))
        return dstep;
      else
        return 0;
    }

  }
  return 0;

}
int dfsl(int dstep,int x,int y,int di )
{

  int i,temp1,temp2;

  for(i=0;i<4;i++)
  {
      temp1=x+dirl[i][0];
      temp2=y+dirl[i][1];

    if((temp1>=0)&&(temp1<h)&&(temp2>=0)&&(temp2<w))
    {
      if(dfsl(dstep+1,temp1,temp2,i))
        return dstep;
      else
        return 0;
    }

  }

  return 0;
}
int bfs()
{

if (sx == ex && sy == ey)
    {

        return 1;
    }
  int t,ww,x,y,t1,t2;
  t=ww=1;
  queue[t].x=sx;
  queue[t].y=sy;
  queue[t].step=0;
  can[sx][sy]=1;
  while(t<=ww&&!can[ex][ey])
  {

    x=queue[t].x;
    y=queue[t].y;
    for(int i=0;i<4;i++)
      if((!map[x+zan[i][0]][y+zan[i][1]])&&(!can[x+zan[i][0]][y+zan[i][1]]))
        {

          t1=x+zan[i][0];
          t2=y+zan[i][1];
          if(t1>=0&&(t1<h)&&(t2>=0)&&(t2<w)&&(!map[t1][t2])&&(!can[t1][t2]))
          {

            queue[++ww].x=t1;
            queue[ww].y=t2;
            queue[ww].step=queue[t].step+1;
            can[t1][t2]=1;
          }
        }
      t++;
  }
  return queue[ww].step+1;
}
int main ()
{
  int t;
  char c;
  scanf("%d",&t);
  getchar();
  while(t--)
  {

    scanf("%d%d",&w,&h);
    getchar();
    for(int i=0;i<h;i++)
    {
      for(int j=0;j<w;j++)
      {
        can[i][j]=0;
        c=getchar();
        if(c==‘#‘)
          map[i][j]=1;
        else if(c==‘.‘)
          map[i][j]=0;
        else if(c==‘S‘)
        {
          map[i][j]=0;
          sx=i,sy=j;
        }
        else
          if(c==‘E‘)
          {
            map[i][j]=0;
            ex=i;ey=j;
          }
      }
      getchar();
    }
  //	init();
  //	dfsr();
  //	printf("%d ",bfs());
    printf("%d %d %d\n",dfsl(0,sx,sy,0)+1,dfsr(0,sx,sy,0)+1,bfs());
  }

  return 0;
}
时间: 2024-08-16 09:36:31

POJ 3083 Bfs+Dfs的相关文章

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

Children of the Candy Corn Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9311 Accepted: 4039 Description The cornfield maze is a popular Halloween treat. Visitors are shown the entrance and must wander through the maze facing zombies, ch

POJ 3083 Children of the Candy Corn(顺时针DFS+逆时针DFS+BFS)

题目链接:POJ 3083 Children of the Candy Corn [题意]给出一个迷宫,不超过40*40,'#'代表墙,'.'代表能走,'S'是起点,'E'是终点.分别求出从起点一直沿左走,一直沿右走,走到终点所需要的步数.以及走出迷宫的最小步数. [思路]首先最小步数很简单,一个普通BFS搞定,这道题重点是一直向左走和一直向右走的DFS的方向问题,方向还和游客当时朝向有关.开始一直认为是每次都向左(右)转,直到可以走,然后就一直不对,在google了之后才知道向左走要遵循左上右

POJ 3083 -- Children of the Candy Corn(DFS+BFS)TLE

POJ 3083 -- Children of the Candy Corn(DFS+BFS) 题意: 给定一个迷宫,S是起点,E是终点,#是墙不可走,.可以走 1)先输出左转优先时,从S到E的步数 2)再输出右转优先时,从S到E的步数 3)最后输出S到E的最短步数 解题思路: 前两问DFS,转向只要控制一下旋转方向就可以 首先设置前进方向对应的数字 向上--N--0 向右--E--1 向下--S--2 向左--W--3 比如说右转优先,即为向右,向前,向左,向后,即逆时针方向for(int i

poj 3083 dfs,bfs

Children of the Candy Corn Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 3083 Description The cornfield maze is a popular Halloween treat. Visitors are shown the entrance and must wander throu

Dearboy&#39;s Puzzle (poj 2308 搜索 dfs+bfs)

Language: Default Dearboy's Puzzle Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 1202   Accepted: 208 Description Dearboy is a game lover. Recently, he loves playing the game Lian Lian Kan. This game is played on a board with N*M grids

POJ 1130(一道纯水,bfs+dfs)

POJ 1130 大概题意:给出一副图,求从起点到终点 (0->ET) 必须经过的一点. 我的思路:首先DFS求出经过每点的次数,必过的一点的次数一定最高,但是就这样吗?有可能有多个必过的点,所以还要找出离ET最近的点,这里就利用BFS逐层搜索的性质求每点到ET的距离. #include<iostream> #include<cstdio> #include<string.h> #include<iomanip> #include<queue&g

邻结矩阵的建立和 BFS,DFS;;

邻结矩阵比较简单,, 它的BFS,DFS, 两种遍历也比较简单,一个用队列, 一个用数组即可!!!但是邻接矩阵极其浪费空间,尤其是当它是一个稀疏矩阵的时候!!!---------------------------------------------------------------------------------------------------------------------------------------//邻接矩阵的建立和 其BFS, DFS, 遍历 #include <

HDU ACM 1044 Collect More Jewels BFS+DFS

题意:在一个迷宫中,有一些宝物,从起点走到终点,问在给定的时间内,到达终点后所能拾取珠宝的最大价值. 分析(BFS+DFS): 1.求入口到第一个取宝物的地方的最短距离 2.求第i个取宝物的地方到第i+1个取宝物的地方的最短距离 3.求第n个取宝物的地方到出口的最短距离 4.保证以上3点能在时间L内实现的情况下,取得的宝石价值最大. BFS特点:对于解决最短或最少问题特别有效,而且寻找深度小,但缺点是内存耗费量大(需要开大量的数组单元来存储状态) DFS特点:对于解决遍历和求所有问题有效,对于问

建图方式一 之 ”前向星“ BFS&amp;&amp;DFS 简单应用

三种建图方式,邻接矩阵.前向星(边表集).邻接链表! 耗时一晚上 ,好好研究了一下 前向星,因为我的指针用的实在是很烂,所以还是 入赘 前向星吧. 问了学长,看了大牛们的博客,终于有点收获了,个人认为 前向星Very Well. 前向星 建图方法: 以储存边的方式来储存图.在构造图时,把边存放在数组里,不需使用指针,只需一个 next  即可是整个图构建完成 . 适用条件: 点集特别多的稀疏图,边数多且繁杂,开邻接矩阵会浪费大量内存. 时间复杂度: O(m),并不比邻接链表差. #include