题解:这道题开始想了很久都没思路,然后在在网上看到别人的做法,将 一个 / 或 \ 放大两倍或三倍的数组来存并用0、1填充,有斜杠的是1,没有是0,即没路和有路,然后用dfs做,但还是不理解为什么是在深搜过程中以判断越界情况来判断是否存在一个圈,所以根据斜杠图去想,因为如果到了数组边缘是无法构成圈的。剩下的就好办了。


#include <stdio.h>
#include <string.h>
const int N = 250;

int row, col, pos[N][N], flag, count, max, step;
int flag1[4] = {0, 0, 1, -1};
int flag2[4] = {1, -1, 0, 0};

void init() {
	memset(pos, 0, sizeof(pos));
	count = flag = step = 0;
	max = -1;
}

void dfs(int x, int y) {
	pos[x][y] = 1;
	int x0, y0;
	for (int i = 0; i < 4; i++) {
		x0 = x + flag1[i];
		y0 = y + flag2[i];
		if (x0 < 0 || x0 >= row || y0 < 0 || y0 >= col) {
			flag = 0;
			continue;
		}
		else
			if (pos[x0][y0] == 0) {
				step++;
				dfs(x0, y0);
			}
	}
}

int main() {
	char c;
	int t = 1;
	while (scanf("%d%d", &col, &row) && (col || row)) {
		getchar();
		init();
		int m = 0, n = 0;
		for (int i = 0; i < row; i++, m += 3) {
			for (int j = 0, n = 0; j < col + 1; j++, n += 3) {
				scanf("%c", &c);
				if (c == '\\') {
					pos[m][n] = 1;
					pos[m + 1][n] = 0;
					pos[m + 2][n] = 0;
					pos[m][n + 1] = 0;
					pos[m + 1][n + 1] = 1;
					pos[m + 1][n + 2] = 0;
					pos[m][n + 2] = 0;
					pos[m + 2][n + 1] = 0;
					pos[m + 2][n + 2] = 1;
				}
				else if (c == '/') {
					pos[m][n] = 0;
					pos[m][n + 1] = 0;
					pos[m][n + 2] = 1;
					pos[m + 1][n] = 0;
					pos[m + 2][n] = 1;
					pos[m][n + 1] = 0;
					pos[m + 1][n + 1] = 1;
					pos[m + 2][n + 2] = 0;
					pos[m + 1][n + 2] = 0;
				}
			}
		}
		row = 3 * row;
		col = 3 * col;
		for (int i = 0; i < row; i++)
			for (int j = 0; j < col; j++)
				if (pos[i][j] == 0) {
					flag = 1;
					step = 1;
					dfs(i, j);
					if (flag == 1) {
						count++;
						if (step > max)
							max = step;
					}
				}
		printf("Maze #%d:\n", t++);
		if (count == 0)
			printf("There are no cycles.\n\n");
		else
			printf("%d Cycles; the longest has length %d.\n\n", count, max / 3);
	}
	return 0;
}