Slash Maze
Slash Maze |
By filling a rectangle with slashes (/) and backslashes (), 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 integers w and 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.'', wherek 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.
解决方案:方法我没想出,看了大神的代码。就是把’\‘和’/‘变成四个格的,用0和1来表示斜杠。如
01
10 表示’/‘。
然后,八个方向进行dfs,但在左上,左下,右上,右下的地方要特殊处理,分类讨论。
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
bool vis[220][220];
int Maz[220][220];
int n,m;
int dir[9][2]={{0,0},{-1,0},{1,0},{0,-1},{0,1},{-1,-1},{-1,1},{1,-1},{1,1}};
int cnt,Max,Count;
bool flag;
void input()
{
memset(Maz,0,sizeof(Maz));
memset(vis,0,sizeof(vis));
for(int i=0; i<=2*m+1; i++) //翻倍后的迷宫的上下外围不能访问
Maz[0][i]=Maz[2*n+1][i]=1;
for(int i=0; i<=2*n+1; i++) //翻倍后的迷宫的左右外围不能访问
Maz[i][0]=Maz[i][2*m+1]=1;
for(int i=1;i<=n;i++)
{
char s[200];
scanf("%s",s+1);
for(int j=1;j<=m;j++)
{
if(s[j]=='/')
{
Maz[2*i][(2*j)-1]=1;
Maz[(2*i)-1][2*j]=1;
}
else
{
Maz[2*i][2*j]=1;
Maz[(2*i)-1][(2*j)-1]=1;
}
}
}
return ;
}
void dfs(int x,int y)
{
vis[x][y]=1;
cnt++;//格子数计数
if(x==1 || x==2*n || y==1 || y==2*m) flag=0;
for(int i=1;i<=8;i++)
{ int xx,yy;
xx=(x+dir[i][0]);
yy=(y+dir[i][1]);
if(!Maz[xx][yy]&&!vis[xx][yy])
{
if(i<=4) dfs(xx,yy);
else //斜方向
{
if(i==5) //左上
{
if( (Maz[x][y-1]&&Maz[x-1][y-2])&& (Maz[x][y+1]&&Maz[x-1][y])&& !(x&1)&&(y&1) ||
(Maz[x-1][y]&&Maz[x-2][y-1])&& (Maz[x+1][y]&&Maz[x][y-1])&& (x&1)&&!(y&1) )
dfs(xx,yy);
}
else if(i==6) //右上
{
if((Maz[x][y-1]&&Maz[x-1][y])&& (Maz[x][y+1]&&Maz[x-1][y+2])&& !(x&1)&&!(y&1) ||
(Maz[x-1][y]&&Maz[x-2][y+1])&& (Maz[x+1][y]&&Maz[x][y+1])&& (x&1)&&(y&1) )
dfs(xx,yy);
}
else if(i==7) //左下
{
if( (Maz[x][y-1]&&Maz[x+1][y-2])&& (Maz[x][y+1]&&Maz[x+1][y])&& (x&1)&&(y&1)||
(Maz[x][y-1]&&Maz[x-1][y])&& (Maz[x+1][y]&&Maz[x+2][y-1])&& !(x&1)&&!(y&1))
dfs(xx,yy);
}
else //右下
{
if( (Maz[x][y-1]&&Maz[x+1][y])&& (Maz[x][y+1]&&Maz[x+1][y+2])&& (x&1)&& !(y&1) ||
(Maz[x-1][y]&&Maz[x][y+1])&& (Maz[x+1][y]&&Maz[x+2][y+1])&& !(x&1)&& (y&1) )
dfs(xx,yy);
}
}
}
}
return ;
}
void solve()
{
Max=0;
for(int i=1;i<=2*n;i++)
for(int j=1;j<=2*m;j++)
if(!Maz[i][j])
if(!vis[i][j])
{
cnt=0;
flag=true;
dfs(i,j);
if(flag)
{
Count++;
if(cnt>Max) Max=cnt;
}
}
}
int main(){
int k=0;
while(~scanf("%d%d",&m,&n)&&(n+m))
{
input();
Max=0;Count=0;
solve();
cout<<"Maze #"<<++k<<":\n";
if(Count) cout<<Count<<" Cycles; the longest has length "<<Max<<"."<<endl;
else cout<<"There are no cycles.\n";
cout<<endl;
}
return 0;
}