UVaOJ705---Slash Maze

本文介绍了一种通过填充斜杠和反斜杠生成迷宫的方法,并提供了一个程序用于计算迷宫中周期的数量及其最长周期的长度。输入为一系列迷宫描述,输出包括每个迷宫的周期数及最长周期长度。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

705 - Slash Maze

Time limit: 3.000 seconds

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.



Miguel A. Revilla 
2000-02-09
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;

int w, h, last_x, last_y;
char str[100][100], map[300][300];
bool vis[300][300];
int dir[8][2]={{1,0}, {-1,0}, {0,1},{0,-1},
               {-1,1},{1,1},{1,-1},{-1,-1}};

void change(int row)
{
   for (int i = 0; i < (int)strlen(str[row]); i ++)
   {
      int m_row = row * 2, m_col = i * 2;
      if(str[row][i] == '\\' )
      {
            map[m_row][m_col] = '\\';
            map[m_row+1][m_col+1] = '\\';
            map[m_row][m_col+1] = ' ';
            map[m_row+1][m_col] = ' ';
      }
      else if(str[row][i] == '/')
      {
            map[m_row][m_col+1] = '/';
            map[m_row+1][m_col] = '/';
            map[m_row][m_col] = ' ';
            map[m_row+1][m_col+1] = ' ';
      }
   }
}

bool isPass(int x, int y, int dirNo)
{
    int x1,y1,x2,y2;  // x1Ϊͬrow, x2Ϊͬcol
    x1 = x, y1=y+dir[dirNo][1];
    x2 = x+dir[dirNo][0], y2=y;

    if(dirNo==4 || dirNo==6)
    {
        if(map[x1][y1]=='/' && map[x2][y2]=='/')
            return true;
    }
    else
    {
        if(map[x1][y1]=='\\' && map[x2][y2]=='\\')
            return true;
    }
    return false;
}

void dfs(int x, int y, int &cnt)
{
   for (int i = 0; i < 8; i ++)
   {
      int dx = x + dir[i][0], dy = y + dir[i][1];
      if (i < 4)
      {
         if (dx >= 0 && dx < 2 * h && dy >= 0 && dy < 2 * w && map[dx][dy] != '\\'
             && map[dx][dy] != '/' && map[dx][dy] == ' ' && !vis[dx][dy])
         {
            vis[dx][dy] = true;
            last_x = dx;
            last_y = dy;
            cnt ++;
            dfs(dx, dy, cnt);
         }
      }
      else
      {
         if (dx < 0 || dx >= 2 * h || dy < 0 || dy >= 2 * w || map[dx][dy] == '\\' || map[dx][dy] == '/' || vis[dx][dy])
         {
            continue;
         }
         if (isPass(x, y, i))
         {
            vis[dx][dy] = true;
            last_x = dx;
            last_y = dy;
            cnt ++;
            dfs(dx, dy, cnt);
         }
      }
   }
}
int main()
{
    int cas = 1;
    while (cin>>w>>h)
    {
       getchar();
       if (w == 0 && h == 0) break;
       memset(str, 0, sizeof(str));
       memset(map, 0, sizeof(map));
       memset(vis, 0, sizeof(vis));
       for (int i = 0; i < h; i ++)
       {
          gets(str[i]);
          change(i);
       }
       int maxNum = -2147483646, cnt, circleCnt = 0;
       bool haveCircle = false;
       for (int i = 0; i < 2 * h; i ++)
       {
          for (int j = 0; j < 2 * w; j ++)
          {
             if (map[i][j] == ' ' && ! vis[i][j])
             {
                map[i][j] = '#';
                cnt = 1;
                vis[i][j] = true;
                dfs(i, j, cnt);
                bool flag = false;
                for (int k = 0; k < 8; k ++)
                {
                   int dx = last_x + dir[k][0];
                   int dy = last_y + dir[k][1];
                   if (dx == i && dy == j)
                   {
                      flag = true;
                      break;
                   }
                }
                if (flag && cnt >= 4)
                {
                   haveCircle = true;
                   ++ circleCnt;
                   if (cnt > maxNum)
                   {
                      maxNum = cnt;
                   }
                }
             }
          }
       }
       cout<<"Maze #"<<cas ++<<":"<<endl;
       if(haveCircle)
       {
            cout<<circleCnt<<" Cycles; the longest has length "<<maxNum<<".\n\n";
       }
       else
       {
            cout<<"There are no cycles.\n\n";
       }
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值