stone_1.pas 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. Program Stone_Algorithm1;
  2. Const
  3. inf='stone.in';
  4. outf='stone.out';
  5. maxn=100;
  6. Var
  7. a,b:longint;
  8. ans:array[0..maxn,0..maxn]of longint;
  9. function dfs(a,b:longint):longint;
  10. var i,t:longint;
  11. begin
  12. if ans[a,b]<>0 then begin
  13. ans[b,a]:=ans[a,b];
  14. dfs:=ans[a,b]; exit;
  15. end;
  16. if ans[b,a]<>0 then begin
  17. ans[a,b]:=ans[b,a];
  18. dfs:=ans[a,b]; exit;
  19. end;
  20. for i:=1 to a do
  21. begin
  22. t:=dfs(a-i,b);
  23. if t=1 then begin
  24. ans[a,b]:=2; ans[b,a]:=2;
  25. dfs:=2;
  26. exit;
  27. end;
  28. end;
  29. for i:=1 to b do
  30. begin
  31. t:=dfs(a,b-i);
  32. if t=1 then begin
  33. ans[a,b]:=2; ans[b,a]:=2;
  34. dfs:=2;
  35. exit;
  36. end;
  37. end;
  38. for i:=1 to a do
  39. if i<=b then begin
  40. t:=dfs(a-i,b-i);
  41. if t=1 then begin
  42. ans[a,b]:=2; ans[b,a]:=2;
  43. dfs:=2;
  44. exit;
  45. end;
  46. end
  47. else break;
  48. dfs:=1;
  49. end;
  50. Begin
  51. assign(input,inf); reset(input);
  52. assign(output,outf); rewrite(output);
  53. fillchar(ans,sizeof(ans),0);
  54. ans[0,0]:=1;
  55. while not(seekeof) do
  56. begin
  57. read(a,b);
  58. ans[a,b]:=dfs(a,b);
  59. writeln(ans[a,b]-1);
  60. end;
  61. close(input); close(output);
  62. End.