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;
    }
     
  • 相关阅读:
    Gmail总是把MS发来的信作为垃圾邮件
    添加了CSpreadSheet.h后出现LNK2005错误
    弃用Eudora
    卸载Intel C++ Compiler后Visual C++ 6.0不能工作了
    Matlab对多CPU系统的支持
    Borland决定出售Delphi、JBuilder、C++Builder等IDE产品
    微软提供的免费软件
    生命在于运动?
    "Cannot modify header information"的解决方法
    慢慢的,我们长成了受困于数字的大人
  • 原文地址:https://www.cnblogs.com/xl1164191281/p/4696710.html
Copyright © 2011-2022 走看看