Swun question address:
Http: // 218.194.91.48/acmhome/problemdetail. do? & Method = showdetail & id = 1307
HDU question address: http://acm.hdu.edu.cn/showproblem.php? PID = 1, 1495
At first glance, it was quite difficult. In fact, it was the basic BFs, a trainer... I found a rule and wrote a-B-lengthCode~~
The Code should be well understood ~~
# Include <iostream> # include <cstring> using namespace STD; struct node {int S, N, M, T; node () {} node (int A, int B, int C, int d) {S = A, n = B, M = C, T = D;} que [400000]; bool vis [104] [104]; inline int add (int A, int R, int R) {return a + r <= r? A: r-r;} int BFS (int s, int N, int m) {If (S & 1) Return-1; int p, q, n, m, s, T; memset (VIS, 0, sizeof (VIS); que [p = q = 0] = node (S, 0, 0 ); while (P <= q) {n = que [p]. n, m = que [p]. m, S = que [p]. s, T = que [p]. t; P ++, t ++; If (vis [s] [N]) continue; vis [s] [N] = true; if (n = 0 & M = S | M = 0 & n = s) return T-1; que [+ q] = node (S-add (S, N, N), N + Add (S, N, N), M, t ); que [++ q] = node (S, N + Add (m, n, n), M-add (m, n, n), t ); que [++ q] = node (S, N-add (n, m, m), m + Add (n, m, m), t ); que [++ q] = node (S-add (S, M, m), n, m + Add (S, M, m), t ); que [++ q] = node (S + N, 0, M, T); que [++ q] = node (S + m, n, 0, t);} return-1;} int main () {int S, N, M, Res; while (CIN> S> N> M, S) {res = BFS (S, n, m); If (RES =-1) cout <"no" <Endl; else cout <res <Endl ;} return 0 ;}
This is written according to the regular expression of the table ~~
# Include <iostream> using namespace STD; int gcd (int A, int B) {If (B = 0) return a; return gcd (B, A % B );} int main () {int S, N, M; while (CIN> S> N> M, S) {S/= gcd (S, N ), m); If (S & 1) cout <"no" <Endl; else cout <S-1 <Endl;} return 0 ;}