1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
| import java.io.*; import java.util.*; public class Main { static boolean [][]used; static int ss,n,m; public static void main(String[] args) { FastScanner sc=new FastScanner(); PrintWriter pw=new PrintWriter(System.out); while(sc.hasNext()){ ss=sc.nextInt(); n=sc.nextInt(); m=sc.nextInt(); if(ss==0&&n==0&&m==0)break; used=new boolean[n+1][m+1]; if(ss%2!=0){ pw.println("NO"); }else{ int t=bfs(ss,0,0); if(t==-1)pw.println("NO"); else pw.println(t); } pw.flush(); } } static int bfs(int s,int a,int b){ Queue<Num>queue=new LinkedList<Num>(); used[a][b]=true; queue.add(new Num(s,a,b,0)); while(!queue.isEmpty()){ Num t=queue.poll(); if((t.b==ss/2&&t.a==0)||(t.a==ss/2&&t.b==0)||(t.a==ss/2&&t.s==0)) return t.bs; int sa=t.s+t.a; int dao=0; if(sa<=n){ dao=t.s; }else{ dao=n-t.a; } if(!used[t.a+dao][t.b]){ queue.add(new Num(t.s-dao,t.a+dao,t.b,t.bs+1)); used[t.a+dao][t.b]=true; } int sb=t.s+t.b; dao=0; if(sb<=m){ dao=t.s; }else{ dao=m-t.b; } if(!used[t.a][t.b+dao]){ queue.add(new Num(t.s-dao,t.a,t.b+dao,t.bs+1)); used[t.a][t.b+dao]=true; } int as=t.s+t.a; dao=0; if(as<=ss){ dao=t.a; }else{ dao=ss-t.s; } if(!used[t.a-dao][t.b]){ queue.add(new Num(t.s+dao,t.a-dao,t.b,t.bs+1)); used[t.a-dao][t.b]=true; } int bs=t.s+t.b; dao=0; if(bs<=ss){ dao=t.b; }else{ dao=ss-t.s; } if(!used[t.a][t.b-dao]){ queue.add(new Num(t.s+dao,t.a,t.b-dao,t.bs+1)); used[t.a][t.b-dao]=true; } int ab=t.a+t.b; dao=0; if(ab<=m){ dao=t.a; }else{ dao=m-t.b; } if(!used[t.a-dao][t.b+dao]){ queue.add(new Num(t.s,t.a-dao,t.b+dao,t.bs+1)); used[t.a-dao][t.b+dao]=true; } int ba=t.a+t.b; dao=0; if(ba<=n){ dao=t.b; }else{ dao=n-t.a; } if(!used[t.a+dao][t.b-dao]){ queue.add(new Num(t.s,t.a+dao,t.b-dao,t.bs+1)); used[t.a+dao][t.b-dao]=true; } } return -1; } } class Num{ int s,a,b,bs; Num(int s,int a,int b,int bs){ this.s=s; this.a=a; this.b=b; this.bs=bs; } }
|