UVA - 705 Slash Maze(dfs+floodfiil)

来源:互联网 发布:mac有必要安装office 编辑:程序博客网 时间:2024/06/04 23:35

 Slash Maze 

By filling a rectangle with slashes (/) and backslashes ($\backslash$), youcan generate nice little mazes. Here is an example:

As you can see, paths in the maze cannot branch, so the whole maze onlycontains cyclic paths and paths entering somewhere and leavingsomewhere 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 thelength of the longest one. The length is defined as the number ofsmall squares the cycle consists of (the ones bordered by gray linesin the picture). In this example, the long cycle has length 16 andthe short one length 4.

Input 

The input contains several maze descriptions. Each description begins with oneline containing two integersw andh ($1 \le w, h \le 75$), the width and the height of the maze. The nexth lines represent the maze itself, and containw 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 lengthl.'', wherek is the number of cycles in the maze andl 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倍,并将斜杠标记为1,这样只要遍历4个方向就好了。

如果在dfs时,遇到边界,就将flag标记为false。

最后将所求的结果/3,就是最大的长度。


#include <iostream>#include <stdio.h>#include <string.h>using namespace std;const int MAX = 100;const int dr[] = {-1 , 0 , 1 , 0};const int dc[] = { 0 , 1 , 0 , -1};char grid[MAX][MAX];int map[3*MAX][3*MAX];int vis[3*MAX][3*MAX];int w,h;bool flag; //flag 用于标记是否构成回路int step; //step 用于统计步数void dfs(int r,int c) {if( r < 0 || r >= 3*h || c < 0 || c >= 3*w) {return ;}if(map[r][c] == 0 && vis[r][c] == 0) {step++;vis[r][c] = 1;if(r == 0 || r == (3*h -1) || c == 0 || c == (3*w - 1)) {flag = false;}for(int i = 0; i < 4; i++) {dfs(r + dr[i],c + dc[i]);}}}int main() {int r,c;int cas = 1;int cnt,max;while(scanf("%d%d",&w,&h) != EOF && (w || h)) {//输入for(int i = 0; i < h; i++) {scanf("%s",grid[i]);}//将迷宫*3memset(map,0,sizeof(map));memset(vis,0,sizeof(vis));for(int i = 0; i < h; i++) {for(int j = 0; j < w; j++) {r = 3 * i;c = 3 * j;if(grid[i][j] == '/') {map[r][c+2] = map[r+1][c+1] = map[r+2][c] = 1;}else if(grid[i][j] == '\\'){map[r][c] = map[r+1][c+1] = map[r+2][c+2] = 1;}}}max = -1000;cnt = 0;for(int i = 0; i < 3*h; i++) {for(int j = 0; j < 3*w; j++) {if (vis[i][j] || map[i][j])continue;flag = true;step = 0;dfs(i,j);if( flag ){cnt++;if(max < step) {max = step;}}}}printf("Maze #%d:\n",cas++);if( cnt > 0 ) {printf("%d Cycles; the longest has length %d.\n",cnt,max/3);}else {printf("There are no cycles.\n");}printf("\n");}return 0;}


0 0