poj 3083 dfs+bfs

背景:竟然G++,wa了一发,同样的代码,改为C++就过。。。后来看了discuss里面人说bfs最后虽然不会用到有return 的情况,也要加上,这个warning警告了的,没想到加上就ac了。。。

思路:bfs求最短路,然后就是对一直向左和一直向右进行dfs,方法是:记录上一次来的方向,然后根据上一次来的方向确定当前方向怎样才是向左,怎样才是向右,向左的话是顺时针转动,向右的话是逆时针转动。

我的代码:

#include <set>
#include <stack>
#include <queue>
#include <vector>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#define LL long long int
using namespace std;
const int M=42,INF=0x3fffffff;
int n,m,dir[4][2]={0,-1,-1,0,0,1,1,0},ans;
bool diagram[M][M],vis[M][M];
struct place{int x,y,last_dir,step;}s,e,temp1,temp2;

int bfs(void){
    queue<place> q1;
    s.step=0;
    q1.push(s);
    while(!q1.empty()){
        temp1=q1.front();
        q1.pop();
        if(temp1.x == e.x && temp1.y == e.y) return temp1.step;
        for(int j=0,i=temp1.last_dir-1;j < 4;j++,i++){
            if(i == -1) i=3;
            if(i == 4) i=0;
            temp2.x=temp1.x+dir[i][0];
            temp2.y=temp1.y+dir[i][1];
            if(temp2.x > 0 && temp1.x > 0 && temp1.x <= n && temp1.x <= m && !vis[temp2.x][temp2.y] && diagram[temp2.x][temp2.y]){
                temp2.last_dir=i;
                temp2.step=temp1.step+1;
                vis[temp2.x][temp2.y]=1;
                q1.push(temp2);
            }
        }
    }
    return 0;
}

void dfs(place pla,int d){
    if(ans >= 0) return;
    if(pla.x == e.x && pla.y == e.y){ans=pla.step;return;}
    int i;
    if(d == 0) i=pla.last_dir-1;
    else i=pla.last_dir+1;
    for(int j=0;j < 4;j++){
        if(i == -1) i=3;
        if(i == 4) i=0;
        if(pla.x+dir[i][0] > 0 && pla.y+dir[i][1] <= m && pla.x+dir[i][0] <= n && pla.y+dir[i][1] > 0 && diagram[pla.x+dir[i][0]][pla.y+dir[i][1]]){
            pla.step++;
            pla.last_dir=i;
            pla.x+=dir[i][0];
            pla.y+=dir[i][1];
            dfs(pla,d);
        }
        if(!d) i++;
        else i--;
    }
}

int main(void){
    int t;
    scanf("%d",&t);
    while(t--){
        scanf("%d%d",&m,&n);
        getchar();
        for(int i=1;i <= n;i++){
            for(int j=1;j <= m;j++){
                char c;
                scanf("%c",&c);
                c == '#' ? diagram[i][j]=0 : diagram[i][j]=1;
                if(c == 'S'){
                    s.x=i,s.y=j;
                    if(j == 1) s.last_dir=2;
                    else if(j == m) s.last_dir=0;
                    else if(i == 1) s.last_dir=3;
                    else if(i == n) s.last_dir=1;
                }
                if(c == 'E') e.x=i,e.y=j;
            }
            getchar();
        }
        memset(vis,0,sizeof(vis));
        int min_step=bfs()+1;
        //cout << min_step << endl;
        ans=-1;
        dfs(s,0);//0代表一直左转
        printf("%d ",ans+1);
        ans=-1;
        dfs(s,1);
        printf("%d %d\n",ans+1,min_step);
    }
    return 0;
}
时间: 2024-08-13 06:58:44

poj 3083 dfs+bfs的相关文章

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

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

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

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 1724ROADS(bfs和dfs做法)

1 /* 2 dfs比较好想,就是测试数据的问题,导致在遍历边的时候要倒着遍历才过! 3 */ 4 #include<iostream> 5 #include<cstdio> 6 #include<cstring> 7 #include<vector> 8 #include<algorithm> 9 #define Max 0x3f3f3f3f 10 using namespace std; 11 12 struct node{ 13 int D

【POJ 3083】Children of the Candy Corn

POJ[3083]Children of the Candy Corn Dfs+Bfs 分别求沿左墙到达E 沿右墙到达E 还有S到E的最短步数 前两个Dfs实现 最后一个Bfs 耐心写很容易A 主要注意方向问题 dir四个方向 上右下左 刚开始我分别用下标0 1 2 3代表 开dirx diry两个移动数组 假设前一状态朝向0(上) 沿左墙移动即为3 0 1 2(左上右下<顺时针>) 沿右墙即为1 0 3 2(右上左下<逆时针>) 同理其余方向很容易遍历 略自豪的是不断精简代码从6

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>