dfs+bfs poj 3038

Children of the Candy Corn

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 10331   Accepted: 4466

Description

The cornfield maze is a popular Halloween treat. Visitors are shown the entrance and must wander through the maze facing zombies, chainsaw-wielding psychopaths, hippies, and other terrors on their quest to find the exit.

One popular maze-walking strategy guarantees that the visitor will eventually find the exit. Simply choose either the right or left wall, and follow it. Of course, there‘s no guarantee which strategy (left or right) will be better, and the path taken is seldom the most efficient. (It also doesn‘t work on mazes with exits that are not on the edge; those types of mazes are not represented in this problem.)

As the proprieter of a cornfield that is about to be converted into a maze, you‘d like to have a computer program that can determine the left and right-hand paths along with the shortest path so that you can figure out which layout has the best chance of confounding visitors.

Input

Input to this problem will begin with a line containing a single integer n indicating the number of mazes. Each maze will consist of one line with a width, w, and height, h (3 <= w, h <= 40), followed by h lines of w characters each that represent the maze layout. Walls are represented by hash marks (‘#‘), empty space by periods (‘.‘), the start by an ‘S‘ and the exit by an ‘E‘.

Exactly one ‘S‘ and one ‘E‘ will be present in the maze, and they will always be located along one of the maze edges and never in a corner. The maze will be fully enclosed by walls (‘#‘), with the only openings being the ‘S‘ and ‘E‘. The ‘S‘ and ‘E‘ will also be separated by at least one wall (‘#‘).

You may assume that the maze exit is always reachable from the start point.

Output

For each maze in the input, output on a single line the number of (not necessarily unique) squares that a person would visit (including the ‘S‘ and ‘E‘) for (in order) the left, right, and shortest paths, separated by a single space each. Movement from one square to another is only allowed in the horizontal or vertical direction; movement along the diagonals is not allowed.

Sample Input

2
8 8
########
#......#
#.####.#
#.####.#
#.####.#
#.####.#
#...#..#
#S#E####
9 5
#########
#.#.#.#.#
S.......E
#.#.#.#.#
#########

Sample Output

37 5 5
17 17 9

Source

South Central USA 2006

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<queue>
#include<vector>
using namespace std;
queue<int> q;
int tt,n,m,sx,sy,ex,ey,cnt;
bool flag,vis[50][50];
int dic[4][2]={{0,-1},{-1,0},{0,1},{1,0}};
char s[50][50];
bool check(int x,int y)
{
    if(x<0||x>=n||y<0||y>=m)
        return false;
    return true;
}
void dfs(int x,int y,int xx,int yy,int d)
{
    if(x==xx&&y==yy)
    {
        flag=1;
        return ;
    }
    d=(d+3)%4;
    for(int i=d;i<d+4;i++)
    {
        int tx,ty;
        tx=x+dic[i%4][0],ty=y+dic[i%4][1];
        if(!check(tx,ty)||s[tx][ty]==‘#‘) continue;
        cnt++;
        dfs(tx,ty,xx,yy,i%4);
        if(flag)
            return ;
    }
}

int bfs()
{
    int step,x,y,xx,yy;
    while(!q.empty()) q.pop();
    q.push(sx),q.push(sy),q.push(1);
    vis[sx][sy]=1;
    while(!q.empty())
    {
        x=q.front(),q.pop();
        y=q.front(),q.pop();
        step=q.front(),q.pop();
        if(x==ex&&y==ey)
            return step;
        for(int i=0;i<4;i++)
        {
            xx=x+dic[i][0];
            yy=y+dic[i][1];
            if(!check(xx,yy)||vis[xx][yy]||s[xx][yy]==‘#‘) continue;
            q.push(xx),q.push(yy),q.push(step+1);
            vis[xx][yy]=1;
        }
    }
}
int main()
{
    scanf("%d",&tt);
    while(tt--)
    {
        memset(vis,0,sizeof(vis));
        scanf("%d%d",&m,&n);
        for(int i=0;i<n;i++)
            scanf("%s",s[i]);
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<m;j++)
            {
                if(s[i][j]==‘S‘)
                    sx=i,sy=j;
                if(s[i][j]==‘E‘)
                    ex=i,ey=j;
            }
        }
        cnt=1;
        flag=0;
        dfs(sx,sy,ex,ey,0);
        printf("%d",cnt);
        cnt=1;
        flag=0;
        dfs(ex,ey,sx,sy,0);
        printf(" %d",cnt);
        printf(" %d\n",bfs());
    }
    return 0;
}

  

时间: 2024-10-28 19:57:23

dfs+bfs poj 3038的相关文章

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 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+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

poj3083——dfs+bfs综合题

POJ 3083   dfs+bfs+模拟 Children of the Candy Corn Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10564   Accepted: 4539 Description The cornfield maze is a popular Halloween treat. Visitors are shown the entrance and must wander through

FZU1205/SDUT1157_小鼠迷宫问题(DFS+BFS)

解题报告 http://blog.csdn.net/juncoder/article/details/38146041 题目传送门 题意 求最短路和最短路的路数. 思路: BFS+DFS,先求出最短路.在DFS搜等于最短路的条数. 不加优化SDUTOJ过了,数据就是水. 确定了最短路的长度,加上奇偶剪枝FOJ也过了. #include <queue> #include <cmath> #include <cstdio> #include <cstring>

HDU 4771 (DFS+BFS)

Problem Description Harry Potter has some precious. For example, his invisible robe, his wand and his owl. When Hogwarts school is in holiday, Harry Potter has to go back to uncle Vernon's home. But he can't bring his precious with him. As you know,

HDU 4771 Stealing Harry Potter&#39;s Precious dfs+bfs

Stealing Harry Potter's Precious Problem Description Harry Potter has some precious. For example, his invisible robe, his wand and his owl. When Hogwarts school is in holiday, Harry Potter has to go back to uncle Vernon's home. But he can't bring his

【DFS/BFS】NYOJ-58-最少步数(迷宫最短路径问题)

[题目链接:NYOJ-58] 经典的搜索问题,想必这题用广搜的会比较多,所以我首先使的也是广搜,但其实深搜同样也是可以的. 不考虑剪枝的话,两种方法实践消耗相同,但是深搜相比广搜内存低一点. 我想,因为广搜需要的就是队列,所以相比递归队列更耗内存? 当然DFS并不像上图所说,需要用栈,而是运用递归即可. BFS: 因为BFS是要一个接一个的遍历,所以用到了结构体,来保存坐标和当前所走步数 1.每走一步,通过定义的结构体,从队列中提取a(即上一步的坐标.步数(步数每次累加)) 2.在a的基础上进行

DFS/BFS+思维 HDOJ 5325 Crazy Bobo

题目传送门 1 /* 2 题意:给一个树,节点上有权值,问最多能找出多少个点满足在树上是连通的并且按照权值排序后相邻的点 3 在树上的路径权值都小于这两个点 4 DFS/BFS+思维:按照权值的大小,从小的到大的连有向边,搜索最多连接点数即是答案.因为排序后,他们之间的路径, 5 可定都是从当前节点u连过去的,那么都是小于这两个节点的.DFS需手动加栈,BFS类似拓扑排序的思路 6 */ 7 #pragma comment (linker, "/STACK:1024000000,10240000