UVA 657 The die is cast

题意: 先寻找到色子的区域,再在色子的区域里面寻找点数

#include <cstdio>
char data[55][55];
int row, column, count = 0, dir[8][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};

bool find_dot(int x, int y) {
	if (data[x][y] != 'X' || x < 0 || x == row || y < 0 || y == column) //非点数、越界、已来过就跳过
		return false;
	data[x][y] = '*'; //标记点数此处已来过

	for (int i = 0; i < 4; i++) //在此点数的范围内,四个方向遍历点数
		find_dot(x + dir[i][0], y + dir[i][1]);
	return true;
}

bool find_dice(int x, int y) {
	if (data[x][y] == '.' || x < 0 || x == row || y < 0 || y == column) //非色子、越界、已来过就跳过
		return false;
	if (data[x][y] == '*') //标记色子的的此处来过
		data[x][y] = '.';

	if (find_dot(x, y)) //寻找色子里面的点数
		count++;
	for (int i = 0; i < 4; i++) //在此色子的范围内, 四个方向遍历色子
		find_dice(x + dir[i][0], y + dir[i][1]);
	return true;
}

int main() {
	int t = 0;
	while (scanf("%d%d", &column, &row), row) {
		for (int i = 0; i < row && scanf("%s", data[i]); i++);
		int num[7] = {0};
		for (int i = 0; i < row; i++)
			for (int j = 0; j < column; j++)
				if (find_dice(i, j)) //如果寻找到了色子. 这个点数出现的次数就加一
					num[count]++, count = 0; 

		printf("Throw %d\n", ++t);
		for (int i = 1; i < 7; i++)
			while (num[i]--)
				printf(num[0]++ ? " %d" : "%d", i); //每行第一个数字前面输出空格
		printf("\n\n");
	}
	return 0;
}


你可能感兴趣的:(UVA 657 The die is cast)