1103:Maze (已翻译题目)(图形模拟+简单搜索)

描述

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.

通过使用斜杠(/)和反斜杠()填充矩形,可以生成漂亮的小迷宫。这里有一个例子: 正如你所看到的,迷宫中的路径不能分支,所以整个迷宫只包含循环路径和进入某处和离开某处的路径。我们只对周期感兴趣。在我们的例子中,有两个。您的任务是编写一个程序来计算周期并找出最长周期的长度。长度定义为循环所包含的小正方形的数量(图片中以灰色线条为边界的正方形)。在本例中,长周期的长度为16,短周期的长度为4。

输入
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.

输入包含几个迷宫描述。每个描述以一行开始,其中包含两个整数 w 和 h (1 < = w,h < = 75) ,以及迷宫的宽度和高度。接下来的 h 行表示迷宫本身,每行包含 w 字符; 所有这些字符都将是“/”或“”。输入由以 w = h = 0开始的测试用例终止。这个事例不应该被处理。

输出
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.

对于每个迷宫,首先输出行“ Maze # n:”,其中 n 是迷宫的数目。然后,输出行“ k 循环; 最长的有长度 l”,其中 k 是迷宫中的循环数,l 是最长循环的长度。如果迷宫不包含任何周期,输出行“ There are no Cycle。”.在每个测试用例之后输出一个空行。

样例输入
6 4
\//\\/
\///\/
//\\/\
\/\///
3 3
///
\//
\\\
0 0

样例输出
Maze #1:
2 Cycles; the longest has length 16.

Maze #2:
There are no cycles.
来源
Mid-Central European Regional Contest 1999


分析

其实这题就是难在图形模拟上面,我是用一个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;
}

给个赞和关注吧

  • 48
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值