UVA 705

斜线迷宫

这个题真的有些奇特,想了一会还是搞不清楚到底应该如何表征迷宫,如何数数

将格子分成9个或者4个小格子,然后通过深度优先搜索数格子

#include<iostream>
#include<cstdio>
#include<cstring>
#include<memory>
#include<queue>
#include<algorithm>
using namespace std;

///
int row, col;
int map[230][230];
bool vis[230][230];

int cnt, curLen, maxLen; //回路个数,当前长度,最大长度
bool iscyc;              //是否找到回路

void dfs(int r, int c)
{
    if(r < 0 || r >= row || c < 0 || c >= col)
    {
        iscyc = false; // 由于没有分枝,所以能走出图就一定不会构成环。
        return;
    }
    if(map[r][c] || vis[r][c])
        return;
    vis[r][c] = true;
    ++curLen;
    dfs(r -1, c);
    dfs(r, c + 1);
    dfs(r + 1, c);
    dfs(r, c - 1);
}

int main()
{
	///
	int i, j;
	char ch;
    int ca = 0;
    while(EOF != scanf("%d%d", &col, &row) && (col || row))
    {
        row *= 3;
        col *= 3;
        memset(map, 0, sizeof(map));
        memset(vis, 0, sizeof(vis));
        for(i = 0; i < row; i += 3)
        {
            getchar();
            for(int j = 0; j < col; j += 3)
            {
                scanf("%c", &ch);
                if(ch == '/')
                {
                    map[i][j + 2] = 1;
                    map[i + 1][j + 1] = 1;
                    map[i + 2][j] = 1;
                }
                else if(ch == '\\')
                {
                    map[i][j] = 1;
                    map[i + 1][j + 1] = 1;
                    map[i + 2][j + 2] = 1;
                }
            }
        }
        cnt = 0;
        maxLen = 0;
        for(i = 0; i < row; ++i)
            for(int j = 0; j < col; ++j)
            {
                if(!map[i][j] && !vis[i][j])
                {
                    iscyc = true;
                    curLen = 0;
                    dfs(i, j);
                    if(iscyc)
                    {
                        ++cnt;
                        if(curLen > maxLen)
                            maxLen = curLen;
                    }
                }
            }
			printf("Maze #%d:\n", ++ca);
			if(cnt)
				printf("%d Cycles; the longest has length %d.\n\n", cnt, maxLen / 3);
			else
				printf("There are no cycles.\n\n");
    }


	///
    return 0;
}


 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值