uva_705-Slash Maze
2012-11-25 17:26
337 查看
/* 题目大意:输入一个由'/'和'\'组成的迷宫,判断它有无环路,如果有,输出 * 环路数与最大环路的大小,如果没有,输出 “There are no cycles.” * 解题思路:刚开始看到这道题,没一点思路。。。然后想了一晚,想到一个很 * 笨的方法,结果越写越复杂。。。最后还是去网上找了个思路,用3*3的矩阵表示两种 * 斜线: * /: 001 \: 100 * 010 010 * 100 001 * 然后搜索没有与边界相连的0的数量,最后除以3,得到环路大小 * 例如: * 6 4 * \//\\/ * \///\/ * //\\/\ * \/\/// * * 转化为:(#为搜索到的环路) * 100001001100100001 * 01001001##10010010 * 0011001####1001100 * 100001##1##1100001 * 01001##1##1##10010 * 0011##1##1####1100 * 001##11##1####1100 * 01##1##1##1##10010 * 1##1####1##1100001 * 1####11####1002001 * 01##1001##10020010 * 001100001100200100 */ #include <cstdio> #include <cstring> #include <algorithm> using namespace std; #define MAX 75*3+1 #define DIR 4 int dir[][2] = { // 搜索的方向 {-1, 0}, {0, -1},{0, 1}, {1, 0} }; char a[MAX][MAX]; // 储存迷宫 bool visited[MAX][MAX]; // 标记是否走过 int w, h; // 迷宫的宽和高 bool flag; // 标记是否是圆 int num; // 记录圆的大小 /* 搜索,如果越界,则不为环路,如果搜索处已被标记或值为‘1’,返回 */ void DFS(int x, int y) { if( x < 0 || y < 0 || x > 3*h || y > 3*w ) { flag = false; return ; } if( visited[x][y] || a[x][y] == '1' ) return ; num ++; visited[x][y] = true; for(int i = 0; i < DIR; i ++) DFS(x+dir[i][0], y+dir[i][1]); } int main(int argc, char const *argv[]) { #ifndef ONLINE_JUDGE freopen("test.in", "r", stdin); #endif int ans, count, cas(1); while( scanf("%d %d", &w, &h), w || h ) { // 初始化 ans = 0; count = 0; memset(a, '0', sizeof(a)); memset(visited, false, sizeof(visited)); // 输入处理 for(int i = 0; i < 3*h; i += 3) { getchar(); for(int j = 0; j < 3*w; j += 3) { if( '/' == getchar() ) { a[i][j+2] = '1'; a[i+1][j+1] = '1'; a[i+2][j] = '1'; } else { a[i][j] = '1'; a[i+1][j+1] = '1'; a[i+2][j+2] = '1'; } } } // 搜索 for(int i = 0; i < 3*h; i ++) { for(int j = 0; j < 3*w; j ++) { num = 0; flag = true; if( !visited[i][j] && a[i][j] == '0' ) { DFS(i, j); if( flag ) { count ++; ans = max(ans, num); } } } } // 输出 printf("Maze #%d:\n", cas ++); if( count ) printf("%d Cycles; the longest has length %d.\n\n", count, ans/3); else printf("There are no cycles.\n\n"); } return 0; }
相关文章推荐
- UVA 705 - Slash Maze (Flood_Fill + DFS)
- UVA 705 - Slash Maze
- UVa 705 - Slash Maze
- uva705 - Slash Maze 【转化+dfs】
- Uva 705 - Slash Maze
- UVA - 705 Slash Maze
- uva 705 Slash Maze 解题报告
- Slash Maze(uva 705)
- UVA 705 - Slash Maze
- Uva 705 - Slash Maze
- uva 705 - Slash Maze
- uva 705 Slash Maze
- UVA 705 Slash Maze
- uva 705 - Slash Maze(斜线迷宫)
- Uva 705 - Slash Maze
- UVA 705 Slash Maze
- UVA 705 Slash Maze
- UVA-705 Slash Maze
- uva 705 Slash Maze
- UVA 705 Slash Maze