Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Write a program that finds the largest area of equal neighbor elements in a rectangular matrix and prints its size.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace _07.LargeArea
- {
- class LargeArea
- {
- struct coord
- {
- public int col;
- public int row;
- }
- static int maxAreaCells = 0;
- static int tempAreaCells = 0;
- static int[,] matrix =
- {
- {1,3,2,2,2,4},
- {3,3,3,2,4,4},
- {4,3,1,2,3,3},
- {4,3,1,3,3,1},
- {4,3,3,3,1,1},
- };
- static bool[,] isVisited = new bool[matrix.GetLength(0), matrix.GetLength(1)];
- static int CheckEquals(int i, int j)
- {
- //проверява дали има еднакви съседни
- //Console.WriteLine("matrix[{0},{1}]={2} ", i, j, matrix[i,j]);
- List<coord> temp = new List<coord>();
- int l0 = matrix.GetLength(0);
- int l1 = matrix.GetLength(1);
- coord c = new coord();
- c.row = i;
- c.col = j;
- temp.Add(c);
- //Console.WriteLine(" r " + c.row + " c " + c.col);
- int temprow = i;
- int tempcol = j;
- while (true) {
- for (int t = 0; t < temp.Count; t++)
- {
- if (!isVisited[temp[t].row, temp[t].col])
- {
- temprow = temp[t].row;
- tempcol = temp[t].col;
- break;
- }
- }
- if (((temprow - 1) >= 0) && ((temprow - 1) <l0) && (tempcol>=0) && (tempcol<l1))
- {
- //check to left
- if (matrix[temprow, tempcol] == matrix[temprow - 1, tempcol])
- {
- c.row = temprow - 1;
- c.col = tempcol;
- temp.Add(c);
- //Console.WriteLine(" r " + c.row + " c " + c.col);
- }
- }
- if (((temprow +1) >= 0) && ((temprow + 1) < l0) && (tempcol >= 0) && (tempcol < l1))
- {
- //check to right
- if (matrix[temprow, tempcol] == matrix[temprow + 1, tempcol])
- {
- c.row = temprow + 1;
- c.col = tempcol;
- temp.Add(c);
- //Console.WriteLine(" r " + c.row + " c " + c.col);
- }
- }
- if (((temprow ) >= 0) && ((temprow ) < l0) &&( (tempcol+1) >= 0) &&((tempcol+1) < l1))
- {
- //check down
- if (matrix[temprow, tempcol] == matrix[temprow, tempcol + 1])
- {
- c.col = tempcol +1;
- c.row = temprow;
- temp.Add(c);
- //Console.WriteLine(" r " + c.row + " c " + c.col);
- }
- }
- if (((temprow) >= 0) && ((temprow) < l0) && ((tempcol - 1) >= 0) && ((tempcol - 1) < l1))
- {
- //check up
- if (matrix[temprow, tempcol] == matrix[temprow, tempcol - 1])
- {
- c.col = tempcol - 1;
- c.row = temprow;
- temp.Add(c);
- // Console.WriteLine(" r " + c.row + " c " + c.col);
- }
- }
- isVisited[temprow, tempcol] = true;
- bool v = true;
- for (int t = 0; t < temp.Count; t++)
- {
- v = v && isVisited[temp[t].row, temp[t].col];
- }
- if (v) break;
- }
- //remove duplicated elements
- return temp.Distinct().Count();
- }
- static void Main(string[] args)
- {
- int l0=matrix.GetLength(0);
- int l1=matrix.GetLength(1);
- List<coord> result = new List<coord>();
- int bestResult = 0;
- int tempResult;
- for (int i = 0; i < matrix.GetLength(0); i++)
- {
- for (int j = 0; j < matrix.GetLength(1); j++)
- {
- coord c = new coord();
- c.row = i;
- c.col = j;
- if (!isVisited[i, j])//check what are the neighboring cells
- {
- tempResult= CheckEquals(i, j);
- Console.WriteLine("Temp result {0} for matrix element {1} ", tempResult,matrix[i,j]);
- if (tempResult > bestResult) bestResult = tempResult;
- }
- else
- {
- continue;
- }
- }
- }
- Console.WriteLine("The best result is "+bestResult);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement