UVA Slash Maze(BFS)

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


      这个题一开始看不懂题目要求,看懂之后没有思路,不知道怎么做,只好求助于大神们的解题报告。

   

刚开始方格是斜着放的,这样的话,比较难转换成用数组来存。

因为题目所给的斜杆的长度是一个格子长的两倍,于是,

把原来的图像扩大两倍,在数组中用两个格子来存储一根斜线。

这样的话,就可以用数组来表示这个图像。



其中,五角星代表的格子数组中是空的(可以用一个符号来表示),这些格子是

可以走的。

转换后,就可以很简单的用搜索做出这道题了。

其中需要注意的地方:  搜索时,可以往8个方向走,其中,往上、下、左、右

走时,如果那个格子为空的,那么就可以直接走过去。

但是如果往斜着方向走时,仅仅是空的还不足以判断可以走,还需要再特判。

例如, 会出现如下情况:


这里从左上往右下走(或者从右下往左上走)时,虽然右下是空的,但是不可以走, 因为有斜杆”墙”阻挡着。



代码:


#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<iostream>
#include<algorithm>
#include<queue>

using namespace std;

int jx[] = {0,1,0,-1,1,1,-1,-1};
int jy[] = {1,0,-1,0,1,-1,1,-1};
char a[201][201];
int map[201][201];
int v[201][201];
int n,m;
int xx,yy,xx1,yy1;
int cnt;

struct node
{
    int x;
    int y;
};

int BFS(int ii,int jj)
{
    xx = 0;
    yy = 0;
    int count = 0;
    struct node t,f;
    queue<node>q;
    t.x = ii;
    t.y = jj;
    xx1 = t.x;
    yy1 = t.y;
    q.push(t);
    v[t.x][t.y] = 1;
    count++;
    while(!q.empty())
    {
        t = q.front();
        q.pop();
        int pf = 0;
        for(int i=0; i<8; i++)
        {
            if(pf>=1)
            {
                break;
            }
            f.x = t.x + jx[i];
            f.y = t.y + jy[i];
            if(f.x>=0 && f.x<n && f.y>=0 && f.y<m && map[f.x][f.y] == 1 && v[f.x][f.y] == 0)
            {
                if(i == 1 || i == 2 || i == 3 || i == 0)
                {
                    q.push(f);
                    v[f.x][f.y] = 1;
                    xx = f.x;
                    yy = f.y;
                    count++;
                    pf++;
                }
                else if(i == 4 && a[f.x/2][f.y/2]!='/')
                {
                    q.push(f);
                    v[f.x][f.y] = 1;
                    xx = f.x;
                    yy = f.y;
                    count++;
                    pf++;

                }
                else if(i == 5 && a[f.x/2][f.y/2]!='\\')
                {
                    q.push(f);
                    v[f.x][f.y] = 1;
                    xx = f.x;
                    yy = f.y;
                    count++;
                    pf++;
                }
                else if(i == 6 && a[f.x/2][f.y/2]!='\\')
                {
                    q.push(f);
                    v[f.x][f.y] = 1;
                    xx = f.x;
                    yy = f.y;
                    count++;
                    pf++;
                }
                else if(i == 7 && a[f.x/2][f.y/2]!='/')
                {
                    q.push(f);
                    v[f.x][f.y] = 1;
                    xx = f.x;
                    yy = f.y;
                    count++;
                    pf++;
                }
            }
        }
    }
    return count;
}

int main()
{
    int kk = 0;
    while(scanf("%d%d",&m,&n)!=EOF)
    {
        if(n == 0 && m == 0)
        {
            break;
        }
        kk++;
        cnt = 0;
        int pflag = 0;
        memset(v,0,sizeof(v));
        for(int i=0; i<n; i++)
        {
            scanf("%s",a[i]);
            for(int j=0; j<m; j++)
            {
                if(a[i][j] == '/')
                {
                    map[2*i][2*j] = 1;
                    map[2*i][2*j+1] = 0;
                }
                else if(a[i][j] == '\\')
                {
                    map[2*i][2*j] = 0;
                    map[2*i][2*j+1] = 1;
                }
            }
            for(int j=0; j<m; j++)
            {
                if(a[i][j] == '/')
                {
                    map[2*i+1][2*j] = 0;
                    map[2*i+1][2*j+1] = 1;
                }
                else if(a[i][j] == '\\')
                {
                    map[2*i+1][2*j] = 1;
                    map[2*i+1][2*j+1] = 0;
                }
            }
        }
        n = 2 * n;
        m = 2 * m;
        int maxx = 0;
        int flag = 0;
        for(int i=0; i<n; i++)
        {
            for(int j=0; j<m; j++)
            {
                if(map[i][j] == 1 && v[i][j] == 0)
                {
                    flag = 0;
                    int pk = BFS(i,j);
                    for(int v1=0; v1<8; v1++)
                    {
                        int x1 = xx + jx[v1];
                        int y1 = yy + jy[v1];
                        if(xx1 == x1 && yy1 == y1)
                        {
                            flag = 1;
                            break;
                        }
                    }
                    if(flag == 1 && pk>=4)
                    {
                        pflag = 1;
                        cnt++;
                        if(pk > maxx)
                        {
                            maxx = pk;
                        }
                    }
                }
            }
        }
        printf("Maze #%d:\n",kk);
        if(pflag == 1)
        {
            printf("%d Cycles; the longest has length %d.\n\n",cnt,maxx);
        }
        else
        {
            printf("There are no cycles.\n\n");
        }
    }
    return 0;
}




Miguel A. Revilla
2000-02-09

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

叶孤心丶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值