比赛 20101117 评测结果 TTTETETTEA
题目名称 拯救 最终得分 10
用户昵称 苏轼 运行时间 0.000 s
代码语言 Pascal 内存使用 0.00 MiB
提交时间 2010-11-17 11:16:13
显示代码纯文本
  1. program savey(input,output);
  2.  
  3. type
  4. re=record
  5. deep,n:longint;
  6. end;
  7.  
  8. var
  9. n,i,len,dp,rec:longint;
  10. ch:char;
  11. boo:array[0..16777216]of boolean;
  12. q:array[1..1000000]of re;
  13.  
  14. function expand(a:re):boolean;
  15. var
  16. i,t:longint;
  17. begin
  18. t:=a.n xor (1 shl (n-1));
  19.  
  20. if t=0 then
  21. exit(true);
  22.  
  23. if (t<16777216) and not(boo[t]) then
  24. begin
  25. inc(len);
  26. boo[t]:=true;
  27. q[len].deep:=a.deep+1;
  28. q[len].n:=t;
  29. end;
  30.  
  31. for i:=n-1 downto 1 do
  32. if a.n shr i=1 then
  33. begin
  34. t:=a.n xor (1 shl (i-1));
  35.  
  36. if t=0 then
  37. exit(true);
  38.  
  39. if (t<16777216) and not(boo[t]) then
  40. begin
  41. inc(len);
  42. boo[t]:=true;
  43. q[len].deep:=a.deep+1;
  44. q[len].n:=t;
  45. end;
  46. end
  47. else if a.n shr i>1 then
  48. break;
  49.  
  50. exit(false);
  51. end;
  52.  
  53. begin
  54. assign(input,'savey.in');
  55. reset(input);
  56. assign(output,'savey.out');
  57. rewrite(output);
  58.  
  59. readln(n);
  60.  
  61. for i:=1 to n do
  62. begin
  63. read(ch);
  64. rec:=rec shl 1;
  65. if ch='1' then
  66. rec:=rec+1;
  67. read(ch);
  68. end;
  69.  
  70. if rec=0 then
  71. begin
  72. writeln(0);
  73. close(input);
  74. close(output);
  75. halt;
  76. end;
  77.  
  78. q[1].n:=rec;
  79. q[1].deep:=0;
  80. if (rec<16777216) then
  81. boo[rec]:=true;
  82. i:=1;
  83. dp:=0;
  84. len:=1;
  85. repeat
  86. while q[i].deep=dp do
  87. begin
  88. if expand(q[i]) then
  89. begin
  90. writeln(dp+1);
  91. close(input);
  92. close(output);
  93. halt;
  94. end;
  95. inc(i);
  96. end;
  97.  
  98. inc(dp);
  99. until 1=2;
  100. end.
  101.  
  102.