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.

题意:输入'/'、'\'组合成一个迷宫。。要找出迷宫中可以形成回路的回路个数和回路中最长的回路长度。。。

思路:这里用了2个方法。。。

第一个方法是: 把迷宫每一个格子转换成3 *3的格子。。斜线的格子用1表示,其余用0表示,每个格子保存好后。在新的地图进行搜索。这样就可以广搜BFS。如果地图中为0点的点可以走出边界。就是不可能形成回路。

如果不会走出边界。就必然可以形成回路,每次搜完一点后,把相邻的一片标记掉。就不用重复搜索了。如果搜索到可以形成回路的。他的长度为走过的格子数除以3。

#include <stdio.h>
#include <string.h> int n, m;
int d[4][2] = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}};
int map[275][275];
int maxx;
char sb; struct Q
{
int x;
int y;
} q[66666]; int vis[275][275];
int numc; void bfs(int x, int y)
{
memset(q, 0, sizeof(q));
memset(vis, 0, sizeof(vis));
int h = 0;
int r = 1;
int num = 1;
q[h].x = x;
q[h].y = y;
vis[x][y] = 1;
while (h < r)
{
for (int i = 0; i < 4; i ++)
{
int xx = q[h].x + d[i][0];
int yy = q[h].y + d[i][1];
if (xx < 0 || xx >= 3 * n || yy < 0 || yy >= 3 * m)
{
return;
}
if (map[xx][yy] == 0 && vis[xx][yy] == 0)
{
vis[xx][yy] = 1;
q[r].x = xx;
q[r].y = yy;
num ++;
r ++;
}
}
h ++;
}
numc ++;
if (maxx < num / 3)
maxx = num / 3;
} void bfs2(int x, int y)
{
memset(q, 0, sizeof(q));
int h = 0;
int r = 1;
q[h].x = x;
q[h].y = y;
map[x][y] = 1;
while (h < r)
{
for (int i = 0; i < 4; i ++)
{
int xx = q[h].x + d[i][0];
int yy = q[h].y + d[i][1];
if (map[xx][yy] == 0 && xx >= 0 && xx < 3 * n && yy >= 0 && yy < 3 * m)
{
map[xx][yy] = 1;
q[r].x = xx;
q[r].y = yy;
r ++;
}
}
h ++;
}
}
int main()
{
int tt = 1;
while (scanf("%d%d", &m, &n) != EOF && n + m)
{
maxx = 0;
numc = 0;
memset(map, 0, sizeof(map));
getchar();
for (int i = 0; i < n; i ++)
{
for (int j = 0; j < m; j ++)
{
scanf("%c", &sb);
if (sb == '\\')
{
map[i * 3][j * 3] = 1;
map[i * 3 + 1][j * 3 + 1] = 1;
map[i * 3 + 2][j * 3 + 2] = 1;
}
if (sb == '/')
{
map[i * 3][j * 3 + 2] = 1;
map[i * 3 + 1][j * 3 + 1] = 1;
map[i * 3 + 2][j * 3] = 1;
}
}
getchar();
}
for (int i = 0; i < 3 * n; i ++)
{
for (int j = 0; j < 3 * m; j ++)
{
if (map[i][j] == 0)
{
bfs(i, j);
bfs2(i, j);
}
}
}
printf("Maze #%d:\n", tt ++);
if (numc)
printf("%d Cycles; the longest has length %d.\n\n", numc, maxx);
else
printf("There are no cycles.\n\n");
}
return 0;
}

第二个方法是:跟物理中的光学有关。。其实可以把每个墙壁看成镜子,路径看成光线。光射入镜子以后反射的路径是唯一的。然后每次就按照这个路径搜索。。如果会射出边界。。那么这条路径不可取。如果回射回起点,表明有回路。。这个方法写起来比较烦。要考虑墙壁是‘\','/'和路径入射方向。。但是时间比上一个方法快了许多。

