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;
    }
     
  • 相关阅读:
    Windows下memcache安装使用
    Linux 下memcache安装及使用
    C语言第五节scanf函数
    C语言第四节数据类型、常量、变量
    C语言第三节关键字、标识符、注释
    C语言第一节 C语言程序与开发工具
    快到而立之年了,可是能撑得起而立吗?
    idea-安装SequenceDiagram插件-生成时序图
    mysql中 查询一对多关系的时候,获取最新的一条
    判断多个时间段区间是否有重叠
  • 原文地址:https://www.cnblogs.com/xl1164191281/p/4696710.html
Copyright © 2011-2022 走看看