zoukankan      html  css  js  c++  java
  • 八连块问题

    问题描述:共有多少块相邻的空白的区域

    注意递归和边界问题的值的设置

    源代码如下:

    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    #define MAX 100 +10
    using namespace std;
    
    int mat[MAX][MAX] ,vis[MAX][MAX];
    
    void dfs(int x,int y)
    {
        if(!mat[x][y] || vis[x][y])   //1代表空白
            return;
        vis[x][y]=1;
        dfs(x-1,y-1);
        dfs(x-1,y);
        dfs(x-1,y+1);
        dfs(x,y-1);
        dfs(x,y+1);
        dfs(x+1,y-1);
        dfs(x+1,y);
        dfs(x+1,y+1);
    }
    void main()
    {
        memset(mat,0,sizeof(mat));
        memset(vis,0,sizeof(mat));
        int n,i,j;
        scanf("%d",&n);
        for(i=0;i<n;i++)
            for(j=0;j<n;j++)
                scanf("%d",&mat[i+1][j+1]);
        int count=0;
        for(i=1;i<=n;i++)
            for(j=1;j<=n;j++)
            {
                if(mat[i][j] && !vis[i][j])
                {count++;dfs(i,j);}
            }
            printf("%d
    ",count);
    
    }
    选择了远方,便只顾风雨兼程
  • 相关阅读:
    C#面向对象
    C#语句
    C#语言数据类型
    Jupyter Notebook(iPython)
    BeautifulSoup模块
    requests模块
    爬虫基本原理
    版本控制系统
    支付宝支付
    django内置组件——ContentTypes
  • 原文地址:https://www.cnblogs.com/ly-rabbit-wust/p/5575200.html
Copyright © 2011-2022 走看看