#include <stdio.h>
#include <string.h> int bo;
int n, m;
int xxx, yyy;
int www;
int numc;
int d[4][2] = {{-1,0},{0,1},{1,0},{0,-1}};
char map[80][80];
int vis[2][80][80];
int maxx;
void dfs(int x, int y, int f, int w, int bu)
{
if (x < 1 || x > n || y < 1 || y > m)
return;
if (bo == 0)
bo = 1;
else
{
if (x == xxx && y == yyy && w == www)
{
numc ++;
if (maxx < bu)
maxx = bu;
return;
}
}
vis[w][x][y] = 1;
int xx = x + d[f][0];
int yy = y + d[f][1];
if (f == 0)
{
if (map[xx][yy] == '\\')
{
dfs(xx, yy, 3, 1, bu + 1);
}
if (map[xx][yy] == '/')
{
dfs(xx, yy, 1, 1, bu + 1);
}
}
if (f == 1)
{
if (map[xx][yy] == '\\')
{
dfs(xx, yy, 2, 1, bu + 1);
}
if (map[xx][yy] == '/')
{
dfs(xx, yy, 0, 0, bu + 1);
}
}
if (f == 2)
{
if (map[xx][yy] == '\\')
{
dfs(xx, yy, 1, 0, bu + 1);
}
if (map[xx][yy] == '/')
{
dfs(xx, yy, 3, 0, bu + 1);
}
}
if (f == 3)
{
if (map[xx][yy] == '\\')
{
dfs(xx, yy, 0, 0, bu + 1);
}
if (map[xx][yy] == '/')
{
dfs(xx, yy, 2, 1, bu + 1);
}
}
}
int main()
{
int tt = 1;
while (scanf("%d%d", &m, &n) != EOF && n + m)
{
maxx = 0;
numc = 0;
getchar();
memset(map, 0, sizeof(map));
memset(vis, 0, sizeof(vis));
for (int i = 1; i <= n; i ++)
{
gets(map[i] + 1);
}
for (int i = 1; i <= n; i ++)
for (int j = 1; j <= m; j ++)
{
xxx = i; yyy = j;
if (vis[0][i][j] == 0)
{
www = 0;
bo = 0;
dfs(i, j, 0, 0, 0);
if (map[i][j] == '/')
{
bo = 0;
dfs(i, j, 3, 0, 0);
}
if (map[i][j] == '\\')
{
bo = 0;
dfs(i, j, 1, 0, 0);
}
}
if (vis[1][i][j] == 0)
{
www = 1;
if (map[i][j] == '/')
{
bo = 0;
dfs(i, j, 1, 1, 0);
}
if (map[i][j] == '\\')
{
bo = 0;
dfs(i, j, 3, 1, 0);
}
bo = 0;
dfs(i, j, 2, 1, 0);
}
}
printf("Maze #%d:\n", tt ++);
if (numc)
printf("%d Cycles; the longest has length %d.\n\n", numc / 2, maxx);
else
printf("There are no cycles.\n\n");
}
return 0;
}

UVA 705 Slash Maze的更多相关文章

  1. 705 - Slash Maze

    By filling a rectangle with slashes (/) and backslashes ( ), you can generate nice little mazes. Her ...

  2. uva 705

    题意,给你迷宫算出其中个封闭空间的个数,以及求出所有封闭的空间的最大步长,但是给你的迷宫式“/”,“\”来标记的所以需要将每个格子分开来3*3的格子来算, 一开始按照2*2来算,2*2有临界情况不好算 ...

  3. UVA题目分类

    题目 Volume 0. Getting Started 开始10055 - Hashmat the Brave Warrior 10071 - Back to High School Physics ...

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

    http://www.cnblogs.com/sxiszero/p/3618737.html 下面给出的题目共计560道,去掉重复的也有近500题,作为ACMer Training Step1,用1年 ...

  5. ACM训练计划step 1 [非原创]

    (Step1-500题)UVaOJ+算法竞赛入门经典+挑战编程+USACO 下面给出的题目共计560道,去掉重复的也有近500题,作为ACMer Training Step1,用1年到1年半年时间完成 ...

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

    下面给出的题目共计560道,去掉重复的也有近500题,作为ACMer Training Step1,用1年到1年半年时间完成.打牢基础,厚积薄发. 一.UVaOJ http://uva.onlinej ...

  7. 【UVA 10307 Killing Aliens in Borg Maze】最小生成树, kruscal, bfs

    题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=20846 POJ 3026是同样的题,但是内存要求比较严格,并是没有 ...

  8. uva 784 Maze Exploration 染色 搜索水题 DFS

    染色问题,其实就是看看图上某一点能扩散多少. 用DFS解决,因为BFS不是很熟 =-=...以后要多练. 提交后32ms,优化了一下,在递归前进行判定,优化到22ms,不是优化的很好... 代码: # ...

  9. [UVA] 784 - Maze Exploration

      Maze Exploration  A maze of rectangular rooms is represented on a two dimensional grid as illustra ...

