uva 705

放大处理,放大两倍后,循环中的空位‘0’恰好等于原图像中小格子的数量;

\  => \0     / => 0/

        0\            /0

\//\\/    ==>    \00/0/\0\00/
\///\/              0\/0/00\0\/0
//\\/\              \00/0/0/\00/
\/\///              0\/0/0/00\/0
                    0/0/\0\00/\0
                    /0/00\0\/00\
                    \00/\00/0/0/
                    0\/00\/0/0/0
   


 

#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;

int w, h;
char t[75][76];
char maze[150][155], vis[150][155];
int dir[8][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}, {-1, -1}, {1, -1}, {-1, 1}, {1, 1}};
int count, ans, flag, cnt;  //count 记录发现的循环总数, ans 记录最多的小格子数量;

void init(){
    ans = -0x3f3f3f3f;
    memset(vis, 0, sizeof(vis));
    memset(maze, 0, sizeof(maze));
    count = 0;
}
void read_input(){
    for(int i = 0; i < h; ++i){
        gets(t[i]);
    }
    for(int i = 0; i < h; ++i){       //做了一个转换,将斜线放大了两倍;
        for(int j = 0; j < w; ++j){
            if(t[i][j] == '\\'){
                maze[2*i][2*j] = maze[2*i+1][2*j+1] = '\\';
                maze[2*i+1][2*j] = maze[2*i][2*j+1] = '0';
            }
            if(t[i][j] == '/'){
                maze[2*i][2*j] = maze[2*i+1][2*j+1] = '0';
                maze[2*i+1][2*j] = maze[2*i][2*j+1] = '/';
            }
        }
    }
}
bool judge(int x, int y, int n_x, int n_y){
    if((n_x > x && n_y > y) || (n_x < x && n_y < y)){    
        if(maze[x][n_y] == '/' && maze[n_x][y] == '/')
            return true;
    }
    if((n_x > x && n_y < y) || (n_x < x && n_y > y)){
        if(maze[x][n_y] == '\\' && maze[n_x][y] == '\\')
            return true;
    }
    return false;
}

void dfs(int x, int y){
    ++cnt;
    for(int i = 0; i < 8; ++i){
        if(i < 4){
            int n_x = x + dir[i][0], n_y = y + dir[i][1];
            if(n_x < 0 || n_x >= 2*h || n_y < 0 || n_y >= 2*w){
                flag = 0;    //如果能够访问到边界,不构成循环;
                continue;
            }
            if(maze[n_x][n_y] == '0' && !vis[n_x][n_y]){
                vis[n_x][n_y] = 1;
                dfs(n_x, n_y);
            }
        }
        else{
            int n_x = x + dir[i][0], n_y = y + dir[i][1];
            if(n_x < 0 || n_x >= 2*h || n_y < 0 || n_y >= 2*w){
                flag = 0;
                continue;
            }
            if(maze[n_x][n_y] != '0' || judge(x, y, n_x, n_y))
                continue;
            if(!vis[n_x][n_y]){
                vis[n_x][n_y] = 1;
                dfs(n_x, n_y);
            }
        }
    }
}
void deal(){
    for(int i = 0; i < 2*h; ++i)
        for(int j = 0; j < 2*w; ++j)
            if(!vis[i][j] && maze[i][j] == '0'){
                vis[i][j] = 1;
                flag = 1;
                cnt = 0;
                dfs(i,j);
                if(flag){
                    ++count;
                    if(cnt > ans){
                        ans = cnt;
                    }
                }
            }
}

int main()
{
    int cas = 0;
    while(scanf("%d%d", &w, &h) != EOF){
        if(!w && !h)
            break;
        getchar();
        init();
        read_input();
        deal();
        printf("Maze #%d:\n", ++cas);
        if(count){
            printf("%d Cycles; the longest has length %d.\n", count, ans);
        }
        else
            printf("There are no cycles.\n");
        printf("\n");
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值