Advertisement
Guest User

Untitled

a guest
Jul 21st, 2017
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.33 KB | None | 0 0
  1. 0 1 0 1 1 0
  2. 1 0 1 1 0 0
  3. 0 0 0 1 0 1
  4. 0 0 0 0 0 0
  5. 1 0 1 1 0 0
  6. 0 1 0 1 1 1
  7.  
  8. public static int isSink(int[][] mat) {
  9. int n = mat[0].length;
  10. boolean[] check = new boolean[n];
  11. for (int i = 0; i < n; i++)
  12. check[i] = true;
  13. for (int i = 0; i < n; i++)
  14. if (check[i]) {
  15. for (int j = 0; j < n; j++) {
  16. if (i != j)
  17. check[j] = false;
  18. else {
  19. check[i] = false;
  20. break;
  21. }
  22. if (i == j)
  23. continue;
  24. if (mat[j][i] == 1)
  25. check[j] = false;
  26. else {
  27. check[i] = false;
  28. break;
  29. }
  30. }
  31. if (check[i])
  32. return i;
  33. }
  34. return -1;
  35. }
  36.  
  37. public static int isSink(int[][] mat) {
  38. int rowSum = 0;
  39. int colSum = 0;
  40. int colHeight = mat[0].length;
  41.  
  42. int iterrationCounter = 0;
  43.  
  44. int rowIndex = -1;
  45. int colIndex = -1;
  46. for (int i = 0; i < mat.length; i++) {
  47. for (int j = 0; j < mat[i].length; j++) {
  48. // Находи суммы элементов строки(rowSum) и столбца(colSum)
  49. rowSum += mat[i][j];
  50. colSum += mat[j][i];
  51. iterrationCounter++;
  52. }
  53.  
  54.  
  55. // Ищем строку в которой все элементы равны 0
  56. // т.е. сумма элементов, такой строки, равна 0
  57. if (rowIndex == -1 && rowSum == 0) {
  58. rowIndex = i;
  59. } else {
  60. rowSum = 0;
  61. }
  62.  
  63. // Ищем столбец в котором все элементы, за исключением k, равны 1
  64. // т.е. сумма элементов, такого столбца, равна (высота столбца - 1)
  65. if (colIndex == -1 && colSum == colHeight - 1) {
  66. colIndex = i;
  67. } else {
  68. colSum = 0;
  69. }
  70. }
  71.  
  72. System.err.println(iterrationCounter);
  73.  
  74. if (rowIndex != -1 && colIndex != -1) {
  75. System.out.println("The sink is found by row:" + rowIndex + " column:" + colIndex);
  76. return mat[rowIndex][colIndex];
  77. }
  78.  
  79. return -1;
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement