首页 > 其他 > 详细

poj 3009 dfs

时间:2015-03-27 22:17:16      阅读:310      评论:0      收藏:0      [点我收藏+]

背景:dfs,再加点模拟,各种代码疏漏错误wa了三次!!也有变量名使用不规则照成的。比如临时变量我我就应该用temp,buffer,key,三个变量名来表示。

思路:每一个点四个方向的dfs,到达终点就判断最少步数。

bfs的思路:这个是经典的最短路问题,但是缺点是,地图会改变而bfs没办法像dfs那样容易回溯,方法就是把地图直接放在每一个坐标上,也就是定义一个结构体:

struct place{
     int x,y,step;
     int diagram[M][M];//每一个坐标点都付一个图
}

我的代码:

#include<queue>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include <iostream>
#define LL long long int
#define INF 0x3fffffff
#define M 29
using namespace std;
int n,m,diagram[M][M],ans;
struct place{int x,y,step;}s,temp1;

void dfs(place temp){
    #ifdef Ms
    for(int i=1;i <= n;i++){
            for(int j=1;j <= m;j++){
                printf("%d ",diagram[i][j]);
            }
            printf("\n");
    }
    printf("\n");
    #endif // LOCAL
     if(temp.step+1 > 10 || temp.step+1 > ans) return;
     for(int i=temp.x-1;i > 0;i--){//向上走。
         if(diagram[i][temp.y] == 3 && diagram[temp.x-1][temp.y] != 1){if(temp.step+1 < ans) ans=temp.step+1;return;}
         if(diagram[i][temp.y] == 1 && diagram[temp.x-1][temp.y] != 1){
            temp1.x=i+1;
            temp1.y=temp.y;
            temp1.step=temp.step+1;
            diagram[i][temp.y]=0;
            dfs(temp1);
            diagram[i][temp.y]=1;
            break;
         }
     }
     for(int i=temp.x+1;i <= n;i++){//向下走。
         if(diagram[i][temp.y] == 3 && diagram[temp.x+1][temp.y] != 1){if(temp.step+1 < ans) ans=temp.step+1;return;}
         if(diagram[i][temp.y] == 1 && diagram[temp.x+1][temp.y] != 1){
            temp1.x=i-1;
            temp1.y=temp.y;
            temp1.step=temp.step+1;
            diagram[i][temp.y]=0;
            dfs(temp1);
            diagram[i][temp.y]=1;
            break;
         }
     }
    for(int i=temp.y+1;i <= m;i++){//向右走。
        if(diagram[temp.x][i] == 3 && diagram[temp.x][temp.y+1] != 1){if(temp.step+1 < ans) ans=temp.step+1;return;}
         if(diagram[temp.x][i] == 1 && diagram[temp.x][temp.y+1] != 1){
            temp1.x=temp.x;
            temp1.y=i-1;
            temp1.step=temp.step+1;
            diagram[temp.x][i]=0;
            dfs(temp1);
            diagram[temp.x][i]=1;
            break;
         }
     }
     for(int i=temp.y-1;i > 0;i--){//向左走。
         if(diagram[temp.x][i] == 3 && diagram[temp.x][temp.y-1] != 1){if(temp.step+1 < ans) ans=temp.step+1;return;}
         if(diagram[temp.x][i] == 1 && diagram[temp.x][temp.y-1] != 1){
            temp1.x=temp.x;
            temp1.y=i+1;
            temp1.step=temp.step+1;
            diagram[temp.x][i]=0;
            dfs(temp1);
            diagram[temp.x][i]=1;
            break;
         }
     }
}

int main(void){
    while(scanf("%d%d",&m,&n),m*m+n*n){
        memset(diagram,0,sizeof(diagram));
        for(int i=1;i <= n;i++)
            for(int j=1;j <= m;j++){
                scanf("%d",&diagram[i][j]);
                if(diagram[i][j] == 2){
                    s.x=i;
                    s.y=j;
                    diagram[i][j]=0;
                }
            }
        ans=INF;
        s.step=0;
        dfs(s);
        if(ans == INF) printf("-1\n");
        else printf("%d\n",ans);
    }
    return 0;
}



poj 3009 dfs

原文:http://blog.csdn.net/jibancanyang/article/details/44682057

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!