UVA705 UVALive5641 Slash Maze【DFS】

By filling a rectangle with slashes (/) and backslashes (), you can generate nice little mazes. The picture on the right 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 ≤ w, h ≤ 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 ‘k Cycles; 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.

Regionals 1999 >> Europe - Mid-Central
Regionals 1999 >> Europe - Southwestern

问题链接UVA705 UVALive5641 Slash Maze
问题简述:(略)
问题分析
    用字符“/”和“"构成一个邪线迷宫,计算有多少封闭循环,最长循环的长度是多少?
    一个经典邪线迷宫题。可以将迷宫放大2倍或3倍再进行计算,不放大的话计算逻辑估计会过于困难。
    放大2倍 :
''变为:
\*
*\
'/'变为:
*/
/*
搜索时需要考虑8个方向,上下左右则按正常搜索即可,邪线方向则需要考虑条件,如果遇到墙(邪线)就不逾越(继续)搜索。详细处理见代码。
    放大3倍:
'\'变为:
\**
*\*
**\
'/'变为:
**/
*/*
/**
搜索时需要考上下左右,算出结果后需要除以3,因为被放大了3倍。
程序说明:(略)
参考链接:(略)
题记:(略)

AC的C++语言程序(放大3倍)如下:

/* UVA705 UVALive5641 Slash Maze */

#include <bits/stdc++.h>

using namespace std;

const int K = 3;
const int N = 75;
char maze[N * K][N * K], s[N + 1];
bool flag;
int caseno = 0, h, w, sum;

void dfs(int row, int col)
{
    if(row >= 0 && row < h * K && col >= 0 && col < w * K) {
        if(maze[row][col] == 0) {
            maze[row][col] = 1;
            sum++;
            dfs(row - 1, col);
            dfs(row, col + 1);
            dfs(row + 1, col);
            dfs(row, col - 1);
        }
    } else
        flag = false;
}

int main()
{
    while(~scanf("%d%d", &w, &h) && (w || h)) {
        memset(maze, 0, sizeof(maze));

        for(int i = 0; i < h; i++) {
            scanf("%s", s);
            for(int j = 0; j < w; j++)
                if(s[j] == '\\')
                    maze[i*K][j*K] = maze[i*K + 1][j*K + 1] = maze[i*K + 2][j*K + 2] = 1;
                else
                    maze[i*K][j*K + 2] = maze[i*K + 1][j* K + 1] = maze[i*K + 2][j*K] = 1;
        }

        int cnt = 0, maxsum = 0;
        for(int i = 0; i < h * K; i++)
            for(int j = 0; j < w * K; j++)
                if(maze[i][j] == 0) {
                    flag = true;
                    sum = 0;
                    dfs(i, j);
                    if(flag) {
                        cnt++;
                        maxsum = max(maxsum, sum);
                    }
                }

        if(cnt == 0)
            printf("Maze #%d:\nThere are no cycles.\n\n", ++caseno);
        else
            printf("Maze #%d:\n%d Cycles; the longest has length %d.\n\n", ++caseno, cnt, maxsum / K);
    }

    return 0;
}

AC的C++语言程序(放大2倍)如下:

/* UVA705 UVALive5641 Slash Maze */

#include <bits/stdc++.h>

using namespace std;

const char SPACE = ' ';
const int K = 2;
const int N = 75;
char maze[N * K][N * K], s[N + 1];
bool flag;
int caseno = 0, h, w, sum;

void dfs(int row, int col)
{
    if(row >= 0 && row < h * K && col >= 0 && col < w * K) {
        if(maze[row][col] == SPACE) {
            maze[row][col] = '#';
            sum++;
            dfs(row - 1, col);
            dfs(row, col + 1);
            dfs(row + 1, col);
            dfs(row, col - 1);

            if(row > 0 && col > 0 && maze[row][col - 1] != '/' && maze[row - 1][col] != '/' )
                dfs(row - 1, col - 1);
            if(row > 0 && col < w * K && maze[row - 1][col] != '\\' && maze[row][col + 1] != '\\' )
                dfs(row - 1, col + 1);
            if(row < h * K && col > 0 && maze[row][col - 1] != '\\' && maze[row + 1][col] != '\\' )
                dfs(row + 1, col - 1);
            if(row < h * K && col < w * K && maze[row][col + 1] != '/' && maze[row + 1][col] != '/' )
                dfs(row + 1, col + 1);
        }
    } else
        flag = false;
}

int main()
{
    while(~scanf("%d%d", &w, &h) && (w || h)) {
        memset(maze, SPACE, sizeof(maze));

        for(int i = 0; i < h; i++) {
            scanf("%s", s);
            for(int j = 0; j < w; j++)
                if(s[j] == '\\')
                    maze[i*K][j*K] = maze[i*K + 1][j*K + 1] = '\\';
                else
                    maze[i*K][j*K + 1] = maze[i*K + 1][j* K] = '/';
        }

        int cnt = 0, maxsum = 0;
        for(int i = 0; i < h * K; i++)
            for(int j = 0; j < w * K; j++)
                if(maze[i][j] == SPACE) {
                    flag = true;
                    sum = 0;
                    dfs(i, j);
                    if(flag) {
                        cnt++;
                        maxsum = max(maxsum, sum);
                    }
                }

        if(cnt == 0)
            printf("Maze #%d:\nThere are no cycles.\n\n", ++caseno);
        else
            printf("Maze #%d:\n%d Cycles; the longest has length %d.\n\n", ++caseno, cnt, maxsum);
    }

    return 0;
}

posted on 2019-03-14 07:56  新海岛Blog  阅读(119)  评论(0编辑  收藏  举报

导航

// ... runAll: function() { this.resetPreCode(); hljs.initHighlightingOnLoad(); // 重新渲染,添加语法高亮 hljs.initLineNumbersOnLoad(); // 为代码加上行号 } // ...