Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- //Write a program that finds the largest area of equal neighbor elements in a rectangular matrix and prints its size
- class Program
- {
- static int[,] matrix = {
- {1,3,2,2,2,4},
- {3,3,3,2,4,4},
- {1,3,1,2,3,3},
- {4,3,1,3,3,1},
- {4,3,3,3,1,1}, //not work for value 0 coz zero is used to mark
- };
- static int model;
- static int score = 0;
- static int bestScore = 0;
- static int bestModel;
- static void Main()
- {
- for (int x = 0; x < matrix.GetLength(0); x++)
- {
- for (int y = 0; y < matrix.GetLength(1); y++)
- {
- model = matrix[x,y];
- score = 0;
- CheckPoint(x, y);
- }
- }
- Console.WriteLine(bestScore+" "+bestModel);
- }
- private static void CheckPoint(int x, int y)
- {
- //bottom of recursion
- if (x < 0 || x >= matrix.GetLength(0) || y < 0 || y >= matrix.GetLength(1))
- {
- return;
- }
- if (matrix[x,y] != model)
- {
- return;
- }
- //count score
- score++;
- if (score > bestScore)
- {
- bestScore = score;
- bestModel = model;
- }
- matrix[x, y] = 0; //mark as visited
- //call recursion for all directions
- CheckPoint(x, y+1); //rigth
- CheckPoint(x+1, y); //down
- CheckPoint(x, y-1); //left
- CheckPoint(x-1, y); // up
- matrix[x, y] = model; //unmark
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement