Advertisement
Guest User

Untitled

a guest
Jul 16th, 2012
302
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 27.54 KB | None | 0 0
  1. {Ww}
  2. uses sysutils,math;
  3.  
  4. const isfile='beard4.map';
  5.  
  6. const zzc = 20000;
  7. const size = 500;
  8. const maxiter = 5000;
  9. const lfsmax = 30;
  10.  
  11.  
  12. var ourd,numdest,numsour,ppo:longint;
  13. var oppp:longint;
  14. var sourcesx,sourcesy:array[1..10] of longint;
  15. var destinationsx,destinationsy:array[1..10] of longint;
  16.  
  17. var ppp,numt:longint;
  18.  
  19. var teee:longint;
  20. var xto,xfrom,yto,yfrom:longint;
  21. var ii,jj:longint;
  22.  
  23. var sch:char;
  24. var za,sc:longint;
  25.  
  26. var stepiter:longint;
  27. var onceadd:boolean;
  28.  
  29. var ctime:TDateTime;
  30. var tst:longint;
  31.  
  32. var nera,nepp,tera,pp:longint;
  33.  
  34. var checked:boolean;
  35. var endd:boolean;
  36. var lose:boolean;
  37. var appended:boolean;
  38.  
  39. var bestres:string;
  40. var mincryst:longint;
  41. var i,j:longint;
  42. var reshenie,restemp:widestring;
  43.  
  44. var maze:array[1..size,1..size] of char;
  45. var umaze:array[1..size,1..size] of char;
  46. var tmaze:array[1..size,1..size] of longint;
  47.  
  48. var mazes:array[1..maxiter+3,1..size,1..size] of char;
  49. var kristall:array[1..maxiter+3] of longint;
  50. var opened:array[1..maxiter+3] of boolean;
  51. var changed:array[1..maxiter+3] of boolean;
  52. var poradok:array[1..maxiter+3] of string;
  53. var iter:array[1..maxiter+3] of longint;
  54. var crystalsx:array[1..size*size] of longint;
  55. var crystalsy:array[1..size*size] of longint;
  56. var uaternou:array[1..maxiter+3] of longint;
  57. var uater, flooding, uaterproof:longint;
  58. var trampo:array[1..maxiter+3,1..10] of longint;
  59.  
  60. var leng:array[1..size] of longint;
  61. var totalcrystall:longint;
  62. var maxleng:longint;
  63. var tc:char;
  64. var ts:string;
  65. var needtocheck:longint;
  66.  
  67. var curx,cury:longint;
  68. var maxx,maxy:longint;
  69. var robotx,roboty:longint;
  70. var exitx,exity:longint;
  71. var ccx,ccy:longint;
  72. var male:longint;
  73. var mara,tmara:longint;
  74.  
  75. var ocheredx,ocheredy:array[1..size*size] of longint;
  76. var ocl,ocr:longint;
  77. var pix,pjx:longint;
  78. var finished,empty:boolean;
  79. var tex,tey,culevel:longint;
  80. var tcx:char;
  81. var ishzad,uluzad,xuluzad,ulux,uluy:longint;
  82.  
  83. procedure checkteleport(curiter:longint);
  84. var ppp:longint;
  85. begin
  86. {check teleporting}
  87. for ppp:=1 to 10 do
  88. begin
  89.  
  90. if (robotx=sourcesx[ppp]) and
  91. (roboty=sourcesy[ppp]) and
  92. (trampo[curiter,ppp]<>0) then
  93. begin
  94. ourd:=trampo[curiter,ppp];
  95. for oppp:=1 to 10 do
  96. begin
  97. if trampo[curiter,oppp]=ourd then
  98. begin
  99. trampo[curiter,oppp]:=0;
  100. end;
  101. end;
  102. maze[sourcesx[ourd],sourcesy[ourd]]:=' ';
  103. maze[destinationsx[ourd],destinationsy[ourd]]:='R';
  104. robotx:=destinationsx[ourd];
  105. roboty:=destinationsy[ourd];
  106. end;
  107. end;
  108. end;
  109.  
  110. function lfs(nepp:longint):char;
  111. var blilevel,blix,bliy:longint;
  112. var tepe:char;
  113. begin
  114. blix:=0;
  115. bliy:=0;
  116.  
  117. lfs:='N';
  118.  
  119. for pix:=1 to maxx do
  120. for pjx:=1 to maxy do
  121. tmaze[pix,pjx]:=-1;
  122.  
  123. ishzad:=trunc(sqrt(sqr(robotx-crystalsx[nepp])+sqr(roboty-crystalsy[nepp])));
  124. uluzad:=ishzad;
  125. ulux:=0; uluy:=0;
  126.  
  127. tmaze[robotx,roboty]:=1;
  128. {tmaze[crystalsx[nepp],crystalsy[nepp]]:=0;}
  129. ocl:=1;ocr:=2;
  130. ocheredx[1]:=robotx;
  131. ocheredy[1]:=roboty;
  132. finished:=false;
  133. empty:=false;
  134.  
  135. repeat
  136. {get next}
  137. tex:=ocheredx[ocl];
  138. tey:=ocheredy[ocl];
  139. inc(ocl);if ocl>ocr then empty:=true;
  140. if ocl=size*size then ocl:=1;
  141. if ocr=size*size then ocr:=1;
  142.  
  143. if empty=false then
  144. begin
  145.  
  146. culevel:=tmaze[tex,tey];
  147. if culevel<lfsmax then
  148. begin
  149. if (tex<>1) then if (tmaze[tex-1,tey]=-1)
  150. and ((maze[tex-1,tey]=' ')
  151. or (maze[tex-1,tey]='.')
  152. or (maze[tex-1,tey]='\')) then
  153. begin
  154. if (tex-1=crystalsx[nepp]) and (tey=crystalsy[nepp]) then
  155. finished:=true;
  156. tmaze[tex-1,tey]:=culevel+1;
  157. ocheredx[ocr]:=tex-1;
  158. ocheredy[ocr]:=tey;
  159. xuluzad:=trunc(sqrt(sqr(tex-1-crystalsx[nepp])+sqr(tey-crystalsy[nepp])));
  160. if xuluzad<uluzad then
  161. begin
  162. uluzad:=xuluzad;
  163. ulux:=tex-1;
  164. uluy:=tey;
  165. end;
  166. if (maze[tex-1,tey]='\') and
  167. (culevel<blilevel) then begin
  168. blix:=tex-1; bliy:=tey; blilevel:=culevel; end;
  169. inc(ocr);if ocr=size*size then ocr:=1;
  170. end;
  171. if (tex<>maxx) then if (tmaze[tex+1,tey]=-1)
  172. and ((maze[tex+1,tey]=' ')
  173. or (maze[tex+1,tey]='.')
  174. or (maze[tex+1,tey]='\')) then
  175. begin
  176. if (tex+1=crystalsx[nepp]) and (tey=crystalsy[nepp]) then
  177. finished:=true;
  178. tmaze[tex+1,tey]:=culevel+1;
  179. ocheredx[ocr]:=tex+1;
  180. ocheredy[ocr]:=tey;
  181. xuluzad:=trunc(sqrt(sqr(tex+1-crystalsx[nepp])+sqr(tey-crystalsy[nepp])));
  182. if xuluzad<uluzad then
  183. begin
  184. uluzad:=xuluzad;
  185. ulux:=tex+1;
  186. uluy:=tey;
  187. end;
  188. if (maze[tex+1,tey]='\') and
  189. (culevel<blilevel) then begin
  190. blix:=tex+1; bliy:=tey; blilevel:=culevel; end;
  191. inc(ocr);if ocr=size*size then ocr:=1;
  192. end;
  193. if (tey<>1) then if (tmaze[tex,tey-1]=-1)
  194. and ((maze[tex,tey-1]=' ')
  195. or (maze[tex,tey-1]='.')
  196. or (maze[tex,tey-1]='\')) then
  197. begin
  198. if (tey-1=crystalsy[nepp]) and (tex=crystalsx[nepp]) then
  199. finished:=true;
  200. tmaze[tex,tey-1]:=culevel+1;
  201. ocheredx[ocr]:=tex;
  202. ocheredy[ocr]:=tey-1;
  203. xuluzad:=trunc(sqrt(sqr(tex-crystalsx[nepp])+sqr(tey-1-crystalsy[nepp])));
  204. if xuluzad<uluzad then
  205. begin
  206. uluzad:=xuluzad;
  207. ulux:=tex;
  208. uluy:=tey-1;
  209. end;
  210. if (maze[tex,tey-1]='\') and
  211. (culevel<blilevel) then begin
  212. blix:=tex; bliy:=tey-1; blilevel:=culevel; end;
  213. inc(ocr);if ocr=size*size then ocr:=1;
  214. end;
  215. if (tey<>maxy) then if (tmaze[tex,tey+1]=-1)
  216. and ((maze[tex,tey+1]=' ')
  217. or (maze[tex,tey+1]='.')
  218. or (maze[tex,tey+1]='\')) then
  219. begin
  220. if (tey+1=crystalsy[nepp]) and (tex=crystalsx[nepp]) then
  221. finished:=true;
  222. tmaze[tex,tey+1]:=culevel+1;
  223. ocheredx[ocr]:=tex;
  224. ocheredy[ocr]:=tey+1;
  225. xuluzad:=trunc(sqrt(sqr(tex-crystalsx[nepp])+sqr(tey+1-crystalsy[nepp])));
  226. if xuluzad<uluzad then
  227. begin
  228. uluzad:=xuluzad;
  229. ulux:=tex;
  230. uluy:=tey+1;
  231. end;
  232. if (maze[tex,tey+1]='\') and
  233. (culevel<blilevel) then begin
  234. blix:=tex; bliy:=tey+1; blilevel:=culevel; end;
  235. inc(ocr);if ocr=size*size then ocr:=1;
  236. end;
  237. end;
  238. end
  239. else
  240. begin
  241. finished:=true;
  242. if uluzad<ishzad then lfs:='M' else lfs:=' ';
  243. end;
  244.  
  245. until finished;
  246.  
  247. if (lfs='N') or (lfs='M') then
  248. begin
  249. if lfs='N' then
  250. begin
  251. ccx:=crystalsx[nepp];
  252. ccy:=crystalsy[nepp];
  253. male:=tmaze[ccx,ccy];
  254. end;
  255. if lfs='M' then
  256. begin
  257. ccx:=ulux;
  258. ccy:=uluy;
  259. male:=tmaze[ccx,ccy];
  260. end;
  261.  
  262. if male>2 then begin
  263. repeat
  264.  
  265. if (ccx<>1) and (tmaze[ccx-1,ccy]=male-1) then
  266. begin
  267. dec(ccx);
  268. dec(male);
  269. end
  270. else
  271. if (ccx<>maxx) and (tmaze[ccx+1,ccy]=male-1) then
  272. begin
  273. inc(ccx);
  274. dec(male);
  275. end
  276. else
  277. if (ccy<>1) and (tmaze[ccx,ccy-1]=male-1) then
  278. begin
  279. dec(ccy);
  280. dec(male);
  281. end
  282. else
  283. if (ccy<>maxy) and (tmaze[ccx,ccy+1]=male-1) then
  284. begin
  285. inc(ccy);
  286. dec(male);
  287. end;
  288.  
  289. until male=2;
  290. end;
  291.  
  292. if ccx=robotx-1 then lfs:='L';
  293. if ccx=robotx+1 then lfs:='R';
  294. if ccy=roboty-1 then lfs:='U';
  295. if ccy=roboty+1 then lfs:='D';
  296. end;
  297.  
  298. if length(reshenie)=132 then
  299. begin
  300. teee:=0;
  301. end;
  302.  
  303. teee:=0;
  304. end;
  305.  
  306. function zread(l:integer):longint;
  307. var xi:longint;
  308. var xxs:string;
  309. begin
  310. xxs:='';
  311. for xi:=l to length(ts) do
  312. if (ts[xi]>='0') and (ts[xi]<='9') then xxs:=xxs+ts[xi];
  313. val(xxs,xi);
  314. zread:=xi;
  315. end;
  316.  
  317. function movefront(poradok:string;c:char):string;
  318. var cx,po:longint;
  319. var tss:string;
  320. begin
  321. for cx:=1 to length(poradok) do
  322. begin
  323. if poradok[cx]=c then po:=cx;
  324. end;
  325.  
  326. tss:=c;
  327. for cx:=1 to po-1 do tss:=tss+poradok[cx];
  328. for cx:=po+1 to length(poradok) do tss:=tss+poradok[cx];
  329.  
  330. movefront:=tss;
  331.  
  332. end;
  333.  
  334. {procedure outres;
  335. var i,j:longint;
  336. begin
  337. writeln;
  338. for i:=1 to maxy do
  339. begin
  340. for j:=1 to maxx do
  341. begin
  342. write(maze[j,i]);
  343. end;
  344. writeln;
  345. end;
  346. end;}
  347.  
  348. procedure updatefield(curiter:longint);
  349. var px,py:longint;
  350. begin
  351.  
  352. for py:=1 to maxy do
  353. for px:=1 to maxx do
  354. begin
  355. umaze[px,py]:=maze[px,py];
  356. end;
  357.  
  358. changed[curiter]:=false;
  359.  
  360. for py:=1 to maxy do
  361. for px:=1 to maxx do
  362. begin
  363.  
  364. if maze[px,py]='*' then
  365. begin
  366. if py<>maxy then
  367. begin
  368. if maze[px,py+1]=' ' then
  369. begin
  370. umaze[px,py]:=' ';
  371. umaze[px,py+1]:='*';
  372. changed[curiter]:=true;
  373. end;
  374. end;
  375.  
  376. if (py<>maxy) and (px<>maxx) then
  377. begin
  378. if (maze[px,py+1]='*') or
  379. (maze[px,py+1]='\') then
  380. begin
  381. if (maze[px+1,py]=' ') and
  382. (maze[px+1,py+1]=' ') then
  383. begin
  384. umaze[px,py]:=' ';
  385. umaze[px+1,py+1]:='*';
  386. changed[curiter]:=true;
  387. end
  388. else
  389. if (maze[px-1,py]=' ') and
  390. (maze[px-1,py+1]=' ') and
  391. (maze[px,py+1]='*') then
  392. begin
  393. umaze[px,py]:=' ';
  394. umaze[px-1,py+1]:='*';
  395. changed[curiter]:=true;
  396. end;
  397. end;
  398. end;
  399. end;
  400. end;
  401.  
  402. if changed[curiter] then
  403. begin
  404.  
  405. if (roboty>1) and
  406. (maze[robotx,roboty-1]<>'*') and (umaze[robotx,roboty-1]='*') then
  407. lose:=true;
  408.  
  409. for py:=1 to maxy do
  410. for px:=1 to maxx do
  411. begin
  412. maze[px,py]:=umaze[px,py];
  413. end;
  414. end;
  415.  
  416. end;
  417.  
  418.  
  419. procedure generateanswer(iteration:longint);
  420. var curiter:longint;
  421. var px,py:longint;
  422. var needtocheck:longint;
  423. begin
  424.  
  425. if TimeStamptoMSecs(DateTimeToTimestamp(now))-ctime<zzc then begin
  426.  
  427. curiter:=iteration+1;
  428.  
  429. if (curiter<>1) then
  430. for px:=1 to 10 do
  431. trampo[curiter,px]:=trampo[curiter-1,px];
  432.  
  433. if (curiter<>1) then
  434. begin
  435. if uater+floor(curiter/flooding)>=maxy-roboty then
  436. uaternou[curiter]:=uaternou[curiter-1]+1 else
  437. uaternou[curiter]:=0;
  438. end;
  439. if uaternou[curiter]>=uaterproof then
  440. begin
  441. lose:=true;
  442. end;
  443.  
  444.  
  445. if (curiter<>1) and (lose=false) then
  446. begin
  447. if kristall[curiter-1]<mincryst then
  448. begin
  449. bestres:=reshenie;
  450. mincryst:=kristall[curiter-1];
  451. {writeln('New best: ',bestres);}
  452. end;
  453. if (kristall[curiter-1]=mincryst) and (length(reshenie)<length(bestres)) then
  454. begin
  455. bestres:=reshenie;
  456. mincryst:=kristall[curiter-1];
  457. {writeln('New best: ',bestres);}
  458. end;
  459. end;
  460.  
  461.  
  462. {outres;
  463. writeln(reshenie);
  464. if curiter<>1 then writeln(kristall[curiter-1]);}
  465. {readln;}
  466.  
  467. if curiter>2 then
  468. begin
  469. if kristall[curiter-2]>kristall[curiter-1] then
  470. begin
  471. iter[curiter]:=iter[curiter-1]+stepiter;
  472. if iter[curiter]>maxiter-10 then
  473. begin
  474. iter[curiter]:=maxiter-10;
  475. end;
  476. end
  477. else
  478. iter[curiter]:=iter[curiter-1];
  479. end;
  480.  
  481. if curiter<iter[curiter] then
  482. if lose<>true then
  483. begin
  484.  
  485. lose:=false;
  486. for py:=1 to maxy do
  487. for px:=1 to maxx do
  488. begin
  489. mazes[curiter,px,py]:=maze[px,py];
  490. end;
  491. {check last iter}
  492. if curiter<>1 then
  493. begin
  494. if mazes[curiter-1,robotx,roboty]='\' then
  495. kristall[curiter]:=kristall[curiter-1]-1 else
  496. kristall[curiter]:=kristall[curiter-1];
  497. end
  498. else kristall[curiter]:=totalcrystall;
  499.  
  500. {may _e at exit?}
  501.  
  502. if curiter<>1 then
  503. begin
  504. if kristall[curiter]=0 then
  505. begin
  506. if (exitx=robotx) and (exity=roboty-1) then
  507. begin
  508. reshenie:=reshenie+'U';
  509. bestres:=reshenie;
  510. endd:=true;
  511. end;
  512. if (exitx=robotx) and (exity=roboty+1) then
  513. begin
  514. reshenie:=reshenie+'D';
  515. bestres:=reshenie;
  516. endd:=true;
  517. end;
  518. if (exitx=robotx-1) and (exity=roboty) then
  519. begin
  520. reshenie:=reshenie+'L';
  521. bestres:=reshenie;
  522. endd:=true;
  523. end;
  524. if (exitx=robotx+1) and (exity=roboty) then
  525. begin
  526. reshenie:=reshenie+'R';
  527. bestres:=reshenie;
  528. endd:=true;
  529. end;
  530. end;
  531. end;
  532.  
  533. if endd then exit;
  534.  
  535. poradok[curiter]:='UDLRW';
  536. {optimize movements}
  537. {not repeat}
  538. for za:=1 to 4 do
  539. begin
  540. sc:=random(4)+1;
  541. sch:=poradok[curiter][sc];
  542. poradok[curiter]:=movefront(poradok[curiter],sch);
  543. end;
  544.  
  545. {find near-crystals}
  546. nera:=100000;
  547. mara:=100000;
  548. nepp:=0;
  549. for pp:=1 to totalcrystall do
  550. begin
  551. if maze[crystalsx[pp],crystalsy[pp]]='\' then
  552. {do not _ant crystal and rock above}
  553. if (crystalsy[pp]>=roboty) or (crystalsy[pp]=1) or
  554. ((crystalsy[pp]<>1) and
  555. (maze[crystalsx[pp],crystalsy[pp]-1]<>'*')) then
  556. begin
  557.  
  558. if crystalsx[pp]>robotx then
  559. begin
  560. xfrom:=robotx;
  561. xto:=crystalsx[pp];
  562. end
  563. else
  564. begin
  565. xfrom:=crystalsx[pp];
  566. xto:=robotx;
  567. end;
  568. if crystalsy[pp]>roboty then
  569. begin
  570. yfrom:=roboty;
  571. yto:=crystalsy[pp];
  572. end
  573. else
  574. begin
  575. yfrom:=crystalsy[pp];
  576. yto:=roboty;
  577. end;
  578.  
  579. tera:=0;
  580. for ii:=xfrom to xto do
  581. for jj:=yfrom to yto do
  582. begin
  583. if (maze[ii,jj]='#') or
  584. (maze[ii,jj]='*') then inc (tera);
  585. end;
  586.  
  587. if tera<nera then
  588. begin
  589. mara:=trunc(sqrt(sqr(robotx-crystalsx[pp])+sqr(roboty-crystalsy[pp])));
  590. nera:=tera;
  591. nepp:=pp;
  592. end
  593. else
  594. if tera=nera then
  595. begin
  596. tmara:=trunc(sqrt(sqr(robotx-crystalsx[pp])+sqr(roboty-crystalsy[pp])));
  597. if tmara<mara then
  598. begin
  599. mara:=tmara;
  600. nepp:=pp;
  601. end;
  602. end;
  603. end;
  604. end;
  605.  
  606. if nepp<>0 then
  607. begin
  608. tcx:=lfs(nepp);
  609. if (tcx<>' ') and (uater=0) and (flooding=100000) then
  610. poradok[curiter]:=movefront(poradok[curiter],tcx)
  611. else begin
  612. if crystalsx[nepp]<robotx then
  613. poradok[curiter]:=movefront(poradok[curiter],'L');
  614. if crystalsx[nepp]>robotx then
  615. poradok[curiter]:=movefront(poradok[curiter],'R');
  616. if crystalsy[nepp]<roboty then
  617. poradok[curiter]:=movefront(poradok[curiter],'U');
  618. if crystalsy[nepp]>roboty then
  619. poradok[curiter]:=movefront(poradok[curiter],'D');
  620. end;
  621. end;
  622.  
  623. {nearest crystals}
  624. if (robotx<>1) and (maze[robotx-1,roboty]='\') then
  625. poradok[curiter]:=movefront(poradok[curiter],'L');
  626. if (robotx<>maxx) and (maze[robotx+1,roboty]='\') then
  627. poradok[curiter]:=movefront(poradok[curiter],'R');
  628. if (roboty<>1) and (maze[robotx,roboty-1]='\') then
  629. poradok[curiter]:=movefront(poradok[curiter],'U');
  630. if (roboty<>maxy) and (maze[robotx,roboty+1]='\') then
  631. poradok[curiter]:=movefront(poradok[curiter],'D');
  632.  
  633. {if need to exit}
  634. if kristall[curiter]=0 then
  635. begin
  636. if exitx<robotx then
  637. poradok[curiter]:=movefront(poradok[curiter],'L');
  638. if exitx>robotx then
  639. poradok[curiter]:=movefront(poradok[curiter],'R');
  640. if exity<roboty then
  641. poradok[curiter]:=movefront(poradok[curiter],'U');
  642. if exity>roboty then
  643. poradok[curiter]:=movefront(poradok[curiter],'D');
  644. end;
  645.  
  646. for needtocheck:=1 to 5 do
  647. begin
  648.  
  649. {up check}
  650. if (roboty<>1) and (poradok[curiter][needtocheck]='U') then
  651. if (maze[robotx,roboty-1]=' ') or
  652. (maze[robotx,roboty-1]='.') or
  653. (maze[robotx,roboty-1]='\') {or open lambda lift}
  654. then begin
  655. maze[robotx,roboty]:=' ';
  656. dec(roboty);
  657. maze[robotx,roboty]:='R';
  658. checkteleport(curiter);
  659. updatefield(curiter);
  660. reshenie:=reshenie+'U';
  661. generateanswer(curiter);
  662. end;
  663.  
  664. {do_n check}
  665. if (roboty<>maxy) and (poradok[curiter][needtocheck]='D') then
  666. if (maze[robotx,roboty+1]=' ') or
  667. (maze[robotx,roboty+1]='.') or
  668. (maze[robotx,roboty+1]='\') {or open lambda lift}
  669. then begin
  670. maze[robotx,roboty]:=' ';
  671. inc(roboty);
  672. maze[robotx,roboty]:='R';
  673. checkteleport(curiter);
  674. updatefield(curiter);
  675. reshenie:=reshenie+'D';
  676. generateanswer(curiter);
  677. end;
  678. {left check}
  679. if (robotx<>1) and (poradok[curiter][needtocheck]='L') then
  680. if (maze[robotx-1,roboty]=' ') or
  681. (maze[robotx-1,roboty]='.') or
  682. (maze[robotx-1,roboty]='\') {or open lambda lift}
  683. then begin
  684. maze[robotx,roboty]:=' ';
  685. dec(robotx);
  686. maze[robotx,roboty]:='R';
  687. checkteleport(curiter);
  688. updatefield(curiter);
  689. reshenie:=reshenie+'L';
  690. generateanswer(curiter);
  691. end
  692. else
  693. if (robotx<>2) and (maze[robotx-1,roboty]='*')
  694. and (poradok[curiter][needtocheck]='L')
  695. and (maze[robotx-2,roboty]=' ') then
  696. begin
  697. maze[robotx,roboty]:=' ';
  698. dec(robotx);
  699. maze[robotx,roboty]:='R';
  700. maze[robotx-1,roboty]:='*';
  701. updatefield(curiter);
  702. reshenie:=reshenie+'L';
  703. generateanswer(curiter);
  704. end;
  705.  
  706. {right check}
  707. if (robotx<>maxx) and (poradok[curiter][needtocheck]='R') then
  708. if (maze[robotx+1,roboty]=' ') or
  709. (maze[robotx+1,roboty]='.') or
  710. (maze[robotx+1,roboty]='\') {or open lambda lift}
  711. then begin
  712. maze[robotx,roboty]:=' ';
  713. inc(robotx);
  714. maze[robotx,roboty]:='R';
  715. checkteleport(curiter);
  716. updatefield(curiter);
  717. reshenie:=reshenie+'R';
  718. generateanswer(curiter);
  719. end
  720. else
  721. if (robotx<>maxx-1) and (maze[robotx+1,roboty]='*')
  722. and (poradok[curiter][needtocheck]='R')
  723. and (maze[robotx+2,roboty]=' ') then
  724. begin
  725. maze[robotx,roboty]:=' ';
  726. inc(robotx);
  727. maze[robotx,roboty]:='R';
  728. maze[robotx+1,roboty]:='*';
  729. updatefield(curiter);
  730. reshenie:=reshenie+'R';
  731. generateanswer(curiter);
  732. end;
  733.  
  734. {nothing check}
  735. if (poradok[curiter][needtocheck]='W') then
  736. begin
  737. updatefield(curiter);
  738. if changed[curiter] then
  739. begin
  740. reshenie:=reshenie+'W';
  741. generateanswer(curiter);
  742. end;
  743. end;
  744.  
  745. end;
  746. end;
  747.  
  748. begin
  749. if curiter<>1 then
  750. begin
  751. for py:=1 to maxy do
  752. for px:=1 to maxx do
  753. begin
  754. maze[px,py]:=mazes[curiter-1,px,py];
  755. end;
  756.  
  757. restemp:='';
  758. for px:=1 to length(reshenie)-1 do
  759. begin
  760. restemp:=restemp+reshenie[px];
  761. end;
  762.  
  763. {REVERT LAST MOVE}
  764. if reshenie[length(reshenie)]='L' then inc(robotx);
  765. if reshenie[length(reshenie)]='R' then dec(robotx);
  766. if reshenie[length(reshenie)]='U' then inc(roboty);
  767. if reshenie[length(reshenie)]='D' then dec(roboty);
  768.  
  769. reshenie:=restemp;
  770.  
  771. {writeln('Oops, reverting');}
  772. {readln;}
  773. end;
  774. {else writeln('All variants checked');}
  775. end;
  776. lose:=false;
  777.  
  778. end;
  779. end;
  780.  
  781. begin
  782. ctime:=TimeStamptoMSecs(DateTimeToTimestamp(now));
  783. lose:=false;
  784. uater:=0;
  785. flooding:=0;
  786. uaterproof:=10;
  787.  
  788. endd:=false;
  789. bestres:='';
  790. curx:=1;cury:=1;
  791. maxleng:=1;
  792. totalcrystall:=0;
  793.  
  794. for i:=1 to size do leng[i]:=1;
  795.  
  796. appended:=false;
  797. onceadd:=false;
  798. while (not EOF) and (not(appended)) do
  799. begin
  800. readln(ts);
  801. if length(ts)<>0 then begin
  802. for i:=1 to length(ts) do
  803. begin
  804. if (ts[i]='!') then ts[i]:=' ';
  805. if (ts[i]='W') then ts[i]:='#';
  806. if (ts[i]='@') then ts[i]:='*';
  807.  
  808. maze[curx,cury]:=ts[i];
  809.  
  810. if ts[i]='\' then
  811. begin
  812. inc(totalcrystall);
  813. crystalsx[totalcrystall]:=curx;
  814. crystalsy[totalcrystall]:=cury;
  815. end;
  816.  
  817. if ts[i]='R' then
  818. begin
  819. robotx:=curx;
  820. roboty:=cury;
  821. end;
  822.  
  823. if ts[i]='L' then
  824. begin
  825. exitx:=curx;
  826. exity:=cury;
  827. end;
  828.  
  829. inc(curx);
  830. inc(leng[cury]);
  831. if curx>maxleng then maxleng:=curx;
  832. end;
  833. end else appended:=true;
  834. dec(leng[cury]);
  835. curx:=1;
  836. inc(cury);
  837. end;
  838.  
  839. if appended then
  840. begin
  841. for ppo:=1 to 9 do trampo[1,ppo]:=0;
  842.  
  843. while (not EOF) do
  844. begin
  845. readln(ts);
  846. if ts[6]=' ' then uater:=zread(7);
  847. if (ts[9]=' ') and (ts[4]='o') then flooding:=zread(10);
  848. if (ts[11]=' ') and (ts[4]='e') then uaterproof:=zread(12);
  849. if (ts[1]='T') and (ts[2]='r') then
  850. begin
  851. numsour:=ord(ts[12])-ord('A')+1;
  852. val(ts[22],numdest);
  853. trampo[1,numsour]:=numdest;
  854. end;
  855. end;
  856. end;
  857.  
  858. uaternou[1]:=0;
  859. if flooding=0 then flooding:=100000;
  860.  
  861. mincryst:=totalcrystall;
  862. {write('SIZEX=',maxleng,' SIZEY=',cury);}
  863. dec(cury);
  864. dec(maxleng);
  865.  
  866. for i:=1 to cury do
  867. begin
  868. for j:=leng[i]+1 to maxleng do
  869. begin
  870. maze[j,i]:=' ';
  871. end;
  872. end;
  873. {writeln;
  874. for i:=1 to cury do
  875. begin
  876. for j:=1 to maxleng do
  877. begin
  878. write(maze[j,i]);
  879. end;
  880. writeln;
  881. end;}
  882.  
  883. maxx:=maxleng;
  884. maxy:=cury;
  885.  
  886. for i:=1 to maxx do
  887. for j:=1 to maxy do
  888. begin
  889. if (maze[i,j]>='A') and
  890. (maze[i,j]<='I') then
  891. begin
  892. numt:=ord(maze[i,j])-ord('A')+1;
  893. sourcesx[numt]:=i;
  894. sourcesy[numt]:=j;
  895. maze[i,j]:='.';
  896. end;
  897. if (maze[i,j]>='1') and
  898. (maze[i,j]<='9') then
  899. begin
  900. numt:=ord(maze[i,j])-ord('1')+1;
  901. destinationsx[numt]:=i;
  902. destinationsy[numt]:=j;
  903. maze[i,j]:='#';
  904. end;
  905. end;
  906.  
  907. stepiter:=trunc(sqrt(sqr(maxx)+sqr(maxy)));
  908. iter[1]:=stepiter;
  909. iter[2]:=stepiter;
  910. reshenie:='';
  911. generateanswer(0);
  912.  
  913. reshenie:=bestres;
  914. writeln(bestres);
  915.  
  916. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement