Uva 705 - Slash Maze

  Slash 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.

Input

The input contains several maze descriptions. Each description begins with one line containing two integers w and h ( ), 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 ``kCycles; 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.
这题就是给出的图其实是个迷宫的平面图,问你图中一共能形成多少个圈,最长的圈有多长= =刚看到题目。。。。完全没感觉,然后看到博客上有人说是把/和\转成3*3的01矩阵来做。。。/001010100\1000100011表示不可走然后就可以用dfs来找环了
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
int mp[350][350];
int n,m;
int dis[4][2]={1,0,-1,0,0,1,0,-1};
int tmp;
int flag;
void dfs(int x,int y)
{
    mp[x][y]=1;
    tmp++;
    if(x<=0||x>=(3*n-1)||y<=0||y>=(3*m-1))
    flag=1;
    if(x>0&&mp[x-1][y]==0)
    dfs(x-1,y);
    if(y>0&&mp[x][y-1]==0)
    dfs(x,y-1);
    if(x<(3*n-1)&&mp[x+1][y]==0)
    dfs(x+1,y);
    if(y<(3*m-1)&&mp[x][y+1]==0)
    dfs(x,y+1);
}
int main()
{
    int t=0;
    while(scanf("%d%d",&m,&n)!=EOF&&n&&m)
    {
        getchar();
        memset(mp,0,sizeof(mp));
        char s[150];
        for(int i=0;i<n;i++)
        {
            scanf("%s",s);
            int l=strlen(s);
            for(int j=0;j<l;j++)
            {
                if(s[j]==‘\\‘)
                mp[3*i][3*j]=1,mp[3*i+1][3*j+1]=1,mp[3*i+2][3*j+2]=1;
                if(s[j]==‘/‘)
                mp[3*i][3*j+2]=1,mp[3*i+1][3*j+1]=1,mp[3*i+2][3*j]=1;
            }
        }
        int ans=0,sum=0;
        flag=0;
        tmp=0;
        for(int i=0;i<3*n;i++)
        {
            for(int j=0;j<3*m;j++)
            {
                if(mp[i][j]==0)
                {
                    dfs(i,j);
                    if(flag==0)
                    {
                        sum++;
                        ans=max(ans,tmp);
                    }
                tmp=0;
                flag=0;
                }
            }
        }
        printf("Maze #%d:\n",++t);
        if(sum==0)
        {
            printf("There are no cycles.\n");
        }
        else
        {
            printf("%d Cycles; the longest has length %d.\n",sum,ans/3);
        }
        printf("\n");
    }
    return 0;
} 

 
时间: 2024-10-06 13:39:14

Uva 705 - Slash Maze的相关文章

uva 705 Slash Maze 斜线迷宫

唉,上午一直不在状态,都没有好好思考,基本上算是看的题解,切记做题一定要专注,一定要多思考,不能轻易的看题解了,这道题可以把'/'和'\'转化,用0和1表示, '/'表示为 : '\'表示为 001 100 010 010 100 001 相当于扩大了三倍,最后结果除以三就ok了 然后就可以用普通的搜索求了,还是连通问题,注意一点只要是遍历到处于边缘的0就说明这个一定不是环,wa了一次,还是没注意到每组案例之间都有一个空行:休息会,再奋战. 上代码:(基本上跟别人写的一样) #include<s

705 - Slash 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 leavin

uva705 - Slash Maze 【转化+dfs】

题目:uva705 - Slash Maze 题意:给出一个迷宫,看题目给出的图就知道,由 \ 和 / 组成,让你求有几个环,然后最大的环由几个矩形组成. 分析:这是一道很灵活的题目,关键在于对题目给出图形的转化,例如' \ ' 可以转化为 1 0 0 0 1 0 0 0 1 而' / ' 可以转化为 0 0 1 0 1 0 1 0 0 然后直接广搜或者深搜都可以,找环就可以了.也可以转化为 2 * 2 的,需要特判. AC代码: #include <cstdio> #include <

uva 705

题意,给你迷宫算出其中个封闭空间的个数,以及求出所有封闭的空间的最大步长,但是给你的迷宫式“/”,“\”来标记的所以需要将每个格子分开来3*3的格子来算, 一开始按照2*2来算,2*2有临界情况不好算(233333)…… 格式需要额外空一行…… 题很简单,就是dfs走出边界说明不是封闭的…… 不过就这样了……还是太弱了…… 话说,今天又颓了一天…… 233333333 代码…… #include <iostream> #include <cstdio> #include <c

编程题目分类(剪辑)

1. 编程入门 2. 数据结构 3. 字符串 4. 排序 5. 图遍历 6. 图算法 7. 搜索:剪枝,启发式搜索 8. 动态规划/递推 9. 分治/递归 10. 贪心 11. 模拟 12. 算术与代数 13. 组合问题 14. 数论 15. 网格,几何,计算几何 [编程入门] PC 110101, uva 100, The 3n+1 problem, 难度 1 PC 110102, uva 10189, Minesweeper, 难度 1 PC 110103, uva 10137, The T

UVA题目分类

题目 Volume 0. Getting Started 开始10055 - Hashmat the Brave Warrior 10071 - Back to High School Physics 10300 - Ecological Premium 458 - The Decoder 494 - Kindergarten Counting Game 414 - Machined Surfaces 490 - Rotating Sentences 445 - Marvelous Mazes

计划,,留

下面给出的题目共计560道,去掉重复的也有近500题,作为ACMer Training Step1,用1年到1年半年时间完成.打牢基础,厚积薄发. 一.UVaOJ http://uva.onlinejudge.org 西班牙Valladolid大学的程序在线评测系统,是历史最悠久.最著名的OJ. 一.<算法竞赛入门经典> 刘汝佳 (UVaOJ 351道题) 以下部分内容摘自:http://sdkdacm.5d6d.com/thread-6-1-1.html "AOAPC I"

算法竞赛入门经典+挑战编程+USACO

下面给出的题目共计560道,去掉重复的也有近500题,作为ACMer Training Step1,用1年到1年半年时间完成.打牢基础,厚积薄发.   一.UVaOJ http://uva.onlinejudge.org  西班牙Valladolid大学的程序在线评测系统,是历史最悠久.最著名的OJ.   二.<算法竞赛入门经典> 刘汝佳  (UVaOJ  351道题)  以下部分内容摘自:http://sdkdacm.5d6d.com/thread-6-1-1.html   "AO

(Step1-500题)UVaOJ+算法竞赛入门经典+挑战编程+USACO

下面给出的题目共计560道,去掉重复的也有近500题,作为ACMer Training Step1,用1年到1年半年时间完成.打牢基础,厚积薄发. 一.UVaOJ http://uva.onlinejudge.org 西班牙Valladolid大学的程序在线评测系统,是历史最悠久.最著名的OJ. 二.<算法竞赛入门经典> 刘汝佳  (UVaOJ  351道题)  以下部分内容摘自:http://sdkdacm.5d6d.com/thread-6-1-1.html “AOAPC I”是刘汝佳(大