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 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.'', where k 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.

 

很有意思的题目,无从下手,看到别人的用0,1的方式表示/ \感觉豁然开朗了许多,

这个题目可以把原题放大即可,放大2倍每次需要考虑8个方向,放大三倍每次考虑四个方向

放大2倍图

\  10     /   01

   01         10

放大3倍图

\  100  /   001

   010      010

   001      100

只需要把没个斜杠按放大图转化为0,1的矩阵,0表示可走,1表示不可走,剩下的就和以前一样了。

#include<stdio.h>
#include<string.h>
int num=0,f,sum,max,pos,h,w,move[4][2]={1,0,-1,0,0,-1,0,1},map[400][400];
void dfs(int x,int y)
{int X,Y,i;
 for (i=0;i<4;i++)
 {X=x+move[i][0];
  Y=y+move[i][1];
  if ((X>=0)&&(X<h*3)&&(Y>=0)&&(Y<w*3))
  {if (map[X][Y]==0)
   {++pos; map[X][Y]=1; dfs(X,Y);}
  }
  else
  f=0;;
 }
};
int main()
{int i,j;
 char s[200];
 while (scanf("%d%d",&w,&h),w+h)
 {getchar();
  ++num; sum=0; max=0;
  for (i=0;i<h*3;i++)
  for (j=0;j<w*3;j++)
  map[i][j]=0;
  for (i=0;i<h;i++)
  {gets(s);
   for (j=0;j<w;j++)
   if (s[j]=='\\')
        {map[3*i][3*j]=1;map[3*i+1][3*j+1]=1;map[3*i+2][3*j+2]=1;}
   else {map[3*i+2][3*j]=1;map[3*i+1][3*j+1]=1;map[3*i][3*j+2]=1;}

  }

 for (i=0;i<3*h;i++)
 for (j=0;j<3*w;j++)
 {if (map[i][j]==0)
  {pos=1; f=1; map[i][j]=1;
   dfs(i,j);
   if (f)
   {++sum;
    if (pos>max) max=pos;
   }
  }
 }
 if (sum==0) printf("Maze #%d:\nThere are no cycles.\n\n",num);
 else printf("Maze #%d:\n%d Cycles; the longest has length %d.\n\n",num,sum,max/3);因为原图放大3倍所以要除以3}
 return 0;
}


 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值