Slash Maze - UVa 705 搜索

本文详细阐述了如何使用字符填充矩形生成迷宫,并通过转换字符为3*3子块来计数迷宫中的封闭区间及寻找最长的封闭区间长度。通过实例演示算法过程,包括输入处理、迷宫描述、输出格式等关键步骤。

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

Slash Maze 

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 wand 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.


题意:按照图中的解释,有多少个封闭区间,最大的封闭区间的格数是多少。

思路:将一个字符换成3*3的格子,1代表是墙(即深色线),2代表图中浅色线,可以发现,在一个封闭区间内,是连续的0和2组成,然后有多少个2(多少个浅色墙),就有多少个格子。

AC代码如下:

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int num[250][250],vis[250][250],t,ret,ans,maxn,ox[4]={-1,0,1,0},oy[4]={0,1,0,-1};
char s[100];
bool flag;
void dfs(int x,int y)
{
    if(num[x][y]==1 || vis[x][y]==t)
      return;
    vis[x][y]=t;
    if(num[x][y]==2)
      ret++;
    if(num[x][y]==-1)
    {
        flag=false;
        return;
    }
    int k;
    for(k=0;k<4;k++)
       dfs(x+ox[k],y+oy[k]);
}
int main()
{
    int n,m,i,j,k;
    while(~scanf("%d%d",&m,&n) && n+m)
    {
        t++;
        getchar();
        for(i=1;i<=n;i++)
        {
            gets(s+1);
            for(j=1;j<=m;j++)
            {
                if(s[j]=='/')
                {
                    num[i*3-2][j*3-2]=2;num[i*3-2][j*3-1]=0;num[i*3-2][j*3]=1;
                    num[i*3-1][j*3-2]=0;num[i*3-1][j*3-1]=1;num[i*3-1][j*3]=0;
                    num[i*3  ][j*3-2]=1;num[i*3  ][j*3-1]=0;num[i*3  ][j*3]=2;
                }
                else
                {
                    num[i*3-2][j*3-2]=1;num[i*3-2][j*3-1]=0;num[i*3-2][j*3]=2;
                    num[i*3-1][j*3-2]=0;num[i*3-1][j*3-1]=1;num[i*3-1][j*3]=0;
                    num[i*3  ][j*3-2]=2;num[i*3  ][j*3-1]=0;num[i*3  ][j*3]=1;
                }
            }
        }
        for(i=0;i<=m*3+1;i++)
           num[0][i]=num[n*3+1][i]=-1;
        for(i=0;i<=n*3+1;i++)
           num[i][0]=num[i][m*3+1]=-1;
        ans=0;maxn=0;
        for(i=1;i<=n*3;i++)
           for(j=1;j<=m*3;j++)
              if(vis[i][j]!=t && num[i][j]!=1)
              {
                  flag=true;
                  ret=0;
                  dfs(i,j);
                  if(flag)
                  {
                      if(ret>0)
                      ans++;
                      maxn=max(ret,maxn);
                  }
              }
        printf("Maze #%d:\n",t);
        if(ans>0)
          printf("%d Cycles; the longest has length %d.\n\n",ans,maxn);
        else
          printf("There are no cycles.\n\n");
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值