随机推荐

  1. getchar()函数的返回值赋给char型,用if(ch=getchar() != EOF)测试,输入ctrl+z同样可以结束循环的分析

    2013-07-18 21:35:58 getchar()函数的返回值赋给char型,用if(ch=getchar() != EOF)测试,输入ctrl+z同样可以结束循环的分析. char是字符型数 ...

  2. unity3d中获得物体的尺寸(size)

    1:获得诸如Plane.Cube的size.    1):可以为它们添加Collider,然后使用XXX.collider.bounds.size;该方法获得的size和缩放比例有关,是一一对应的,缩 ...

  3. UVa 11440 (欧拉函数) Help Tomisu

    题意: 给出N和M,统计区间x ∈ [2, N!],x满足所有素因子都大于M的x的个数. 分析: 首先将问题转化一下,所有素因子都大于M 等价于 这个数与M!互素 对于k大于M!,k与M!互素等价于 ...

  4. spring data jpa入门学习

    本文主要介绍下spring data jpa,主要聊聊为何要使用它进行开发以及它的基本使用.本文主要是入门介绍,并在最后会留下完整的demo供读者进行下载,从而了解并且开始使用spring data ...

  5. 算法的时间复杂度(大O表示法)

    定义:如果一个问题的规模是n,解这一问题的某一算法所需要的时间为T(n),它是n的某一函数 T(n)称为这一算法的“时间复杂性”. 当输入量n逐渐加大时,时间复杂性的极限情形称为算法的“渐近时间复杂性 ...

  6. ps一般常用的快捷键

    jpg 不支持透明和半透明 :网页中的大图,高清图(体积大) gif 支持透明 不支持半透明 :网页中的小图标(动画图片)png8 支持透明 不支持半透明 :网页中的小图标png24 支持透明 支持半 ...

  7. Asp.net MVC 4 异步方法

    今天我们来看一下,同样功能在 Asp.net MVC 4 下的实现,基于.net framework 4.5 下的async支持,让我们的代码更加简单,看下面片断代码名叫Index的Action方法: ...

  8. WCF 实例化与会话

    实例管理旨在解决服务实例的激活和服务实例生命周期的控制,会话的目的是在于保持相同客户端(服务代理)多次服务调用的状态. 实例上下文 实例上下文是对服务实例的封装,是WCF管理服务实例生命周期的依托,S ...

  9. JMX学习笔记(二)-Notification

    Notification通知,也可理解为消息,有通知,必然有发送通知的广播,JMX这里采用了一种订阅的方式,类似于观察者模式,注册一个观察者到广播里,当有通知时,广播通过调用观察者,逐一通知. 这里写 ...

  10. [Tommas] 测试场景 VS 测试用例 哪个更好?(转)

    分享一篇网上别人的感悟:      6年前,我在一家中型跨国公司工作的时候,我建议与其浪费时间在准备充分的测试用例,还不如编写描述测试场景的文档.所有的人都对我的建议.投以烦恼的目光.他们的脸上清晰地 ...