题意:由'\' 和'/'组成的图,问是否有环的存在,如果有输出最大的环所占的格子数量。

题解:刚开始看没思路,在网上见到有人把斜线放大三倍,然后用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;
}