uva_705-Slash Maze

/*      题目大意:输入一个由'/'和'\'组成的迷宫,判断它有无环路,如果有,输出
 * 环路数与最大环路的大小,如果没有,输出 “There are no cycles.”
 *      解题思路:刚开始看到这道题,没一点思路。。。然后想了一晚,想到一个很
 * 笨的方法,结果越写越复杂。。。最后还是去网上找了个思路,用3*3的矩阵表示两种
 * 斜线:
 *      /:  001             \:  100
 *          010                 010
 *          100                 001
 * 然后搜索没有与边界相连的0的数量,最后除以3,得到环路大小
 *  例如:
 *  6 4
 *  \//\\/
 *  \///\/
 *  //\\/\
 *  \/\///
 *
 * 转化为:(#为搜索到的环路)
 * 100001001100100001
 * 01001001##10010010
 * 0011001####1001100
 * 100001##1##1100001
 * 01001##1##1##10010
 * 0011##1##1####1100
 * 001##11##1####1100
 * 01##1##1##1##10010
 * 1##1####1##1100001
 * 1####11####1002001
 * 01##1001##10020010
 * 001100001100200100
*/
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

#define MAX     75*3+1
#define DIR     4

int dir[][2] = {            // 搜索的方向
    {-1, 0}, {0, -1},{0, 1}, {1, 0}
};
char a[MAX][MAX];           // 储存迷宫
bool visited[MAX][MAX];     // 标记是否走过
int w, h;                   // 迷宫的宽和高
bool flag;                  // 标记是否是圆
int num;                    // 记录圆的大小

/* 搜索,如果越界,则不为环路,如果搜索处已被标记或值为‘1’,返回 */
void DFS(int x, int y) {
    if( x < 0 || y < 0 || x > 3*h || y > 3*w ) {
        flag = false;
        return ;
    }
    if( visited[x][y] || a[x][y] == '1' ) return ;
    num ++;
    visited[x][y] = true;
    for(int i = 0; i < DIR; i ++)
        DFS(x+dir[i][0], y+dir[i][1]);
}

int main(int argc, char const *argv[]) {
#ifndef ONLINE_JUDGE
    freopen("test.in", "r", stdin);
#endif
    int ans, count, cas(1);
    while( scanf("%d %d", &w, &h), w || h ) {

        // 初始化
        ans = 0;
        count = 0;
        memset(a, '0', sizeof(a));
        memset(visited, false, sizeof(visited));

        // 输入处理
        for(int i = 0; i < 3*h; i += 3) {
            getchar();
            for(int j = 0; j < 3*w; j += 3) {
                if( '/' == getchar() ) {
                                    a[i][j+2] = '1';
                            a[i+1][j+1] = '1';
                    a[i+2][j] = '1';
                } else {
                    a[i][j] = '1';
                            a[i+1][j+1] = '1';
                                    a[i+2][j+2] = '1';
                }
            }
        }

        // 搜索
        for(int i = 0; i < 3*h; i ++) {
            for(int j = 0; j < 3*w; j ++) {
                num = 0;
                flag = true;
                if( !visited[i][j] && a[i][j] == '0' ) {
                    DFS(i, j);
                    if( flag ) {
                        count ++;
                        ans = max(ans, num);
                    }
                }
            }
        }

        // 输出
        printf("Maze #%d:\n", cas ++);
        if( count )
            printf("%d Cycles; the longest has length %d.\n\n", count, ans/3);
        else
            printf("There are no cycles.\n\n");
    }

    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值