zoukankan      html  css  js  c++  java
  • uva 705

      Slash Maze 

    By filling a rectangle with slashes (/) and backslashes ( $ackslash$), 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.
    
    #include <iostream>
    #include <stack>
    #include <cstring>
    #include <cstdio>
    #include <string>
    #include <algorithm>
    #include <queue>
    #include <set>
    #include <map>
    #include <fstream>
    #include <stack>
    #include <list>
    #include <sstream>
    #include <cmath>
    
    using namespace std;
    /*
    将一个格转换成四小格,在上下左右,对角线搜索,对角线需加特判
    */
    #define ms(arr, val) memset(arr, val, sizeof(arr))
    #define mc(dest, src) memcpy(dest, src, sizeof(src))
    #define N 80
    #define M 160
    #define INF 0x3fffffff
    #define vint vector<int>
    #define setint set<int>
    #define mint map<int, int>
    #define lint list<int>
    #define sch stack<char>
    #define qch queue<char>
    #define sint stack<int>
    #define qint queue<int>
    
    int ans[2500], p;
    
    char rect[N][N], maze[M][M];
    bool tag[M][M];
    int w, h, cycles, _max, ri, rj;
    int dir[8][2] = {-1, 0, 0, -1, 0, 1, 1, 0, -1, -1, 1, -1, -1, 1, 1, 1};
    bool judge(int i, int j, int k)
    {
        int ii = dir[k][0] + i, jj = dir[k][1] + j;
        if (k == 0 || k == 3)
        {
            return maze[ii][jj] == '\';
        }
        return maze[ii][jj] == '/';
        //if (k == 4)//左上角-1 -1(0, -1; -1, 0)
        //{
        //
        //}
        //if (k == 5)//左下角1 -1(0, -1; 1, 0)
        //{
        //}
        //if (k == 6)//右上角-1 1(-1, 0; 0, 1)
        //{
        //}
        //if (k == 7)//右下角1 1(0, 1; 1, 0)
        //{
        //}
    }
    void dfs(int i, int j, int len)
    {
        if (maze[i][j] != ' ')
        {
            if (len >= 4 && i == ri && j == rj)//是否有回路(len>=4很关键)
            {
                if (len > _max)
                {
                    _max = len;
                }
                cycles++;
            }
            return;
        }
        maze[i][j] = '*';
        int ii, jj;
        for (int k = 0; k < 8; k++)//上下左右直接走,对角线需要加特判
        {
            ii = i + dir[k][0];
            jj = j + dir[k][1];
            if (ii > 0 && jj > 0 && ii < h && jj < w)
            {
                if (k < 4 || judge(i, j, k - 4))
                {
                    dfs(ii, jj, len + 1);
                }
            }
        }
    }
    
    void trans()//转换成maze
    {
        int ii, jj;
        for (int i = 0; i < h; i++)
        {
            ii = i << 1;
            for (int j = 0; j < w; j++)
            {
                jj = j << 1;
                if (rect[i][j] == '/')
                {
                    maze[ii][jj] = maze[ii + 1][jj + 1] = ' ';
                    maze[ii + 1][jj] = maze[ii][jj + 1] = '/';
                }
                else
                {
                    maze[ii + 1][jj] = maze[ii][jj + 1] = ' ';
                    maze[ii][jj] = maze[ii + 1][jj + 1] = '\';
                }
            }
            jj = w << 1;
            maze[ii][jj] = maze[ii + 1][jj] = '';
        }
        h = (h << 1) - 1;
        w = (w << 1) - 1;
    }
    int main()
    {
        int cases = 1;
        while (scanf("%d%d", &w, &h), w && h)
        {
            for (int i = 0; i < h; i++)
            {
                scanf("%s", rect[i]);
            }
            trans();
            cycles = _max = 0;
            
            for (int i = 1; i < h; i++)//回路不可能会到最外围一圈
            {
                for (int j = 1; j < w; j++)
                {
                    if (maze[i][j] == ' ')
                    {
                        ri = i;
                        rj = j;
                        dfs(i, j, 0);
                    }
                }
            }
            printf("Maze #%d:
    ", cases++);
            if (cycles)
            {
                printf("%d Cycles; the longest has length %d.
    
    ", cycles, _max);
            }
            else
            {
                printf("There are no cycles.
    
    ");
            }
        }
        return 0;
    }
  • 相关阅读:
    一起了解 .Net Foundation 项目 No.7
    一起了解 .Net Foundation 项目 No.6
    一起了解 .Net Foundation 项目 No.5
    一起了解 .Net Foundation 项目 No.4
    一起了解 .Net Foundation 项目 No.3
    实时流式计算系统中的几个陷阱
    DataHub——实时数据治理平台
    聊聊流计算系统中的核心问题:状态管理
    一小时搭建实时数据分析平台
    重大更新!Druid 0.18.0 发布—Join登场,支持Java11
  • 原文地址:https://www.cnblogs.com/jecyhw/p/3914669.html
Copyright © 2011-2022 走看看