fork download
  1. package ac;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Arrays;
  5. import java.util.Iterator;
  6. import java.util.LinkedList;
  7. import java.util.Queue;
  8.  
  9. public class man3 {
  10.  
  11. static int bien;
  12. static ArrayList<Integer> ds[];
  13.  
  14. public man3(int v)
  15. {
  16. bien = v;
  17. ds = new ArrayList[v];
  18. for(int i=0;i<v;i++)
  19. {
  20. ds[i] = new ArrayList<>();
  21. }
  22. }
  23. int bfs(int a, int b) {
  24. int loc0[] = new int[bien+1];
  25. Arrays.fill(loc0, 0);
  26. Queue<Integer> abcd= new LinkedList<>();
  27. // hang doi
  28. loc0[a]= 1;
  29. abcd.add(a);
  30.  
  31. while(!abcd.isEmpty())
  32. {
  33. int p =abcd.poll();
  34. Iterator<Integer> it = ds[p].iterator();
  35. while(it.hasNext())
  36. {
  37. int tt = it.next();
  38. if(loc0[tt]==0)
  39. {
  40. loc0[tt] = loc0[p]+1;
  41. abcd.add(tt);
  42. }
  43. if(tt == b)
  44. {
  45. return loc0[tt]-1;
  46. }
  47.  
  48. }
  49.  
  50.  
  51. }
  52. //System.out.println("X");
  53. return 0;
  54. }
  55. void them(int u, int v)
  56. {
  57. ds[u].add(v);
  58. ds[v].add(u);
  59. }
  60. static boolean sosanh(int a, int b)
  61. {
  62. char[]s1=(""+a).toCharArray();
  63. char[] s2 = (""+b).toCharArray();
  64. int c = 0;
  65. if(s1[0]!=s2[0])
  66. c++;
  67. if(s1[1]!=s2[1])
  68. c++;
  69. if(s1[2]!=s2[2])
  70. c++;
  71. if(s1[3]!= s2[3])
  72. c++;
  73. return (c==1);
  74. }
  75. static void dsonguyentoco4chuso(ArrayList<Integer> danhsachnt) {
  76. int n=9999;
  77. boolean snt[]=new boolean[n+1];
  78. Arrays.fill(snt,true);
  79.  
  80. for (int p=2;p*p<=n;p++) {
  81. if (snt[p]==true) {
  82. for (int i=p*p; i<=n; i+=p)
  83. snt[i]= false;
  84. }
  85. }
  86. for (int p=1000;p<=n;p++)
  87. if (snt[p])
  88. danhsachnt.add(p);
  89.  
  90. // System.out.println(danhsachnt);
  91. }
  92. static int tinhbuocnhay(int a, int b) {
  93. ArrayList<Integer> layvao=new ArrayList<>();
  94. dsonguyentoco4chuso(layvao);
  95. man3 g = new man3(layvao.size());
  96. for (int i=0; i <layvao.size(); i++)
  97. for (int j =i+1; j <layvao.size(); j++)
  98. if (sosanh(layvao.get(i), layvao.get(j)))
  99. {
  100. g.them(i, j);
  101. }
  102.  
  103. int in1 = 0;
  104. int in2 = 0;
  105. for (int j=0; j<layvao.size();j++)
  106. if (layvao.get(j) == a)
  107. in1=j;
  108. for (int j=0; j<layvao.size();j++)
  109. if (layvao.get(j) == b)
  110. in2=j;
  111. // System.out.println(layvao);
  112.  
  113. return g.bfs(in1,in2);
  114. }
  115.  
  116.  
  117. public static void main(String[] args) {
  118. int a =1033;
  119. int b =8179;
  120. int in=tinhbuocnhay(a, b);
  121. if(in==0) System.out.println("Imposible");
  122. else
  123. System.out.println(in);
  124. }
  125. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
Main.java:9: error: class man3 is public, should be declared in a file named man3.java
public class man3 {
       ^
Note: Main.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
1 error
stdout
Standard output is empty