Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- class DepthFirstSearch
- {
- static void Main()
- {
- int[,] m = { { 1, 3, 2, 2, 2, 4 }, { 3, 3, 3, 2, 4, 4 }, { 4, 3, 1, 2, 3, 3 }, { 4, 3, 1, 3, 3, 1 } };
- int maxPath = 0;
- for (int r = 0; r < m.GetLength(0); r++)
- for (int c = 0; c < m.GetLength(1); c++)
- {
- int count = 1;
- FindPath(m, m[r, c], new bool[m.GetLength(0), m.GetLength(1)], r, c, ref count);
- if (count > maxPath) maxPath = count;
- }
- Console.WriteLine(maxPath);
- }
- private static void FindPath(int[,] m, int n, bool[,] tM, int r, int c, ref int count)
- {
- tM[r, c] = true;
- if (r > 0 && !tM[r - 1, c] && m[r - 1, c] == n) count = Count(m, n, tM, count, r - 1, c);
- if (r < m.GetLength(0) - 1 && !tM[r + 1, c] && m[r + 1, c] == n) count = Count(m, n, tM, count, r + 1, c);
- if (c > 0 && !tM[r, c - 1] && m[r, c - 1] == n) count = Count(m, n, tM, count, r, c - 1);
- if (c < m.GetLength(1) - 1 && !tM[r, c + 1] && m[r, c + 1] == n) count = Count(m, n, tM, count, r, c + 1);
- }
- private static int Count(int[,] m, int n, bool[,] tM, int count, int nr, int nc)
- {
- tM[nr, nc] = true;
- count++;
- FindPath(m, n, tM, nr, nc, ref count);
- return count;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement