题意,给你迷宫算出其中个封闭空间的个数,以及求出所有封闭的空间的最大步长,但是给你的迷宫式“/”,“”来标记的所以需要将每个格子分开来3*3的格子来算,
一开始按照2*2来算,2*2有临界情况不好算(233333)……
格式需要额外空一行……
题很简单,就是dfs走出边界说明不是封闭的……
不过就这样了……还是太弱了……
话说,今天又颓了一天……
233333333
代码……
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include <iostream> #include <cstdio> #include <cmath> #include <cstring> #include <algorithm> #include <cstdlib> #include <stack> #include <cctype> #include <string> #include <malloc.h> #include <queue> #include <map> using namespace std; const int INF = 0xffffff; const double Pi = 4 * atan(1); const int Maxn = 200 + 10; //int dir2[8][2] = {{-1,0},{0,-1},{-1,1},{1,-1},{-1,-1},{1,0},{0,1},{1,1}}; int dr[] = {1,0,-1,0,-1,1,-1,1}; int dc[] = {0,1,0,-1,1,-1,-1,1}; bool graph[300][300]; int w,h; int num; bool flag; void dfs(int r,int c){ if(r < 0 || c < 0 || r > 3*h-1 || c > 3*w-1 ){ flag = 1; return; } if(graph[r][c]) return; graph[r][c] = 1; num++; for(int i = 0;i < 4;i++){ int xx = r + dr[i]; int yy = c + dc[i]; dfs(xx,yy); } } int main() { #ifndef ONLINE_JUDGE freopen("inpt.txt","r",stdin); #endif int cas = 0; while(cin >> w >> h){ if(!w && !h) break; cout << "Maze #" << ++cas << ":" << endl; if(!w || !h){ cout << "There are no cycles." << endl << endl; continue; } char str[100]; memset(graph,0,sizeof(graph)); for(int i = 0;i < h;i++){ cin >> str; for(int j = 0;j < w;j++){ if(str[j] == '\'){ graph[3*i][j*3] = graph[3*i+1][3*j+1] = graph[3*i+2][3*j+2] = 1; } else if(str[j] == '/'){ graph[3*i][j*3+2] = graph[3*i+1][3*j+1] = graph[3*i+2][3*j] = 1; } } } /* for(int i = 0;i < 3*h ;i++){ for(int j = 0;j < 3*w;j++) cout << graph[i][j]; cout << endl; }*/ int cnt = 0; int road = -1; for(int i = 0;i < 3*h;i++){ for(int j = 0;j < 3 * w;j++){ if(!graph[i][j]){ flag = 0; num = 0; dfs(i,j); if(flag) continue; cnt++; if(num > road) road = num; } } } if(cnt == 0) cout << "There are no cycles." << endl; else{ cout << cnt << " Cycles; the longest has length " << road/3 << "." << endl; } cout << endl; } return 0; }
测试用例:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
6 4 //\/ //// //\/ //// 3 3 /// // \ 5 5 /// // /// /// // 4 4 //\ //\ \// \// 6 6 /// //\/ ///\ \/// /\// /// 6 4 /// \/// ///\ /// 6 4 /// \/// //// /// 0 1 1 0 75 75 \///\///////\\\///\\////////\////\/\//////\// // //\//\///\///\///\///\/\////\///////////\//// \/ /\/////\\///////////\\////\/\\\/////\\//\/////\// \ \//\/\///\//\\/\\//\//\/\\\\/\/////\//\/// ///\ /\/////\////\/\/\\//\/\///\/\///\//\/////////\/// \/ \\//\\/\////\//\/////////\/\///\/\\/////\\//\ // /////\//\\/\\/////\////\\\/\\//\//\/\//////\ /// \/\////////\//\\\//\\//////////\//\//////\\/\// \ //\///\///////\///\//////\/\////////\///\////\\//\/ /// /////////\\\/////////\//\\///\/\///\/\//\/\//// \ /\//\///\\\////////////\//\/\//////\////////\/// // //\\/\//\/\///\/////////////////////\/\\\///\//// /\ ///\/\/\///\\/\//////\/\\\/////\/////\///\\/// \ /\///\///\/\///\\\\/\//\///\/\/\\\//\///// / //\//\/\//\\///\\\/\///\///////////////\\/\///\// /// \///////\///\\///////////////\/////////\//\/\\/ / ///\\//////\\//\\///\/\\\\\///\\////\/////\\ // //\\/////\/\/\\////////\///////////\//////\/// /// /\/\\\\\\//\//////\/\\///////\//////////\////\/\/ \/ ///////\/\\/\/\//\\////////\////////\\//////////// // //\\/////\\\///\////////\\/////\///\//\//\/// / \/\/\/////\//\\\//\//////////\/\////\\/\/\////// \ /\//\\///\/\//\///////\///\//\/////\//\///////\/\ \ \///\/////////\\///\////\////////////////\/\/\\/\/ // /////////////////\//\/\////\///////\\\////\\\/// \/ \////\//\/\\/\\/\/\///\///\///\/\/\//\/\/////\ / //////\/\///\/\//\///\\//////\\///\\/\/\/////\ // //\///////\\/\/\\\////\\/\///\/\/\\\///\/\///// // //\/////\\/\//////\//\/////\/////\/\/////\////\/ \/ ///\/////\\\////\\/\////\//\//////\/\\/////////// \/ //////////\///////\//\//\//\\\/////////////\//\//// / /////\/////\\/\/\///////////\//////\////\\///\ //\ \////\//\/////\/\////////////\///\/////\////\ /// ////\//////\//\\///\//////\////////////\\/\\////// /// ////\////\\//\/////\\\////\////////\/\/\/\\\/// /// /////////\//////\/\//\//\\/\\/////\\///////\//// // /\///////////////\////\\//////\//////////\//////\//// /\ ///\///\\///\/////////////\///////////////\///\\ /\ /\\////\/\//\/\///////\//\//////\/////////\/\ /\ \/\//\/\////////\//\////////////\/\\\\//\\/\\\/ // /////////\//\/\///////////\\\///\///////\/\////\ / /////\/\///\\//////\////\////\///////////////\/\/ /\ /////\////\\//\/////\/////\///\\/\//\\////\\/\/ / /\\////\//\/\\///\\/\\///\///\\//\/////////// ///// //\\\//\/\//\\////\/\\//\//////\/////\///// //\ ///////\/\///////////////\//\////\////////////\\/ /// \////\\////\//////\/////\////\//\/\\//\///// ///// /\\\\///\/\\/\/\/////\\///////\/\\\/\/\\ // /////////\///////\//\\\////\//////////\\///\\\///\ // \//\////\///\\/\/\//////////\/\////\///////\/\\\ // \/////\//\//\\\/\//\/\\//\\/////////\\/\////// \/ /\//////\///\//\\\/\///\/\////\//\//\/\\////// /// ///\\//\\/\///\///////\//\//////////\/////////\\/ \ /\////////\\///\\//\\///\/\//////\\/\//////\/\/ \/ \//\\\/\//\/////\//\////\///\/\///////\/\//// // /\/////\////////\//\/\//\////////////\///\////// \/ //////\///\///////\/////////\////////\\/\/\/\/\\/ // /////\////\/\/////////\////////\\////////\////\///// // //\/\///\/////\/////\\///\\\/////\/\/\/////\ // /\/\\////\//\//\\///\//\\///\\/\//\/\\\/// / \/////////\\/\/////////////\//\///////\\/\\//// / ///\\////\//\//\\/////\\///////\//\/\///\//\/ \ /\\/////////////\///////\\/\////\\/////\\/\/////\ // ///////\\/\\/////\////\\//////\/\//\/////////// \/ ///\/////\//\///////\\/\/\/\\\//\/////\\\////// // /////////////\/\////////\//\////\\\//\///\/\/// \ /\//\//////\/\/////\//////\/\/\//////////////\/\/\ // //////\//////////////\//\\////\//\///////\\\////\/ // \////\//////\\\/\\/\/\///\/\///////\/////////// / ////\////\/\////////////\///\///\\////\/\//\/ // \//\////\/////\\//\////////////\\//////////\/\// \ ////\/////\//\\//\\//\/\\////\\//////\//\\/// ////// \\//\/\/////\//\//////////\/\/////\///\\\////// /\ //\/////\/////\/\////////\//////\\////////\/\/\/ /// ///////\///\//\////////\\//////\/\/////////////// \/ 5 5 //\ ///\ \\ /\// \// 0 0
ans:
Maze #1: 2 Cycles; the longest has length 16. Maze #2: There are no cycles. Maze #3: 6 Cycles; the longest has length 4. Maze #4: 2 Cycles; the longest has length 12. Maze #5: 7 Cycles; the longest has length 20. Maze #6: 2 Cycles; the longest has length 28. Maze #7: 1 Cycles; the longest has length 4. Maze #8: There are no cycles. Maze #9: There are no cycles. Maze #10: 586 Cycles; the longest has length 1128. Maze #11: 1 Cycles; the longest has length 24.