705 – Slash MazeTime limit: 3.000 seconds |
Slash Maze |
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 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
1 2 3 4 5 6 7 8 9 10 |
6 4 \//\\/ \///\/ //\\/\ \/\/// 3 3 /// \// \\\ 0 0 |
Sample Output
1 2 3 4 5 |
Maze #1: 2 Cycles; the longest has length 16. Maze #2: There are no cycles. |
Miguel A. Revilla
2000-02-09
好烦的一道题目!
题意就是说求输入里面有几个闭环以及最大的闭环面积是多少~
光看着杠杠肯定是看不出名堂的,首先把输入数据处理一下,把
1 2 |
\ 和 / |
分别转化成
1 2 3 |
01 和 10 10 01 |
正好题意里面每一条杠杠是等于两个单位,所以这里转化过后真好一个点就对应一个面积单位,然后就是找闭环了,自然是用DFS搞一搞,不过这个地方得注意一下的就是在斜方向遍历的时候有一定的限定条件:
比如grid[row][col](row为行数,col为列数, 0 <= row <= 2h-1 0 <= col <= 2w-1)要移动到grid[row-1][col-1]的时候就必须满足
1 2 |
row为偶数,col为奇数,且grid[row-1][col]、grid[row][col-1]和grid[row+1][col]都为1 |
或者
1 2 |
row为奇数,col为偶数,且grid[row][col-1]、grid[row-1][col]和grid[row][col+1]都为1 |
其他三个斜方向同理。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
/******************************************************** * File Name: uvaoj705.cpp * Author: razrLeLe * Mail: razrlele@gmail.com * Homepage: https://yueyu.io * Created Time: Fri 13 Feb 2015 05:50:32 PM CST ******************************************************/ #include "vector" #include "set" #include "deque" #include "queue" #include "algorithm" #include "functional" #include "iostream" #include "cstdio" #include "cmath" #include "cstdlib" #include "string" #include "cstring" #include "string.h" #include "map" #include "cctype" #include "list" #include "stack" using namespace std; #define INF 0x3f3f3f3f #define LOCAL const int maxn = 160; int grid[maxn][maxn]; int w, h; void DFS(int row, int col, int &ans) { grid[row][col] = -1; if(row == 0 || row == 2*h-1 || col == 0 || col == 2*w-1) ans = -1; if(!grid[row-1][col] && row-1 >= 0) DFS(row-1, col, ans); if(!grid[row+1][col] && row+1 < 2*h) DFS(row+1, col, ans); if(!grid[row][col+1] && col+1 < 2*w) DFS(row, col+1, ans); if(!grid[row][col-1] && col-1 >= 0) DFS(row, col-1, ans); if(!grid[row-1][col-1]) { if(!(row%2) && col%2 && grid[row-1][col] == 1 && grid[row][col-1] == 1 && grid[row+1][col] == 1 || row%2 && !(col%2) && grid[row][col-1] == 1 && grid[row-1][col] == 1 && grid[row][col+1] == 1) DFS(row-1, col-1, ans); } if(!grid[row-1][col+1]) { if(row%2 && col%2 && grid[row-1][col] == 1 && grid[row][col-1] == 1 && grid[row][col+1] == 1 || !(row%2) && !(col%2) && grid[row+1][col] == 1 && grid[row-1][col] == 1 && grid[row][col+1] == 1) DFS(row-1, col+1, ans); } if(!grid[row+1][col-1]) { if(row%2 && col%2 && grid[row-1][col] == 1 && grid[row][col-1] == 1 && grid[row+1][col] == 1 || !(row%2) && !(col%2) && grid[row+1][col] == 1 && grid[row][col-1] == 1 && grid[row][col+1] == 1) DFS(row+1, col-1, ans); } if(!grid[row+1][col+1]) { if(!(row%2) && col%2 && grid[row][col+1] == 1 && grid[row][col-1] == 1 && grid[row+1][col] == 1 || row%2 && !(col%2) && grid[row+1][col] == 1 && grid[row-1][col] == 1 && grid[row][col+1] == 1) DFS(row+1, col+1, ans); } if(ans >= 0) ans++; return ; } int main() { #ifdef LOCAL freopen("/home/razrlele/build/data.txt", "r", stdin); freopen("/home/razrlele/build/out.txt", "w", stdout); #endif int numcase = 0; while(cin >> w >> h && w && h) { char tmp; int count = 0; int maxcircle = 0; memset(grid, -1, sizeof(grid)); for(int i = 0; i < 2*h; i+=2) for(int j = 0; j < 2*w; j+=2) { cin >> tmp; if(tmp == '/') { grid[i][j+1] = 1; grid[i+1][j] = 1; grid[i][j] = 0; grid[i+1][j+1] = 0; } else { grid[i][j] = 1; grid[i+1][j+1] = 1; grid[i][j+1] = 0; grid[i+1][j] = 0; } } for(int i = 0; i < 2*h; i++) for(int j = 0; j < 2*w; j++) { int ans = 0; if(!grid[i][j]) DFS(i, j, ans); if(ans > 0) { count++; maxcircle = (maxcircle < ans)?ans:maxcircle; } } printf("Maze #%d:\n", ++numcase); if(count) printf("%d Cycles; the longest has length %d.\n\n", count, maxcircle); else printf("There are no cycles.\n\n"); } return 0; } |