zoukankan      html  css  js  c++  java
  • Program L 暴力求解

    Description

    The GeoSurvComp geologic survey company is responsible for detecting underground oil deposits. GeoSurvComp works with one large rectangular region of land at a time, and creates a grid that divides the land into numerous square plots. It then analyzes each plot separately, using sensing equipment to determine whether or not the plot contains oil. A plot containing oil is called a pocket. If two pockets are adjacent, then they are part of the same oil deposit. Oil deposits can be quite large and may contain numerous pockets. Your job is to determine how many different oil deposits are contained in a grid.

    Input

    The input contains one or more grids. Each grid begins with a line containing m and n, the number of rows and columns in the grid, separated by a single space. If m = 0 it signals the end of the input; otherwise 1 <= m <= 100 and 1 <= n <= 100. Following this are m lines of n characters each (not counting the end-of-line characters). Each character corresponds to one plot, and is either `*', representing the absence of oil, or `@', representing an oil pocket.

    Output

    are adjacent horizontally, vertically, or diagonally. An oil deposit will not contain more than 100 pockets.

    Sample Input

    1 1
    *
    3 5
    *@*@*
    **@**
    *@*@*
    1 8
    @@****@*
    5 5 
    ****@
    *@@*@
    *@**@
    @@@*@
    @@**@
    0 0

    题目大意:给出一个行列式,以‘@’为目标看它有几个连通块,输出其结果。
    思路:连通块问题其实就是寻找可行解,将行列式全部

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    const int maxn=100+5;
    char s[maxn][maxn];
    int bi_ji[maxn][maxn];
    int m,n;
    using namespace std;
    void dfs(int r,int c,int d)
    {
    	if(r<0||r>=m||c<0||c>=n)
    		return;
    	if(bi_ji[r][c]>0||s[r][c]!='@')
    		return;
    	bi_ji[r][c]=d;
    	for(int dr=-1;dr<=1;dr++)
    		for(int dc=-1;dc<=1;dc++)
    			if(dr!=0||dc!=0)
    				dfs(r+dr,c+dc,d);
    }
    int main()
    {
    	int qu;
    	while(scanf("%d%d",&m,&n)==2&&m&&n)
    	{
    		memset(bi_ji,0,sizeof(bi_ji));
    		qu=0;
    		for(int i=0;i<m;i++)	
    			scanf("%s",s[i]);
    		for(int i=0;i<m;i++)
    			for(int j=0;j<n;j++)
    				if(!bi_ji[i][j]&&s[i][j]=='@')
    					dfs(i,j,++qu);
    		printf("%d
    ",qu);
    	}
    	return 0;
    }
     
  • 相关阅读:
    win server 2012 服务器不能ping通
    Linux系统的文件目录结构
    怎样理解和识别 Linux 中的文件类型
    Linux(Centos 7)下安装Git并配置连接GitHub
    centos 7 下升级自带 sqlite3
    Pycharm项目上传到Github
    计算机存储单位:bit, Byte, KB, MB, GB, TB, PB, EB, ZB, BB
    ubuntu 删除命令
    influxDB 各个历史版本号,时间截止2020年9月27日
    使用Github做一个完全免费的个人网站
  • 原文地址:https://www.cnblogs.com/xl1164191281/p/4696710.html
Copyright © 2011-2022 走看看