UVA 705 Slash Maze

题解:把图放大3倍,这样路就出来了,接着只需要向4个方向遍历即可。 如果遍历时超过边界则说明不能构成回路。

#include <cstdio>
#include <cstring>
char data[80][80], maze[240][240];
int row, column, flag, step, dir[4][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};

bool find_cycle(int x, int y) {
	if (x < 0 || x == 3 * row || y < 0 || y == 3 * column) //如果越界,就说明不能构造回路 则 flag = 0
		return flag = 0;
	
	if (maze[x][y] == 1) //如果已来过,就返回
		return false;

	maze[x][y] = 1; //标记已来过
	step++;

	for (int i = 0; i < 4; i++) //由该位置向 4 个方向遍历迷宫
		find_cycle(x + dir[i][0], y + dir[i][1]);

	return flag;
}

int main () {
	int t = 0;
	while (scanf("%d%d", &column, &row), column) {
		for (int i = 0; i < row && scanf("%s", data[i]); i++);
		int max = 0 , count = 0;
		memset(maze, 0, sizeof(maze));

		for (int i = 0; i < row; i++) //构造迷宫	
			for (int j = 0; j < column; j++)
				if (data[i][j] == '\\')
					maze[i * 3][j * 3] = maze[i * 3 + 1][j * 3 + 1] = maze[i * 3 + 2][j * 3 + 2] = 1;
				else
					maze[i * 3 + 2][j * 3] = maze[i * 3+ 1][j * 3 + 1] = maze[i * 3][j * 3 + 2] = 1;

		for (int i = 0; i < 3 * row; i++) //寻找回路
			for (int j = 0; j < 3 * column; j++) {
				flag = 1, step = 0;
				if (find_cycle(i, j))
					count++, max = (step > max ? step : max);
			}

		printf("Maze #%d:\n", ++t);
		count ? printf("%d Cycles; the longest has length %d.\n\n", count, max/3) : printf("There are no cycles.\n\n"); 	
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

冰阔落

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值