UVA-705 Slash Maze

UVA-705 Slash Maze

题目大意:用/ \给出一个斜着的矩阵,求里面封闭环的个数和最大长度。
解题思路:一开始就懵逼系列 , 看同学题解有的思路 , 将给的图进行放大 令 ‘\’ 变成
100
010
001
‘/’反之 然后用dfs找被1包围0的个数 除以 3即可

/*************************************************************************
    > File Name: UVA-705.cpp
    > Author: Robin
    > Mail: 499549060@qq.com 
    > Created Time: 2016年08月08日 星期一 113648************************************************************************/

#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstring>
using namespace std;
int d[4][2] = { {1,0}, {0,1}, {-1,0}, {0,-1}};
int map[3000][3000];
int m,n;
int num;
int flag;
void dfs (int x, int y) {
    map[x][y] = 1;
    for (int i = 0; i < 4; i++) 
        if (x+d[i][0] >= 0 && x+d[i][0] < 3*n && y+d[i][1] >= 0 && y+d[i][1] < 3*m) {
            if (map[x+d[i][0]][y+d[i][1]] == 0) {
                num++;
                dfs (x+d[i][0], y + d[i][1]);
            }
        }else flag = 0;
}
int main() {
    int t = 1;
    while (scanf("%d%d\n", &m, &n) && m+n) {
        memset(map, 0, sizeof(map));
        char c;
        int s = 0;
        for (int i = 0; i < n; i++)
            for (int j = 0; j < m+1; j++) {
                scanf("%c", &c);
                if ( c == '\\') {
                    map[3*i][3*j] = 1;
                    map[3*i+1][3*j+1] = 1;
                    map[3*i+2][3*j+2] = 1;
                }
                if (c == '/') {
                    map[3*i][3*j+2] = 1;
                    map[3*i+1][3*j+1] = 1;
                    map[3*i+2][3*j] = 1;
                }
            }
        int max = 0;
        for (int i = 0; i < 3*n; i++)
            for (int j = 0; j < 3*m; j++) {
                flag = 1;
                num = 1;
                if (map[i][j] == 0) {dfs(i,j);
                if ( flag) {
                    s++;
                    if ( num >= max) max = num;
                }
                }
            }
        if (s == 0) printf("Maze #%d:\nThere are no cycles.\n\n",t);
        else printf("Maze #%d:\n%d Cycles; the longest has length %d.\n\n",t, s,max/3);
        t++;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值