Advertisement
shevitza

Equal Neighbour Elements

Dec 27th, 2013
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.25 KB | None | 0 0
  1. //Write a program that finds the largest area of equal neighbor elements in a rectangular matrix and prints its size.
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7.  
  8. namespace _07.LargeArea
  9. {
  10. class LargeArea
  11. {
  12. struct coord
  13. {
  14. public int col;
  15. public int row;
  16. }
  17.  
  18. static int maxAreaCells = 0;
  19. static int tempAreaCells = 0;
  20.  
  21. static int[,] matrix =
  22. {
  23. {1,3,2,2,2,4},
  24. {3,3,3,2,4,4},
  25. {4,3,1,2,3,3},
  26. {4,3,1,3,3,1},
  27. {4,3,3,3,1,1},
  28. };
  29. static bool[,] isVisited = new bool[matrix.GetLength(0), matrix.GetLength(1)];
  30. static int CheckEquals(int i, int j)
  31. {
  32. //проверява дали има еднакви съседни
  33. //Console.WriteLine("matrix[{0},{1}]={2} ", i, j, matrix[i,j]);
  34.  
  35. List<coord> temp = new List<coord>();
  36. int l0 = matrix.GetLength(0);
  37. int l1 = matrix.GetLength(1);
  38. coord c = new coord();
  39. c.row = i;
  40. c.col = j;
  41. temp.Add(c);
  42.  
  43. //Console.WriteLine(" r " + c.row + " c " + c.col);
  44. int temprow = i;
  45. int tempcol = j;
  46. while (true) {
  47.  
  48. for (int t = 0; t < temp.Count; t++)
  49. {
  50. if (!isVisited[temp[t].row, temp[t].col])
  51. {
  52. temprow = temp[t].row;
  53. tempcol = temp[t].col;
  54. break;
  55. }
  56. }
  57. if (((temprow - 1) >= 0) && ((temprow - 1) <l0) && (tempcol>=0) && (tempcol<l1))
  58. {
  59. //check to left
  60. if (matrix[temprow, tempcol] == matrix[temprow - 1, tempcol])
  61. {
  62. c.row = temprow - 1;
  63. c.col = tempcol;
  64. temp.Add(c);
  65. //Console.WriteLine(" r " + c.row + " c " + c.col);
  66. }
  67. }
  68. if (((temprow +1) >= 0) && ((temprow + 1) < l0) && (tempcol >= 0) && (tempcol < l1))
  69. {
  70. //check to right
  71. if (matrix[temprow, tempcol] == matrix[temprow + 1, tempcol])
  72. {
  73. c.row = temprow + 1;
  74. c.col = tempcol;
  75. temp.Add(c);
  76. //Console.WriteLine(" r " + c.row + " c " + c.col);
  77. }
  78. }
  79. if (((temprow ) >= 0) && ((temprow ) < l0) &&( (tempcol+1) >= 0) &&((tempcol+1) < l1))
  80. {
  81. //check down
  82. if (matrix[temprow, tempcol] == matrix[temprow, tempcol + 1])
  83. {
  84. c.col = tempcol +1;
  85. c.row = temprow;
  86. temp.Add(c);
  87. //Console.WriteLine(" r " + c.row + " c " + c.col);
  88. }
  89. }
  90. if (((temprow) >= 0) && ((temprow) < l0) && ((tempcol - 1) >= 0) && ((tempcol - 1) < l1))
  91. {
  92. //check up
  93. if (matrix[temprow, tempcol] == matrix[temprow, tempcol - 1])
  94. {
  95. c.col = tempcol - 1;
  96. c.row = temprow;
  97. temp.Add(c);
  98. // Console.WriteLine(" r " + c.row + " c " + c.col);
  99. }
  100. }
  101. isVisited[temprow, tempcol] = true;
  102. bool v = true;
  103. for (int t = 0; t < temp.Count; t++)
  104. {
  105. v = v && isVisited[temp[t].row, temp[t].col];
  106. }
  107. if (v) break;
  108. }
  109. //remove duplicated elements
  110. return temp.Distinct().Count();
  111. }
  112.  
  113. static void Main(string[] args)
  114. {
  115.  
  116. int l0=matrix.GetLength(0);
  117. int l1=matrix.GetLength(1);
  118. List<coord> result = new List<coord>();
  119. int bestResult = 0;
  120. int tempResult;
  121.  
  122. for (int i = 0; i < matrix.GetLength(0); i++)
  123. {
  124. for (int j = 0; j < matrix.GetLength(1); j++)
  125. {
  126. coord c = new coord();
  127. c.row = i;
  128. c.col = j;
  129.  
  130. if (!isVisited[i, j])//check what are the neighboring cells
  131. {
  132. tempResult= CheckEquals(i, j);
  133. Console.WriteLine("Temp result {0} for matrix element {1} ", tempResult,matrix[i,j]);
  134. if (tempResult > bestResult) bestResult = tempResult;
  135. }
  136. else
  137. {
  138. continue;
  139. }
  140. }
  141. }
  142.  
  143. Console.WriteLine("The best result is "+bestResult);
  144. }
  145. }
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement