zoukankan      html  css  js  c++  java
  • 深度和广度查找

    #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;
    }
  • 相关阅读:
    WordPress在nginx服务器伪静态
    excel根据一列的值匹配另一列
    linux上安装Anaconda并创建python虚拟环境
    python模块学习之numpy
    大数据测试工具
    python学习路线
    spark event log
    夏令时
    spark学习指南
    Yarn和Spark对比
  • 原文地址:https://www.cnblogs.com/GoAhead/p/2685538.html
Copyright © 2011-2022 走看看