天天看点

UVa 572/POJ 1562/HDU 1241 Oil Deposits(DFS,两种写法)

572 - Oil Deposits

Time limit: 3.000 seconds

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=105&page=show_problem&problem=513

http://poj.org/problem?id=1562

http://acm.hdu.edu.cn/showproblem.php?pid=1241

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 file 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 

UVa 572/POJ 1562/HDU 1241 Oil Deposits(DFS,两种写法)

 and 

UVa 572/POJ 1562/HDU 1241 Oil Deposits(DFS,两种写法)

. 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 

For each grid, output the number of distinct oil deposits. Two different pockets are part of the same oil deposit if they 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
      

Sample Output 

0
1
2
2      

学英语:

Two different pockets are part of the same oil deposit if they are adjacent horizontally, vertically, or diagonally.

两个不同的油囊归属于相同的油田iff它们是水平、垂直或斜对地相邻的。

思路:

简单的dfs模板题。

完整代码:

递归:

/*UVaOJ: 0.015s*/
/*POJ: 16ms,164KB*/
/*HDU: 15ms,348KB*/

#include <cstdio>
#include <cstring>
using namespace std;

int m, n;
bool grid[102][102], vis[102][102];

void dfs(int x, int y)
{
	if (!grid[x][y] || vis[x][y])
		return;
	vis[x][y] = true;
	dfs(x - 1, y - 1);
	dfs(x - 1, y);
	dfs(x - 1, y + 1);
	dfs(x, y - 1);
	dfs(x, y + 1);
	dfs(x + 1, y - 1);
	dfs(x + 1, y);
	dfs(x + 1, y + 1);
}

int main()
{
	char temp;
	while (~scanf("%d%d", &m, &n))
	{
		if (!m)
			break;
		memset(grid, 0, sizeof(grid));
		memset(vis, 0, sizeof(vis));
		//下面这个专门对付蛋疼的poj样例,你懂的。
		while (getchar() == ' ')
			;
		for (int i = 1; i <= m; i++)
		{
			for (int j = 1; j <= n; j++)
			{
				temp = getchar();
				if (temp == '@')
					grid[i][j] = true;
			}
			getchar();
		}
		int count = 0;
		for (int i = 1; i <= m; i++)
			for (int j = 1; j <= n; j++)
			{
				if (grid[i][j] && !vis[i][j])
				{
					count++;
					dfs(i, j);
				}
			}
		printf("%d\n", count);
	}
	return 0;
}
           

非递归:(占内存小点)

/*UVaOJ: 0.015s*/
/*POJ: 0ms,160KB*/
/*HDU: 15ms,276KB*/

#include <cstdio>
#include <cstring>
using namespace std;

struct data
{
	int x, y;
} queue[20000], now, next;

int in, out;
int mov[8][2] = {{ -1, 0}, { -1, -1}, {0, -1}, {1, -1}, {1, 0}, {1, 1}, {0, 1}, { -1, 1}};
char field[102][102];
int m, n;
int ans;
int k;

void bfs(int i, int j)
{
	in = out = 0;
	queue[in].x = i;
	queue[in].y = j;
	field[i][j] = '*';
	in++;
	while (in != out)
	{
		now = queue[out++];
		for (k = 0; k < 8; k++)
		{
			next.x = now.x + mov[k][0];
			next.y = now.y + mov[k][1];
			if (field[next.x][next.y] == '@')
			{
				field[next.x][next.y] = '*';
				queue[in++] = next;
			}
		}
	}
}

int main()
{
	int i, j;
	while (scanf("%d%d", &m, &n), m)//逗号表达式的值是最后一个表达式的值
	{
		ans = 0;
		memset(field, '*', sizeof(field));
		while (getchar() == ' ')
            ;
		for (i = 1; i <= m; i++)
			scanf("%s", field[i] + 1);
		for (i = 1; i <= m; i++)
			for (j = 1; j <= n; j++)
				if (field[i][j] == '@')
				{
					bfs(i, j);
					ans++;
				}
		printf("%d\n", ans);
	}
}
           

Source

Mid-Central USA 1997