poj 3009 dfs

背景: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;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值