poj 1103 & zoj 1142 Maze (图形模拟+简单搜索)

Maze
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 1138 Accepted: 451

Description

By filling a rectangle with slashes (/) and backslashes (\), 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 <= w,h <= 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 "k Cycles; 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.

Source



题意:

给一个地图,找出有多少个封闭的图形,这样的图形最大的一个占多少格。


思路:

其实这题就是难在图形模拟上面,我是用一个3*3的格子模拟的一个 ‘×’

如 ‘\'  就可以表示为 

100

010

001

然后就是解决哪些位置为格子,这样处理后会发现格子是有规律的,然后我预处理了格子的位置,就剩下简单的搜索计数的问题了,可以先用dfs(0,0)跑一遍,剩下的未标记的都是环的成员了。


代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#pragma comment (linker,"/STACK:102400000,102400000")
#define maxn 305
#define MAXN 200005
#define mod 1000000007
#define INF 0x3f3f3f3f
#define pi acos(-1.0)
#define eps 1e-6
typedef long long ll;
using namespace std;

int n,m,k,cnt,flag;
double ans,tot;
bool mp[maxn][maxn];
bool vis[maxn][maxn];
double val[maxn][maxn];
int dx[]= {-1,1,0,0};
int dy[]= {0,0,-1,1};
char s[maxn];

void show()
{
    int i,j,t;
    for(i=1; i<=3*n; i++)
    {
        for(j=1; j<=3*m; j++)
        {
            printf("%d",mp[i][j]);
        }
        printf("\n");
    }
}
void dfs(int x,int y)
{
    int i,j,t,tx,ty;
    for(i=0; i<4; i++)
    {
        tx=x+dx[i];
        ty=y+dy[i];
        if(tx<0||tx>3*n+1||ty<0||ty>3*m+1||vis[tx][ty]||mp[tx][ty]) continue ;
        tot+=val[tx][ty];
        vis[tx][ty]=1;
        dfs(tx,ty);
    }
}
void solve()
{
    int i,j,t;
    memset(vis,0,sizeof(vis));
    vis[0][0]=1;
    dfs(0,0);
    k=ans=0;
    for(i=1; i<=3*n; i++)
    {
        for(j=1; j<=3*m; j++)
        {
            if(!vis[i][j]&&!mp[i][j])
            {
                k++;
                vis[i][j]=1;
                tot=val[i][j];
                dfs(i,j);
                ans=max(ans,tot);
            }
        }
    }
}
int main()
{
    int i,j,t,nx,ny,test=0;
    memset(val,0,sizeof(val));
    for(i=1; i<=100; i++)
    {
        for(j=1; j<=100; j++)
        {
            nx=3*i-1,ny=3*j-1;
            mp[nx][ny]=1;
            mp[nx-1][ny-1]=1;
            mp[nx+1][ny+1]=1;
            mp[nx-1][ny+1]=1;
            mp[nx+1][ny-1]=1;
        }
    }
    for(i=1;i<=300;i++)
    {
        for(j=1;j<=300;j++)
        {
            if(!mp[i][j]) val[i][j]=0.5;
        }
    }
    while(scanf("%d%d",&m,&n),n||m)
    {
        memset(mp,0,sizeof(mp));
        for(i=1; i<=n; i++)
        {
            scanf("%s",s);
            for(j=1; j<=m; j++)
            {
                nx=3*i-1,ny=3*j-1;
                mp[nx][ny]=1;
                if(s[j-1]=='\\')
                {
                    mp[nx-1][ny-1]=1;
                    mp[nx+1][ny+1]=1;
                }
                else
                {
                    mp[nx-1][ny+1]=1;
                    mp[nx+1][ny-1]=1;
                }
            }
        }
//        show();
        solve();
        printf("Maze #%d:\n",++test);
        if(k) printf("%d Cycles; the longest has length %d.\n",k,int(ans));
        else printf("There are no cycles.\n");
        printf("\n");
    }
    return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值