UVa705 Slash Maze(DFS)
2014-02-09 08:51
323 查看
Slash Maze |
), 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 integersw 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:'', wheren 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.
分析:
求封闭圆环个数,及其中最大周长;
将图案放大化,
/001 \ 100
/ 010 \ 010
/ 100 \001
之后即转化为DFS,只要不触碰边界,必为封闭圆环,周长应除以三以恢复初始状态
#include<stdio.h> #include<string.h> int h,w,len,sum,max; bool ok; char s1[80][80]; int s2[250][250]; int d[4][2]= {-1,0,0,-1,0,1,1,0}; void dfs(int x,int y) { int nx,ny,i; for(i=0; i<4; i++) { nx=x+d[i][0]; ny=y+d[i][1]; if(nx>=0&&nx<h*3&&ny>=0&&ny<w*3) { if(s2[nx][ny]==0) { len++; s2[nx][ny]=1; dfs(nx,ny); } } else ok=0; } } int main() { int cas=1,i,j; while(scanf("%d%d",&w,&h),w!=0) { sum=max=0; memset(s1,0,sizeof(s1)); memset(s2,0,sizeof(s2)); for(i=0; i<h; i++) { scanf("%s",s1[i]); } for(i=0; i<h; i++) for(j=0; j<w; j++) { if(s1[i][j]=='/') s2[i*3][j*3+2]=s2[i*3+1][j*3+1]=s2[i*3+2][j*3]=1; else s2[i*3][j*3]=s2[i*3+1][j*3+1]=s2[i*3+2][j*3+2]=1; } for(i=0; i<h*3; i++) for(j=0; j<w*3; j++) { if(s2[i][j]==0) { len=ok=s2[i][j]=1; dfs(i,j); if(ok) { sum++; if(len>max) max=len; } } } printf("Maze #%d:\n",cas++); if(sum==0) printf("There are no cycles.\n\n"); else printf("%d Cycles; the longest has length %d.\n\n",sum,max/3); } return 0; }
相关文章推荐
- uva 705 Slash Maze(DFS)
- uva 705 Slash Maze(斜线迷宫) —— DFS
- uva 705 Slash Maze(几何转换+DFS)
- UVa 705 Slash Maze(DFS)(斜线构图)
- UVA - 705 Slash Maze(dfs+floodfiil)
- Slash Maze - UVa 705 搜索
- UVA 705 - Slash Maze (Flood_Fill + DFS)
- uva 705 - Slash Maze
- uva705 - Slash Maze 【转化+dfs】
- uva705 Slash Maze
- UVa 705 - Slash Maze
- UVA 705 - Slash Maze (Flood_Fill + DFS)
- uva 705 Slash Maze
- UVA 705-Slash Maze
- uva 705 Slash Maze 斜杠构图统计图中回路
- UVA 705 - Slash Maze
- UVa 705 - Slash Maze
- UVa Problem 705 Slash Maze (斜线迷宫)
- UVa 705 - Slash Maze
- uva 705 - Slash Maze