uva705 Slash Maze

Description

Download as PDF

By filling a rectangle with slashes (/) and backslashes ( $\backslash$), you can generate nice little mazes. Here is an example:

As you can see, paths in the maze cannot branch, so the whole maze only contains cyclic paths and paths entering somewhere and leaving somewhere else. We are only interested in the cycles. In our example, there are two of them.

Your task is to write a program that counts the cycles and finds the length of the longest one. The length is defined as the number of small squares the cycle consists of (the ones bordered by gray lines in the picture). In this example, the long cycle has length 16 and the short one length 4.

Input 

The input contains several maze descriptions. Each description begins with one line containing two integers w and h ( $1 \le w, h \le 75$), the width and the height of the maze. The next h lines represent the maze itself, and contain w characters each; all these characters will be either ``/" or ``\".

The input is terminated by a test case beginning with w = h = 0. This case should not be processed.

Output 

For each maze, first output the line ``Maze #n:'', where n is the number of the maze. Then, output the line ``kCycles; the longest has length l.'', where k is the number of cycles in the maze and l the length of the longest of the cycles. If the maze does not contain any cycles, output the line ``There are no cycles.".

Output a blank line after each test case.

Sample Input 

6 4
\//\\/
\///\/
//\\/\
\/\///
3 3
///
\//
\\\
0 0

Sample Output 

Maze #1:
2 Cycles; the longest has length 16.

Maze #2:
There are no cycles.


斜着的迷宮

dfs題

一開始想着長度不一

確實無從下手

看了網上的題解

放大,真的是不錯的想法

八個方向

斜着的還要討論情況

最後cnt也要討論

長度超過4

才能形成環



#include <stdio.h>
#include <string.h>

char sen[100], s[200][200];
int last_x, last_y, map[200][200], cnt;
bool is_ok(int i, int j, int x, int y){
	if (x >= i - 1 && x <= i + 1 && y <= j + 1 && y >= j - 1)
		return true;
	return false;
}

void dfs(int x, int y){
	if (map[x][y] == 0)
		return ;
	cnt++;
	map[x][y] = 0;
	last_x = x;
	last_y = y;
	dfs(x - 1, y);
	dfs(x, y - 1);
	dfs(x + 1, y);
	dfs(x, y + 1);
	if ((map[x - 1][y] || map[x][y + 1]) || (s[x - 1][y] == '/' && s[x][y + 1] == '/'))
		dfs(x - 1, y + 1);
	if (((map[x][y - 1] || map[x + 1][y])) || (s[x][y - 1] == '/' && s[x + 1][y] == '/'))
		dfs(x + 1, y - 1);
	if ((map[x][y + 1] || map[x + 1][y]) || (s[x][y + 1] == '\\' && s[x + 1][y] == '\\'))
		dfs(x + 1, y + 1);
	if ((map[x - 1][y] || map[x][y - 1]) || (s[x - 1][y] == '\\' && s[x][y - 1] == '\\'))
		dfs(x - 1, y - 1);
}

int main(){
	int w, h, cas = 1;
	while (scanf("%d%d", &w, &h) && w && h) {
		memset(map, 0, sizeof(map));
		memset(s, ' ', sizeof(s));
		getchar();
		for (int i = 1; i <= h; i++) {
			gets(sen);
			for (int j = 0; j < w; j++) {
				if (sen[j] == '/') {
					map[2 * i - 1][2 * (j + 1) - 1] = 1;
					map[2 * i][2 * (j + 1)] = 1;
					s[2 * i - 1][2 * (j + 1)] = '/';
					s[2 * i][2 * (j + 1) - 1] = '/';
				}
				if (sen[j] == '\\') {
					map[2 * i - 1][2 * (j + 1)] = 1;
					map[2 * i][2 * (j + 1) - 1] = 1;
					s[2 * i - 1][2 * (j + 1) - 1] = '\\';
					s[2 * i][2 * (j + 1)] = '\\';
				}
			}
		}
		int cycle = 0, max = 0;
		for (int i = 1; i <= 2 * h; i++) 
			for (int j = 1; j <= 2 * w; j++) {
				if (map[i][j] == 1) {
					cnt = 0;
					dfs(i, j);
					if (is_ok(i, j, last_x, last_y) && cnt >= 4) {
						cycle++;
						if (cnt > max)
							max = cnt;
					}
				}
			}
		printf("Maze #%d:\n", cas++);
		if (cycle)
			printf("%d Cycles; the longest has length %d.\n\n", cycle, max);
		else
			printf("There are no cycles.\n\n");
	}
	return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章