UVA 705 Slash Maze

22 篇文章 0 订阅
10 篇文章 0 订阅

题目如下:

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

斜线迷宫的题,初看真是无从下手。在网上看了看大神们的思路,顿时只有膜拜的份了。把斜线看成是数字,这样整个抽象的斜线图就可以转化成一个简洁明了的数字矩阵,再用flood_fill算法可以求出环的最大长度。将每条斜线看成一个2*2矩阵,/表示成1,\表示成2,空格表示成0代表路,已经访问过的标记为3.先将边界的0以及与边界的0相连的0用flood_fill算法先标记为3,因为这些0是不可能构成环的,自然剩下的0都能构成环。在用flood_fill算法的时候,对于上下左右的0,可以直接用flood_fill算法访问,但对于对角线上的0,要先判断。例如右上有0,那么右边不能为\,类似有四种情况。

AC的代码如下:

#include 
   
   
    
    
using namespace std;
char graph[80][80];
int numgraph[80*2][80*2];
int w,h;
void trans()
{
    for(int i=0; i
    
    
     
     =0&&newy
     
     
      
      =0)
            if(numgraph[newx][newy]==0)
                if(newx==x||newy==y)
                    flood_fill(newx,newy);
                else
                {
                    if(dx[k]==-1&&dy[k]==-1)
                        if(numgraph[x][y-1]!=1)
                            flood_fill(newx,newy);
                    if(dx[k]==1&&dy[k]==-1)
                        if(numgraph[x][y-1]!=2)
                            flood_fill(newx,newy);
                    if(dx[k]==-1&&dy[k]==1)
                        if(numgraph[x][y+1]!=2)
                            flood_fill(newx,newy);
                    if(dx[k]==1&&dy[k]==1)
                        if(numgraph[x][y+1]!=1)
                            flood_fill(newx,newy);

                }

    }
}

int main()
{
    int N=0;
    while(cin>>w>>h)
    {
        if(w==0)
            break;
        N++;

        memset(graph,' ',sizeof graph);
        memset(numgraph,0,sizeof numgraph);
        for(int i=0; i
      
      
       
       >graph[i][j];
        }
        trans();

        for(int i=0; i
       
       
      
      
     
     
    
    
   
   

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值