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;
    }
     
  • 相关阅读:
    关于回溯与招聘市场
    关于回溯与马
    关于回溯和后宫
    关于兔子
    关于递归和斐波那契数列
    关于递归和汉诺塔
    关于简单汉诺塔
    nodejs报错roll back,because of a error.node.js setup wizard ended prematurel
    fatal error C1859 意外的预编译头错误,只需重新运行编译器
    sqlserver2008 无法设置主体sa的凭据
  • 原文地址:https://www.cnblogs.com/xl1164191281/p/4696710.html
Copyright © 2011-2022 走看看