首页 > 代码库 > UVa 469 - Wetlands of Florida

UVa 469 - Wetlands of Florida

题目:给你一个矩阵和某些点,找到给的点所处连续的W区域的面积(八个方向)。

分析:搜索。floodfill算法,利用搜索直接求解就可以了。

说明:注意读入数据的格式。

#include <iostream>
#include <cstdlib>
#include <cstdio>

using namespace std;

char maps[111][111];
int  used[111][111];

int dxy[8][2] = {-1,-1,-1,0,-1,1,0,-1,0,1,1,-1,1,0,1,1};
int dfs(int x, int y, int n, int m)
{
	if (x < 0 || x >= n || y < 0 || y >= m) return 0;
	if (maps[x][y] != 'W' || used[x][y]) return 0;
	used[x][y] = 1;
	int sum = 1;
	for (int i = 0 ; i < 8 ; ++ i)
		sum += dfs(x+dxy[i][0], y+dxy[i][1], n, m);
	return sum;
}

int main()
{
	int T,x,y,temp;
	scanf("%d",&T);getchar();
	for (int t = 1 ; t <= T ; ++ t) {
		temp = getchar();
		while (temp != 'W' && temp != 'L')
			temp = getchar();
		int count = 0;
		while (temp == 'W' || temp == 'L' ) {
			ungetc(temp, stdin);
			gets(maps[count ++]);
			temp = getchar();
		}
		int length = strlen(maps[0]);
		while (temp >= '0' && temp <= '9') {
			ungetc(temp, stdin);
			scanf("%d%d",&x,&y);getchar();
			memset(used, 0, sizeof(used));
			printf("%d\n",dfs(x-1, y-1, count, length));
			temp = getchar();
		}
		if (t < T) printf("\n");
	}
	return 0;
}


UVa 469 - Wetlands of Florida