您的位置:首页 > 其它

Slash Maze - UVa 705 搜索

2014-09-16 23:31 381 查看


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


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

思路:将一个字符换成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");
    }
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: