• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录

  • 博客园
  • 联系
  • 订阅
  • 管理

View Post

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;
}
 

posted on 2015-08-02 20:55  tony-cao  阅读(109)  评论(0)    收藏  举报

刷新页面返回顶部
 
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3