bfs,dfs

作者: star_night | 来源:发表于2017-05-25 15:41 被阅读0次
    #include<stdio.h>
    #define M 7
    int map[M][M];
    int book[M][M];
    int dfs(int x, int y, int step)
    {
        int next[4][2]={{0,1},{1,0},{0,-1},{-1,0}};
        int nx,ny,k,t;
        for(k=0; k<4; k++){
            nx=x+next[k][0];
            ny=y+next[k][1];
            //判断是否越界
            if(x>=M || y>=M || x<0 || y<0){
                continue;
            }
            //判断是否可走
            if(map[nx][ny]==0){
                //判断是否到达终点
                if(nx==4 && ny==6){
                    book[x][y]=step;
                    return 1;
                }
                map[nx][ny]=1;//标记已走
                t=dfs(nx,ny,step+1);
                if(t)
                    book[nx][ny]=step;
                return 1;
            }
        }
        return 0;
    }
    int main(void)
    {
        int sx,sy;
        int i,j;
        //读入起始位置
        scanf("%d%d", &sx, &sy);
        //读入地图
        for(i=0; i<M; i++){
            for(j=0; j<M; j++){
                scanf("%d", &map[i][j]);
            }
        }
        book[sy][sx]=1;
        map[sy][sx]=1;
        dfs(sx,sy,2);
        //打印book
        for(i=0; i<M; i++){
            for(j=0; j<M; j++){
                printf("%d ",book[i][j]);
            }
            printf("\n");
        }
        return 0;
    
    }
    
    
    #include<stdio.h>
    #include<string.h>
    typedef struct Node{
      int x;
      int y;
      int f;
      int s;
    }node;
    int map[9][9]=
    {
     1,1,1,1,1,1,1,1,1,
     1,0,0,1,0,0,1,0,1,
     1,0,0,1,1,0,0,0,1,
     1,0,1,0,1,1,0,1,1,
     1,0,0,0,0,1,0,0,1,
     1,1,0,1,0,1,0,0,1,
     1,1,0,1,0,1,0,0,1,
     1,1,0,1,0,0,0,0,1,
     1,1,1,1,1,1,1,1,1
    };
    int book[9][9];
    int next[4][2]={{0,1},{0,-1},{1,0},{-1,0}};
    int head=1;
    int tail=1;
    
    int main()
    {
      node que[81];
      int flag=0;
    
      int n;
      scanf("%d",&n);
      int i,j,k;
      for(i=0; i<n; i++){
        memset(book,0,sizeof(book));
        memset(que,0,sizeof(que));
        int a,b,c,d;
        scanf("%d%d%d%d",&a,&b,&c,&d);
        que[tail].x=a;
        que[tail].y=b;
        que[tail].f=0;
        que[tail].s=0;
        tail++;
        book[a][b]=1;
    
        flag=0;
        int tx,ty;
        while(head<tail){
          for(k=0; k<=3; k++){
            tx=que[head].x+next[k][0];
            ty=que[head].y+next[k][1];
    
            if( tx<0 || tx>8 || ty<0 || ty>8)
              continue;
            if(map[tx][ty]==0 && book[tx][ty]==0){
              book[tx][ty]=1;
              que[tail].x=tx;
              que[tail].y=ty;
              que[tail].f=head;
              que[tail].s=que[head].s+1;
              tail++;
            }
            if(tx==c && ty==d){
              flag=1;
            }
          }
    
          if(flag==1)
            break;
          head++;
        }
        printf("%d\n",que[tail-1].s);
      }
      return 0;
    }
    
    

    相关文章

      网友评论

          本文标题:bfs,dfs

          本文链接:https://www.haomeiwen.com/subject/gjlzxxtx.html