• 深度和广度查找


    #include <iostream.h>
    #include <stdlib.h>
    #include <string.h>
    #include <time.h>
    #include <math.h>
    void fun(int (*a)[5],int i,int j,int m)
    {
        if(i<0||j<0||i>=m||j>=m||a[i][j]!=1) return;
        a[i][j]=2;
        fun(a,i-1,j,m);
        fun(a,i+1,j,m);
        fun(a,i,j-1,m);
        fun(a,i,j+1,m);
    }
    typedef struct Point{
        int x;
        int y;
    }Point;
    typedef struct Data{
        Point a[100];
        int length;
    }Data;
    void EnQueue(Data& data,int i,int j,int (*array)[5],int m)
    {
        if(i<0||j<0||i>=m||j>=m||array[i][j]!=1) return;
        Point temp;
        temp.x=i;
        temp.y=j;
        data.a[data.length]=temp;
        data.length++;
        array[i][j]=0;
    }
    Point DeQueue(Data& data)
    {
        Point temp;
        temp=data.a[0];
        for (int i=0;i<data.length-1;i++)
        {
            data.a[i]=data.a[i+1];
        }
        data.length--;
        return temp;
    }
    void deep(int (*array)[5],int i,int j,int m)
    {
        Data data;
        data.length=0;
        EnQueue(data,i,j,array,m);
        while (data.length)
        {
            Point temp=DeQueue(data);
            EnQueue(data,temp.x-1,temp.y,array,m);
            EnQueue(data,temp.x+1,temp.y,array,m);
            EnQueue(data,temp.x,temp.y-1,array,m);
            EnQueue(data,temp.x,temp.y+1,array,m);
        }
    
    }
    void main()
    {
        srand((unsigned)time(NULL));
        int map[5][5];
        for (int i=0;i<5;i++)
        {
            for (int j=0;j<5;j++)
            {
                map[i][j]=rand()%2;
                cout<<map[i][j]<<"  ";
            }
            cout<<endl;
        }
        int many=0;
        /*for (i=0;i<10;i++)
        {
            for (int j=0;j<10;j++)
            {
                if(map[i][j]==1)
                { 
                    fun(map,i,j,10);many++;
                    cout<<endl;
                    for (int ii=0;ii<10;ii++)
                    {
                        for (int jj=0;jj<10;jj++)
                        {
                            
                            cout<<map[ii][jj]<<"  ";
                        }
                        cout<<endl;
                    }
                }
                
            }
            
        }*/
        for (i=0;i<5;i++)
        {
            for (int j=0;j<5;j++)
            {
                if(map[i][j]==1)
                { 
                    deep(map,i,j,5);many++;
                    cout<<endl;
                    for (int ii=0;ii<5;ii++)
                    {
                        for (int jj=0;jj<5;jj++)
                        {
                            cout<<map[ii][jj]<<"  ";
                        }
                        cout<<endl;
                    }
                }
                
            }
            
        }
        
        cout<<many<<endl;
    }
  • 相关阅读:
    SpringMVC 返回 json 字符串中文乱码
    两个HTML页面之间传值
    NOIP2018 保卫王国
    Arch Linux使用cisco anyconnect
    CSP模拟赛 巨神兵
    计蒜客The Fake Fake Friends
    Manjaro安装,配置,美化指南
    [国家集训队] Crash 的文明世界
    ZJOI2011 营救皮卡丘
    CF1198E Rectangle Painting 2
  • 原文地址:https://www.cnblogs.com/GoAhead/p/2685538.html
Copyright © 2020-2023  润新知