static void Main(string[] args) { //DS_OperationSpeedTest.test(); //Permutation.TEST(); //DFS_Algo_Stack.main(); //DFS_Find_DAG_TopologicalSort.main(); //DFS_Find_StronglyConnectedComponent_SCC.main(); //int[] test = new[] {4, 7, 3, 8, 6, 9, 1}; //int[] test2 = new[] {1,2,3,4,5,6,7,8,9}; //Sort_QuickSort_Graph.QuickSort(test, 0, 6, test2 ); DFS_Algo.main(); //DFS_Find_StronglyConnectedComponent_SCC.main(); //BFS_Algo.main(); //TestStack.Test(); //MyStack.main(); //MyQueue.main(); //MySequentialQueue.main(); //MyCircularQueue.main(); //TestHashSet.Test(); //QuickSort_Lomuto.Run(); Console.Read(); }
public int LongestIncreasingPath(int[][] matrix) { Func <int, int, int, int> getIdx = (row, col, rowLen) => { return(rowLen * row + col); }; //建立 graph directed graph 的 edge方向 小->大 mxn個點 會有 List <Tuple <int, int> > edges = new List <Tuple <int, int> >(); // matrix m x n int[列][行] // 0 1 2 i=0 j= 0 + 1~3 // 3 4 5 i=1 j= 3 + 1~3 // 6 7 8 i=2 j= 6 + 1~3 // 點的編號 把二維轉成 一維邊號 如上 3x3 編號 1~9 int rowLength = matrix.Length; int colLength = matrix[0].Length; // 水平邊 for (int i = 0; i < rowLength; i++) //固定列 { for (int j = 0; j < colLength - 1; j++) //行 { int left = getIdx(i, j, rowLength); int right = getIdx(i, j + 1, rowLength); if (matrix[i][j] < matrix[i][j + 1]) { edges.Add(Tuple.Create(left, right)); } else if (matrix[i][j] > matrix[i][j + 1]) { edges.Add(Tuple.Create(right, left)); } } } // 垂直邊 for (int i = 0; i < matrix.Length; i++) //固定行 { for (int j = 0; j < matrix[0].Length - 1; j++) //列 { int left = getIdx(j, i, rowLength); int right = getIdx(j + 1, i, rowLength); if (matrix[j][i] < matrix[j + 1][i]) { edges.Add(Tuple.Create(left, right)); } else if (matrix[j][i] > matrix[j + 1][i]) { edges.Add(Tuple.Create(right, left)); } } } List <int> vertexes = new List <int>(); for (int i = 0; i < rowLength * colLength - 1; i++) { vertexes.Add(i); } //graph Graph <int> graph = new Graph <int>(vertexes, edges, GraphOption.Diirected); DFS_Algo dfs = new DFS_Algo(); //DFS_Algo.IsMarkGrayDark = false; dfs.DFS(0, graph); int maxDfsPath = dfs.GetMaxDfsPathLen(); return(maxDfsPath); }