fork download
  1. import java.util.*;
  2.  
  3. public class Main {
  4. public static class pair{
  5. int x;
  6. int y;
  7. public pair(int x,int y) {
  8. this.x=x;
  9. this.y=y;
  10. }
  11. }
  12.  
  13. public static void bfs(int n) {
  14. boolean check[][] = new boolean[3000][3000];
  15. Queue<pair> q = new LinkedList<>();
  16. int dist[][] = new int[3000][3000];
  17. pair p = new pair(1,0);
  18. q.add(p);
  19. check[p.x][p.y]=true;
  20. dist[1][0]=0;
  21. while(true) {
  22. pair k = q.poll();
  23. if(k.x==n)
  24. break;
  25. int x,y;
  26. x=k.x;
  27. y=k.y;
  28. pair temp;
  29. //클립보드에 복사
  30. if(k.x!=k.y) {
  31. if(check[k.x][k.x]!=true) {
  32. dist[k.x][k.x]=dist[k.x][k.y]+1;
  33. temp=new pair(x,x);
  34. q.add(temp);
  35. check[temp.x][temp.y]=true;
  36. }
  37. }
  38. //이모티콘 복사
  39. if(k.y!=0) {
  40. if(check[k.x+k.y][k.x]!=true) {
  41. dist[k.x+k.y][k.y]=dist[k.x][k.y]+1;
  42. temp=new pair(x+y,y);
  43. q.add(temp);
  44. }
  45. }
  46.  
  47. //이모티콘 1개 삭제
  48. if(k.x>1) {
  49. if(check[k.x-1][k.x]!=true) {
  50. dist[k.x-1][k.y]=dist[k.x][k.y]+1;
  51. temp=new pair(x-1,y);
  52. q.add(temp);
  53. }
  54. }
  55. }
  56. int min=99999;
  57. for(int i=0;i<3000;i++) {
  58. if(dist[n][i]<min && dist[n][i]!=0)
  59. min=dist[n][i];
  60. }
  61.  
  62. System.out.println(min);
  63. return;
  64. }
  65.  
  66. public static void main(String[] args) {
  67. Scanner sc = new Scanner(System.in);
  68. int n;
  69. n=sc.nextInt();
  70. bfs(n);
  71.  
  72. }
  73. }
Runtime error #stdin #stdout #stderr 0.24s 2184192KB
stdin
997
stdout
Standard output is empty
stderr
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3072
	at Main.bfs(Main.java:40)
	at Main.main(Main.java:70)