Advertisement
Guest User

Untitled

a guest
Dec 19th, 2014
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.58 KB | None | 0 0
  1. package adse8;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.InputStreamReader;
  5. import java.util.ArrayList;
  6. import java.util.LinkedList;
  7. import java.util.List;
  8.  
  9. class Graph<E> {
  10.  
  11. int num_nodes; // broj na jazli
  12. E nodes[]; // informacija vo jazlite - moze i ne mora?
  13. int adjMat[][]; // matrica na sosednost
  14.  
  15. @SuppressWarnings("unchecked")
  16. public Graph(int num_nodes) {
  17. this.num_nodes = num_nodes;
  18. nodes = (E[]) new Object[num_nodes];
  19. adjMat = new int[num_nodes][num_nodes];
  20.  
  21. for (int i = 0; i < this.num_nodes; i++)
  22. for (int j = 0; j < this.num_nodes; j++)
  23. adjMat[i][j] = 0;
  24. }
  25.  
  26. int adjacent(int x, int y) { // proveruva dali ima vrska od jazelot so
  27. // indeks x do jazelot so indeks y
  28. return (adjMat[x][y] != 0) ? 1 : 0;
  29. }
  30.  
  31. void addEdge(int x, int y) { // dodava vrska megu jazlite so indeksi x i y
  32. adjMat[x][y] = 1;
  33. adjMat[y][x] = 1;
  34. }
  35.  
  36. void deleteEdge(int x, int y) {
  37. // ja brise vrskata megu jazlite so indeksi x i y
  38. adjMat[x][y] = 0;
  39. adjMat[y][x] = 0;
  40. }
  41.  
  42. E get_node_value(int x) { // ja vraka informacijata vo jazelot so indeks x
  43. return nodes[x];
  44. }
  45.  
  46. void set_node_value(int x, E a) { // ja postavuva informacijata vo jazelot
  47. // so indeks na a
  48. nodes[x] = a;
  49. }
  50.  
  51. @Override
  52. public String toString() {
  53. String ret = " ";
  54. for (int i = 0; i < num_nodes; i++)
  55. ret += nodes[i] + " ";
  56. ret += "\n";
  57. for (int i = 0; i < num_nodes; i++) {
  58. ret += nodes[i] + " ";
  59. for (int j = 0; j < num_nodes; j++)
  60. ret += adjMat[i][j] + " ";
  61. ret += "\n";
  62. }
  63. return ret;
  64. }
  65.  
  66. public void printMatrix() {
  67. for (int i = 0; i < num_nodes; i++) {
  68. for (int j = 0; j < num_nodes; j++)
  69. System.out.print(adjMat[i][j] + " ");
  70. System.out.println();
  71. }
  72. }
  73.  
  74. public int pathCount(int nodeIndex,int pathLength){
  75. //vasiot kod tuka
  76. int current = nodeIndex;
  77. LinkedList<Integer> next = new LinkedList<Integer>();
  78. LinkedList<Integer> fin = new LinkedList<Integer>();
  79. LinkedList<Integer> temp = new LinkedList<Integer>();
  80. LinkedList<Integer> pop = new LinkedList<Integer>();
  81. pop.addLast(current);
  82. int n = 0;
  83. for(int i=0; i<pathLength; i++) {
  84. int x = pop.removeFirst();
  85. for(int j=0; j<num_nodes; j++) {
  86. if(this.adjacent(x, j) == 1) {
  87. next.addLast(j);
  88. System.out.println(next);
  89. }
  90. }
  91. if(i==pathLength-1) {
  92. for(int k=0; k<next.size(); k++)
  93. fin.addLast(next.removeFirst());
  94. System.out.printf("f: %s\n",fin);
  95. }
  96. else {
  97. if(!pop.isEmpty()) {
  98. for(int y=0; y<next.size()+2; y++)
  99. temp.addLast(next.removeFirst());
  100. System.out.printf("t: %s\n",temp);
  101. }else {
  102. for(int l=0; l<temp.size()+1; l++) {
  103. if(!temp.isEmpty())
  104. pop.addLast(temp.removeFirst());
  105. else
  106. break;
  107. }
  108. for(int z=0; z<next.size()+1; z++)
  109. pop.addLast(next.removeFirst());
  110. System.out.printf("p: %s\n",pop);
  111. }
  112. }
  113. }
  114. return fin.size();
  115. }
  116. }
  117.  
  118. public class PathCount {
  119.  
  120. public static void main(String[] args) throws Exception {
  121. BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  122. int N = Integer.parseInt(br.readLine());
  123. Graph<Integer> g=new Graph<>(N);
  124. int M = Integer.parseInt(br.readLine());
  125. for (int i=0;i<M;i++){
  126. String line=br.readLine();
  127. String edgeLine[]=line.split(" ");
  128. g.addEdge(Integer.parseInt(edgeLine[0]), Integer.parseInt(edgeLine[1]));
  129. }
  130. int nodeIndex=Integer.parseInt(br.readLine());
  131. int pathLength=Integer.parseInt(br.readLine());
  132. System.out.println(g.pathCount(nodeIndex, pathLength));
  133. br.close();
  134.  
  135. }
  136.  
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement