这道题目抽象出来就是要求一棵树中两点之间最长路径的最大值。
解法是对树进行后序遍历,计算每棵子树中两点之间的最长路径,最后返回这些最长路径中最大的。
以下是题目的描述:
The northern part of the Pyramid contains a very large and complicated labyrinth. The labyrinth is divided into square blocks, each of them either filled by rock, or free. There is also a little hook on the floor in the center of every free block. The ACM have found that two of the hooks must be connected by a rope that runs through the hooks in every block on the path between the connected ones. When the rope is fastened, a secret door opens. The problem is that we do not know which hooks to connect. That means also that the neccessary length of the rope is unknown. Your task is to determine the maximum length of the rope we could need for a given labyrinth.
Input The input consists of T test cases. The number of them (T) is given on the first line of the input. Each test case begins with a line containing two integers C and R (3 <= C, R <= 1000) indicating the number of columns and rows. Then exactly R lines follow, each containing C characters. These characters specify the labyrinth. Each of them is either a hash mark (#) or a period (.). Hash marks represent rocks, periods are free blocks. It is possible to walk between neighbouring blocks only, where neighbouring blocks are blocks sharing a common side. We cannot walk diagonally and we cannot step out of the labyrinth. The labyrinth is designed in such a way that there is exactly one path between any two free blocks. Consequently, if we find the proper hooks to connect, it is easy to find the right path connecting them.
Sample Output Your program must print exactly one line of output for each test case. The line must contain the sentence "Maximum rope length is X." where X is the length of the longest path between any two free blocks, measured in blocks.
Sample Input 2 3 3 ### #.# ### 7 6 ####### #.#.### #.#.### #.#.#.# #.....# #######
Sample Output Maximum rope length is 0. Maximum rope length is 8.
解题思路如下:
用一个矩阵来存储输入的字符,同时可以将矩阵中的每个free block就看作一个树的节点,记录以该节点为根的子
树的最大深度以及子树中两点之间最长路径的最大值。
struct TreeNode { int maxDepth; //该节点为根的子树的最大深度 int maxDist; //以该节点为根节点的子树中两点之间最长路径的最大值 bool visited; //遍历访问标记 };
static struct TreeNode T[R][C]; //对应brinth的树节点矩阵
static char brinth[R][C]; //输入字符矩阵
扫描T,如brinth[i][j]=='.',则以T[i][j]为根节点的树进行后序遍历,先计算以T[i-1][j]为根节点的子树的maxDepth和
maxDist, 以T[i+1][j]为根节点的子树的maxDepth和maxDist, 以T[i][j-1]为根节点的子树的maxDepth和maxDist,
以T[i][j+1]为根节点的子树的maxDepth和maxDist。
T[i][j].maxDepth = MAX( T[i-1][j].maxDepth, T[i+1][j].maxDepth, T[i][j-1].maxDepth, T[i][j+1].maxDepth)
T[i][j].maxDist = MAX(T[i-1][j].maxDist, T[i+1][j].maxDist, T[i][j-1].maxDist, T[i][j+1].maxDist,
MAX( T[i-1][j].maxDepth, T[i+1][j].maxDepth, T[i][j-1].maxDepth, T[i][j+1].maxDepth) + MAX_1( T[i-1][j].maxDepth, T[i+1][j].maxDepth, T[i][j-1].maxDepth, T[i][j+1].maxDepth) )
MAX_1( T[i-1][j].maxDepth, T[i+1][j].maxDepth, T[i][j-1].maxDepth, T[i][j+1].maxDepth)求的是这些数中的第二大值。
上面两个式子意思是:以T[i][j]为根节点的树的最大深度是其子树中最大深度加1。以T[i][j]为根节点的树中两点之间的最长路径的最大值是从深度最大的子树中的最深节点向上到达根节点,然后到深度次大的子树中的最深节点这段路径的长度,然后将这个长度与T[i-1][j], T[i+1][j], T[i][j-1], T[i][j+1]这些子树中已经求出的最大路径长度进行比较,取其中的最大值即为T[i][j].maxDist。
源代码如下:
#include <iostream> using namespace std;
#include <string.h>
#define C 1000 #define R 1000 #define HASHMARK '#' #define PERIOD '.'
struct TreeNode { int maxDepth; int maxDist; bool visited; };
static struct TreeNode T[R][C];
static char brinth[R][C];
static void dfs(int row,int col,int c, int r) { int maxDepth = 0; int maxDist = 0; int maxDepth1 = 0;
int temp[4]; int num=0;
T[row][col].visited = true;
if(col-1 >=0 && brinth[row][col-1]==PERIOD && !T[row][col-1].visited) { dfs(row,col-1,c,r); if(maxDist < T[row][col-1].maxDist) maxDist = T[row][col-1].maxDist;
if(maxDepth < T[row][col-1].maxDepth) { maxDepth1 = maxDepth; maxDepth = T[row][col-1].maxDepth; } else if(maxDepth1 < T[row][col-1].maxDepth) maxDepth1 = T[row][col-1].maxDepth; }
if(col+1 < c && brinth[row][col+1] == PERIOD && !T[row][col+1].visited) { dfs(row,col+1,c,r); if(maxDist < T[row][col+1].maxDist) maxDist = T[row][col+1].maxDist;
if(maxDepth < T[row][col+1].maxDepth) { maxDepth1 = maxDepth; maxDepth = T[row][col+1].maxDepth; } else if(maxDepth1 < T[row][col+1].maxDepth) maxDepth1 = T[row][col+1].maxDepth;
}
if(row-1 >= 0 && brinth[row-1][col] == PERIOD && !T[row-1][col].visited) { dfs(row-1,col,c,r);
if(maxDist < T[row-1][col].maxDist) maxDist = T[row-1][col].maxDist;
if(maxDepth < T[row-1][col].maxDepth) { maxDepth1 = maxDepth; maxDepth = T[row-1][col].maxDepth; } else if(maxDepth1 < T[row-1][col].maxDepth) maxDepth1 = T[row-1][col].maxDepth;
}
if(row+1 < r && brinth[row+1][col] == PERIOD && !T[row+1][col].visited) { dfs(row+1,col,c,r);
if(maxDist < T[row+1][col].maxDist) maxDist = T[row+1][col].maxDist;
if(maxDepth < T[row+1][col].maxDepth) { maxDepth1 = maxDepth; maxDepth = T[row+1][col].maxDepth; } else if(maxDepth1 < T[row+1][col].maxDepth) maxDepth1 = T[row+1][col].maxDepth; }
int dist = maxDepth+maxDepth1;
maxDist = (maxDist > dist? maxDist:dist);
T[row][col].maxDepth = maxDepth + 1; T[row][col].maxDist = maxDist;
//printf("row=%d,col=%d,maxDepth=%d,maxDepth1=%d/n",row,col,maxDepth,maxDepth1); //printf("row=%d,col=%d,maxDepth=%d,maxDist=%d/n",row,col,maxDepth+1,maxDist); }
int main(int argc,char **argv) { int c,r; int testcase;
cin>>testcase;
int maxDist;
for(int t=0;t<testcase;t++) { cin>>c>>r;
for(int i=0;i<r;i++) for(int j=0;j<c;j++) { char temp; cin>>temp; if(temp == HASHMARK || temp == PERIOD) { brinth[i][j] = temp; T[i][j].maxDepth = 0; T[i][j].maxDist = 0; T[i][j].visited = false; } }
maxDist = 0;
for(int row=0;row<r;row++) for(int col=0;col<c;col++) { if(!T[row][col].visited && brinth[row][col] == PERIOD) { dfs(row,col,c,r); maxDist = (maxDist < T[row][col].maxDist? T[row][col].maxDist:maxDist); } }
cout<<"Maximum rope length is "<<maxDist<<"."<<endl; }
return 0; }
本文介绍了一种求解迷宫中两点间最长路径的方法,通过后序遍历树结构,计算并返回最大路径长度。文章详细解释了算法流程,并提供了完整的C++实现代码。
1891

被折叠的 条评论
为什么被折叠?



