Advertisement
Guest User

Untitled

a guest
May 24th, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.78 KB | None | 0 0
  1. Skip to content
  2. Why GitHub?
  3. Enterprise
  4. Explore
  5. Marketplace
  6. Pricing
  7. Search
  8.  
  9. Sign in
  10. Sign up
  11. 0 0 0 T-B00/TGH-SegmentaceObrazu
  12. Code Issues 0 Pull requests 0 Projects 0 Insights
  13. Join GitHub today
  14. GitHub is home to over 36 million developers working together to host and review code, manage projects, and build software together.
  15.  
  16. TGH-SegmentaceObrazu/src/segmentaceobrazu/main.java
  17. @T-B00 T-B00 Add files via upload
  18. b60facd on 27 Jun 2018
  19. 147 lines (130 sloc) 4.29 KB
  20.  
  21. package segmentaceobrazu;
  22.  
  23. import java.io.BufferedReader;
  24. import java.io.File;
  25. import java.io.FileReader;
  26. import java.io.IOException;
  27. import java.io.InputStreamReader;
  28. import static java.lang.Math.abs;
  29. import static java.lang.Math.abs;
  30.  
  31. public class main {
  32. static int[] cesta;
  33. public static int[] komponenty;
  34. static int velikost, pocetVrcholu, pocetHran, i, j;
  35.  
  36. public static void main(String args[]) throws IOException {
  37. int[] pole;
  38.  
  39. File f = new File("a.txt");
  40.  
  41. BufferedReader in = new BufferedReader(new FileReader(f));
  42. //BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
  43. String line = in.readLine();// preskoceni radku
  44. velikost = Integer.parseInt(line);
  45. pole = new int[velikost * velikost];
  46. int index = 0;
  47. while ((line = in.readLine()) != null) {
  48. String[] casti = line.split(" ");
  49. for (int i = 0; i < velikost; i++) {
  50. pole[index] = Integer.parseInt(casti[i]);
  51. index++;
  52. }
  53. }
  54. pocetVrcholu = velikost * velikost;
  55.  
  56. komponenty = new int[pocetVrcholu];
  57. for (int i = 0; i < komponenty.length; i++) {
  58. komponenty[i] = i;
  59. }
  60.  
  61. pocetVrcholu = velikost * velikost;
  62. pocetHran = (velikost + velikost) * (velikost - 1);
  63. cesta = new int[pocetVrcholu + 1];
  64. Hrana e[] = new Hrana[pocetHran];
  65. Hrana t = new Hrana();
  66. int hrana = 0;
  67. for (int i = 1; i < pole.length; i++) {
  68. if (i % velikost != 0) {
  69. e[hrana] = new Hrana();
  70. e[hrana].odkud = i - 1;
  71. e[hrana].kam = i;
  72. e[hrana].vaha = abs(pole[i] - pole[i - 1]);
  73. hrana++;
  74. }
  75. if (i >= velikost) {
  76. e[hrana] = new Hrana();
  77. e[hrana].odkud = i - velikost;
  78. e[hrana].kam = i;
  79. e[hrana].vaha = abs(pole[i] - pole[i - velikost]);
  80. hrana++;
  81. }
  82. }
  83.  
  84. // Bubble sort
  85. for (i = 0; i <= pocetHran - 1; i++) {
  86. for (j = 0; j < pocetHran - i - 1; j++) {
  87. if (e[j].vaha > e[j + 1].vaha) {
  88. t = e[j];
  89. e[j] = e[j + 1];
  90. e[j + 1] = t;
  91. }
  92. }
  93. }
  94.  
  95. for (i = 0; i < pocetVrcholu; i++) {
  96. cesta[i] = 0;
  97. }
  98.  
  99. i = 0;
  100. j = 0;
  101.  
  102. Hrana kostra[] = new Hrana[pocetVrcholu - 1];
  103. int max = 0;
  104. while ((i != pocetVrcholu - 1) && (j != pocetHran)) {
  105. if (jeCyklus(e[j])) {
  106. kostra[i] = new Hrana();
  107. kostra[i].odkud = e[j].odkud;
  108. kostra[i].kam = e[j].kam;
  109. kostra[i].vaha = e[j].vaha;
  110. max = e[j].vaha;
  111. i++;
  112. }
  113. j++;
  114. }
  115.  
  116. for (int i = 0; i < kostra.length; i++) {
  117. if (max != kostra[i].vaha) {
  118. pridatHranu(kostra[i].odkud, kostra[i].kam);
  119. }
  120. }
  121. Vypis(komponenty, velikost);
  122. }
  123.  
  124. public static boolean jeCyklus(Hrana e) {
  125. int u = e.odkud, v = e.kam;
  126. while (cesta[u] > 0) {
  127. u = cesta[u];
  128. }
  129. while (cesta[v] > 0) {
  130. v = cesta[v];
  131. }
  132. if (u != v) {
  133. cesta[u] = v;
  134. return true;
  135. }
  136. return false;
  137. }
  138.  
  139. static class Hrana {
  140. int odkud, kam, vaha;
  141. }
  142.  
  143. public static void Vypis(int[] pole, int delka) {
  144. System.out.println(delka);
  145. for (int i = 0; i < pole.length; i++) {
  146. if (pole[i] == 0) {
  147. System.out.printf(" 0 ");
  148. } else {
  149. System.out.printf(" 1 ");
  150. }
  151. if ((i + 1) % velikost == 0) {
  152. System.out.println("");
  153. }
  154. }
  155. }
  156.  
  157. public static void pridatHranu(int index1, int index2) {
  158. int temp = komponenty[index1];
  159. int temp2 = komponenty[index2];
  160. for (int i = 0; i < komponenty.length; i++) {
  161. if (komponenty[i] == temp2) {
  162. komponenty[i] = temp;
  163. }
  164. }
  165. }
  166. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement