Uva 705 - Slash Maze

 

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

这题就是给出的图其实是个迷宫的平面图,问你图中一共能形成多少个圈,最长的圈有多长= =
刚看到题目。。。。完全没感觉,然后看到博客上有人说是把/和\转成3*3的01矩阵来做。。。
/
001
010
100
\
100
010
001
1表示不可走
然后就可以用dfs来找环了
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
int mp[350][350];
int n,m;
int dis[4][2]={1,0,-1,0,0,1,0,-1};
int tmp;
int flag;
void dfs(int x,int y)
{
    mp[x][y]=1;
    tmp++;
    if(x<=0||x>=(3*n-1)||y<=0||y>=(3*m-1))
    flag=1; 
    if(x>0&&mp[x-1][y]==0)
    dfs(x-1,y);  
    if(y>0&&mp[x][y-1]==0)
    dfs(x,y-1);
    if(x<(3*n-1)&&mp[x+1][y]==0)
    dfs(x+1,y);
    if(y<(3*m-1)&&mp[x][y+1]==0)
    dfs(x,y+1);
}
int main()
{
    int t=0; 
    while(scanf("%d%d",&m,&n)!=EOF&&n&&m)
    {
        getchar();
        memset(mp,0,sizeof(mp));
        char s[150];
        for(int i=0;i<n;i++)
        {
            scanf("%s",s);
            int l=strlen(s); 
            for(int j=0;j<l;j++)
            {
                if(s[j]=='\\')
                mp[3*i][3*j]=1,mp[3*i+1][3*j+1]=1,mp[3*i+2][3*j+2]=1;
                if(s[j]=='/')
                mp[3*i][3*j+2]=1,mp[3*i+1][3*j+1]=1,mp[3*i+2][3*j]=1; 
            }
        }
        int ans=0,sum=0; 
        flag=0;
        tmp=0; 
        for(int i=0;i<3*n;i++)
        {
            for(int j=0;j<3*m;j++)
            {
                if(mp[i][j]==0)
                {
                    dfs(i,j);
                    if(flag==0)
                    {
                        sum++;
                        ans=max(ans,tmp);
                    }
                tmp=0;
                flag=0; 
                } 
            } 
        }
        printf("Maze #%d:\n",++t); 
        if(sum==0)
        {
            printf("There are no cycles.\n");
        }
        else 
        {
            printf("%d Cycles; the longest has length %d.\n",sum,ans/3);
        } 
        printf("\n");
    }
    return 0;
} 

 



 

转载于:https://www.cnblogs.com/NaCl/p/4787514.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值