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\n",qu);
	}
	return 0;
}
 
时间: 2024-12-14 17:07:40

Program L 暴力求解的相关文章

POJ 1562(L - 暴力求解、DFS)

油田问题(L - 暴力求解.DFS) 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 numerou

Program C 暴力求解

Description A ring is composed of n (even number) circles as shown in diagram. Put natural numbers into each circle separately, and the sum of numbers in two adjacent circles should be a prime. Note: the number of first circle should always be 1. Inp

Program A - 暴力求解

Description Write a program that finds and displays all pairs of 5-digit numbers that between them use the digits 0 through 9 once each, such that the first number divided by the second is equal to an integer N, where 2<=N<=79. That is, abcde / fghi

Program B 暴力求解

Given a sequence of integers S = {S1,S2,...,Sn}, you should determine what is the value of the maximum positive product involving consecutive terms of S. If you cannot ?nd a positive sequence, you should consider 0 as the value of the maximum product

UVA 152-Tree&#39;s a Crowd(暴力求解三维坐标求最短距离)

Tree's a Crowd Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Description  Tree's a Crowd  Dr William Larch, noted plant psychologist and inventor of the phrase ``Think like a tree--Think Fig'' has invented a new

第四章 分治策略 4.1 最大子数组问题 (暴力求解算法)

/** * 最大子数组的暴力求解算法,复杂度为o(n2) * @param n * @return */ static MaxSubarray findMaxSubarraySlower(int[] n) { long tempSum = 0; int left = 0; int right = 0; long sum = Long.MIN_VALUE; for (int i = 0; i < n.length; i++) { for (int j = i; j < n.length; j++

求解最大子数组问题 -- 暴力求解 和 分治法求解

/*------------------ 求解最大子数组问题 --------------- 最大子数组,就是求解一个数组的所有元素的各种组合中,和最大的 那个子数组.在这种情况下,如果元素值全部非负,那么最大子数组当然 是所有元素.但是如果有负值的元素存在,那么久需要找到一个由数组中 连续几个元素组成的子数组并且其元素值之和最大. */ #include <iostream> using namespace std; struct ArrayStruct { ArrayStruct(int

BZOJ 1054题解 BFS暴力求解

BZOJ 1054题解 BFS暴力求解 1054: [HAOI2008]移动玩具 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 1884  Solved: 1033[Submit][Status][Discuss] Description 在一个4*4的方框内摆放了若干个相同的玩具,某人想将这些玩具重新摆放成为他心中理想的状态,规定移动 时只能将玩具向上下左右四个方向移动,并且移动的位置不能有玩具,请你用最少的移动次数将初始的玩具状态移 动到某人

HDU 1431 素数回文【暴力求解】

/* 题目大意:找一个范围内的所有素数回文数 解题思路:打一个表将1亿以内所有的素数回文数找出来,大概有780个这样子 关键点:暴力求解 解题人:lingnichong 解题时间:2014-08-29 12:02:55 解题体会:如果按一般方法打个素数表,很容易超内存(MLE),所以就先将所有的素数回文全部算出来,再在这个数组里面找在题上那个范围的所有素数回文数 */ 素数回文 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 655