您的位置:首页 > 其它

uva 705 Slash Maze(几何转换+DFS)

2013-08-01 20:49 525 查看


 Slash Maze 
By filling a rectangle with slashes (/) and backslashes ( 

), 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 (

),
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.

题目大意:给出一个由斜线组成的迷宫,判断它有多少个环状,并且找出最大的环的步数。
解题思路:刚开始式一点思路也没有,后来看到学长的解题报告就顿悟了,给个连接

http://blog.csdn.net/shuangde800/article/details/7726620

#include<stdio.h>
#include<string.h>
#define N 250
#define M 3
int r, c, bo, sum;
int map

, vis

;
const int dir[4][2] = {{1, 0}, {-1, 0}, {0 , 1}, {0, -1}};

int build(){
char ch;
for (int i = 0; i < r; i++){
for (int j = 0; j < c; j++){
scanf("%c", &ch);
if (ch == '\\'){
for (int x = 0; x < M; x++)
for (int y = 0; y < M; y++){
if (x == y)
map[i * M + x][j * M + y] = 1;
else
map[i * M + x][j * M + y] = 0;
}
}
else{
for (int x = 0; x < M; x++)
for (int y = 0; y < M; y++){
if (x + y == M - 1)
map[i * M + x][j * M + y] = 1;
else
map[i * M + x][j * M + y] = 0;
}
}
}
getchar();
}
}

void DFS(int a, int b){
vis[a][b] = 1;
sum++;
int p, q;
for (int i = 0; i < 4; i++){
p = a + dir[i][0];
q = b + dir[i][1];
if (p < 0 || p >= r)	continue;
if (q < 0 || q >= c)	continue;
if (map[p][q] || vis[p][q])	continue;
if (p == 0 || p == r - 1 || q == 0 || q == c - 1)
bo = 1;
DFS(p, q);
}
}
int main(){
int cnt, max, t = 1;
while (scanf("%d%d%*c", &c, &r), c || r){
// Init.
memset(map, 0, sizeof(map));
memset(vis, 0, sizeof(vis));
cnt = max = 0;

// Build_Map.
build();
r *= 3;
c *= 3;

// DFS.
for (int i = 1; i < r - 1; i++)
for (int j = 1; j < c - 1; j++){
if (vis[i][j] || map[i][j])
continue;
sum = bo = 0;
DFS(i, j);
if (!bo){
cnt++;
if (max < sum)
max = sum;
}
}

// Prinf.
printf("Maze #%d:\n", t++);
if (cnt)
printf("%d Cycles; the longest has length %d.\n\n", cnt, max / M);
else
printf("There are no cycles.\n\n");
}
return 0;}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: