Slash Maze
Slash Maze |
By filling a rectangle with slashes (/) and backslashes (), youcan generate nice little mazes. Here is an example:
![](http://uva.onlinejudge.org/external/7/p705.gif)
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 (), 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;
}