uva705


 Slash Maze 

By filling a rectangle with slashes (/) and backslashes ($\backslash$), youcan generate nice little mazes. Here is an example:

As you can see, paths in the maze cannot branch, so the whole maze onlycontains cyclic paths and paths entering somewhere and leavingsomewhere 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 thelength of the longest one. The length is defined as the number ofsmall squares the cycle consists of (the ones bordered by gray linesin the picture). In this example, the long cycle has length 16 andthe short one length 4.

Input 

The input contains several maze descriptions. Each description begins with oneline containing two integersw and h ($1 \le w, h \le 75$), the width and the height of the maze. The nexth 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:'', wheren is the number of the maze. Then, output the line``kCycles; the longest has lengthl.'', wherek is the number of cycles in the maze andl 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

 

表示这是一个坑!太深了。

dfs强搜的,很麻烦,但毕竟自己做的。

看别人的代码,bfs+3*3转换,挺好。

#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
int m,n,M[100][100],OK,N[100][100],ix,iy;
char a[100][100];
int dfs(int i,int j,int ok,int t,int d)
{

    if(ix==i&&ok&&iy==j&&d>2)
    {
        OK=t-2;
    }
    else if(!OK&&((!ok&&!M[i][j])||(ok&&!N[i][j])))
    {
        if(!ok)
        {
            M[i][j]=1;
            if(i-1>=0&&a[i][j]==a[i-1][j]&&a[i-1][j]=='/')N[i-1][j]=1;
            if(i+1<m&&a[i][j]==a[i+1][j]&&a[i+1][j]==92)N[i+1][j]=1;
            if(j-1>=0&&a[i][j]==a[i][j-1])N[i][j-1]=1;
        }
        else
        {
            N[i][j]=1;
            if(i-1>=0&&a[i][j]==a[i-1][j]&&a[i-1][j]==92)M[i-1][j]=1;
            if(i+1<m&&a[i+1][j]==a[i][j]&&a[i+1][j]=='/')M[i+1][j]=1;
            if(j+1<n&&a[i][j]==a[i][j+1])M[i][j+1]=1;
        }
        if(!ok)
        {
            if(a[i][j]=='/')
            {
                if(j-1>=0&&a[i][j-1]==92)dfs(i,j-1,1,t+1,d+1);
                else if(i+1<m&&j-1>=0)
                {
                    if(a[i+1][j-1]=='/')dfs(i+1,j-1,0,t+2,d+1);
                    else if(a[i+1][j]==92)dfs(i+1,j,0,t+3,d+1);
                    else dfs(i,j,1,t+4,d+1);
                }
                if(i-1>=0&&a[i-1][j]==92)dfs(i-1,j,0,t+1,d+1);
                else if(i-1>=0&&j+1<n)
                {
                    if(a[i][j]=='/')dfs(i-1,j+1,0,t+2,d+1);
                    else if(a[i][j+1]==92)dfs(i,j+1,1,t+3,d+1);
                    else dfs(i,j,1,t+4,d+1);
                }
            }
            else if(a[i][j]==92)
            {
                if(i+1<m&&a[i+1][j]=='/')dfs(i+1,j,0,t+1,d+1);
                else if(i+1<m&&j+1<n)
                {
                    if(a[i+1][j+1]==92)dfs(i+1,j+1,0,t+2,d+1);
                    else if(a[i][j+1]=='/')dfs(i,j+1,1,t+3,d+1);
                    else dfs(i,j,1,t+4,d+1);
                }
                if(j-1>=0&&a[i][j-1]=='/')dfs(i,j-1,1,t+1,d+1);
                else if(i-1>=0&&j-1>=0)
                {
                    if(a[i-1][j-1]==92)dfs(i-1,j-1,0,t+2,d+1);
                    else if(a[i-1][j]=='/')dfs(i-1,j,0,t+3,d+1);
                    else dfs(i,j,1,t+4,d+1);
                }
            }
        }
        else
        {
            if(a[i][j]=='/')
            {
                if(j+1<n&&a[i][j+1]==92)dfs(i,j+1,0,t+1,d+1);
                else if(i-1>=0&&j+1<n)
                {
                    if(a[i-1][j+1]=='/')dfs(i-1,j+1,1,t+2,d+1);
                    else if(a[i-1][j]==92)dfs(i-1,j,1,t+3,d+1);
                    else dfs(i,j,0,t+4,d+1);
                }
                if(i+1<m&&a[i+1][j]==92)dfs(i+1,j,1,t+1,d+1);
                else if(i+1<m&&j-1>=0)
                {
                    if(a[i+1][j-1]=='/')dfs(i+1,j-1,1,t+2,d+1);
                    else if(a[i][j-1]==92)dfs(i,j-1,0,t+3,d+1);
                    else dfs(i,j,0,t+4,d+1);
                }
            }
            else if(a[i][j]==92)
            {
                if(j+1<n&&a[i][j+1]=='/')dfs(i,j+1,0,t+1,d+1);
                else if(i+1<m&&j+1<n)
                {
                    if(a[i+1][j+1]==92)dfs(i+1,j+1,1,t+2,d+1);
                    else if(a[i+1][j]=='/')dfs(i+1,j,1,t+3,d+1);
                    else dfs(i,j,0,t+4,d+1);
                }
                if(i-1>=0&&a[i-1][j]=='/')dfs(i-1,j,1,t+1,d+1);
                else if(i-1>=0&&j-1>=0)
                {
                    if(a[i-1][j-1]==92)dfs(i-1,j-1,1,t+2,d+1);
                    else if(a[i][j-1]='/')dfs(i,j-1,0,t+3,d+1);
                    else dfs(i-1,j-1,0,t+4,d+1);
                }
            }
        }
    }
}
int main()
{
    //freopen("1.txt","r",stdin);
    int id=1;
    while(cin>>n>>m)
    {
        if(!m&&!n)break;
        else
        {
            memset(M,0,sizeof(M));
            memset(N,0,sizeof(N));
            for(int i=0; i<m; i++)cin>>a[i];
            int sum=0,max=0;
            for(int i=0; i<m; i++)
                for(int j=0; j<n; j++)
                {
                    int t=2;
                    ix=i,iy=j;
                    OK=0;
                    if(!N[i][j])dfs(i,j,1,t,0);
                    if(OK)
                    {
                        sum++;
                        max=max>OK?max:OK;
                    }
                }
            cout<<"Maze #"<<id++<<":"<<endl;
            if(sum)cout<<sum<<" Cycles; the longest has length "<<max<<"."<<endl<<endl;
            else cout<<"There are no cycles."<<endl<<endl;
        }
    }
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值