JAPCROSS.BAK 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. uses Graph;
  2. const
  3. MAX_SIZE = 100;
  4. MAX_LEN = 20;
  5. bx = 30;
  6. by = 30;
  7. type
  8. tLine = array [1..MAX_SIZE] of shortint;
  9. pLine = ^tLine;
  10. tLines = array [1..MAX_SIZE] of pLine;
  11. pLines = ^TLines;
  12. tData = array [1..MAX_LEN] of byte;
  13. pData = ^tData;
  14. tAll = array [1..MAX_SIZE] of pData;
  15. pAll = ^tAll;
  16. var
  17. Lines : pLines;
  18. Cols : pLines;
  19. DataX : pAll;
  20. DataY : pAll;
  21. changed: boolean;
  22. inp : text;
  23. nIn,nOut:string;
  24. SizeX,SizeY,sDx,sDy : word;
  25. Function KeyPressed:boolean;assembler;
  26. asm
  27. mov ah, 1
  28. int 16h
  29. mov al, 0
  30. jz @@2
  31. mov al, 1
  32. @@2:
  33. end;
  34. procedure InitBoardAndData;
  35. var i : word;
  36. begin
  37. GetMem(Lines,sizeof(pLine)*SizeY);
  38. GetMem(Cols,sizeof(pLine)*SizeX);
  39. for i := 1 to SizeY do
  40. begin
  41. GetMem(Lines^[i],SizeX);
  42. FillChar(Lines^[i]^,SizeX,0);
  43. end;
  44. for i := 1 to SizeX do
  45. begin
  46. GetMem(Cols^[i],SizeY);
  47. FillChar(Cols^[i]^,SizeY,0);
  48. end;
  49. GetMem(DataX,sizeof(pData)*SizeX);
  50. GetMem(DataY,sizeof(pData)*SizeY);
  51. for i := 1 to SizeX do
  52. begin
  53. GetMem(DataX^[i],sDx+1);
  54. FillChar(DataX^[i]^,sDx+1,0);
  55. end;
  56. for i := 1 to SizeY do
  57. begin
  58. GetMem(DataY^[i],sDy+1);
  59. FillChar(DataY^[i]^,sDy+1,0);
  60. end;
  61. end;{InitBoardAndData}
  62. {-------------------------------}
  63. Procedure ReleaseAlldata;
  64. var i : word;
  65. begin
  66. for i := 1 to SizeY do FreeMem(Lines^[i],SizeY);
  67. for i := 1 to SizeX do FreeMem(Cols^[i],SizeX);
  68. FreeMem(Lines,sizeof(pLine)*SizeY);
  69. FreeMem(Cols,sizeof(pLine)*SizeX);
  70. for i := 1 to SizeX do FreeMem(DataX^[i],sDx+1);
  71. for i := 1 to SizeY do FreeMem(DataY^[i],sDy+1);
  72. FreeMem(DataX,sizeof(pData)*SizeX);
  73. FreeMem(DataY,sizeof(pData)*SizeY);
  74. end;{ReleaseAlldata}
  75. {-------------------------------}
  76. Procedure ReadAllData;
  77. var i,j,err:word;
  78. st,s2 : string;
  79. begin
  80. i:=0;
  81. {
  82. for i:=1 to SizeX do
  83. for J := 1 to sDx do Read(inp,dataX^[i]^[j]);
  84. for i:=1 to SizeY do
  85. for J := 1 to sDy do Read(inp,dataY^[i]^[j]);}
  86. repeat
  87. ReadLn(inp,st);
  88. if st = '' then continue;
  89. inc(i);j:=1;
  90. if Pos(' ',st) = 0 then Val(st,DataX^[i]^[1],err)
  91. else
  92. begin
  93. repeat
  94. s2:=Copy(st,1,Pos(' ',st)-1);
  95. Val(s2,DataX^[i]^[j],err);
  96. Delete(st,1,Pos(' ',st));
  97. inc(j);
  98. until pos(' ',st) = 0;
  99. Val(st,DataX^[i]^[j],err);
  100. end;
  101. until eof(inp) or (i=SizeX);
  102. i:=0;
  103. repeat
  104. ReadLn(inp,st);
  105. if st = '' then continue;
  106. inc(i);j:=1;
  107. if Pos(' ',st) = 0 then Val(st,DataY^[i]^[1],err)
  108. else
  109. begin
  110. repeat
  111. s2:=Copy(st,1,Pos(' ',st)-1);
  112. Val(s2,DataY^[i]^[j],err);
  113. Delete(st,1,Pos(' ',st));
  114. inc(j);
  115. until pos(' ',st) = 0;
  116. Val(st,DataY^[i]^[j],err);
  117. end;
  118. until eof(inp) or (i=SizeY);
  119. end;{ReadAllData}
  120. {-----------------------------------------------}
  121. Procedure SavePicture;
  122. var
  123. i,j:word;
  124. Pict : text;
  125. l1,l2:string;
  126. begin
  127. Assign(pict,nOut);
  128. Rewrite(pict);
  129. l1:=' ';
  130. l2:=' ';
  131. if sizex>9 then
  132. begin
  133. for j:=1 to 9 do l1:=l1+' ';
  134. for i:=1 to (sizeX div 10)-1 do for j:=1 to 10 do l1:=l1+chr($30+i);
  135. for j:=0 to (sizex mod 10) do l1:=l1+chr($30+(sizeX) div 10);
  136. for j:=1 to 9 do l2:=l2+chr($30+j);
  137. for i:=1 to (sizeX div 10)-1 do for j:=0 to 9 do l2:=l2+chr($30+j);
  138. for j:=0 to (sizex mod 10) do l2:=l2+chr($30+j);
  139. end
  140. else
  141. for j:=1 to SizeX do l2:=l2+chr($30+j);
  142. { WriteLn(Pict,' 111111111122222222223333333333444444444455555555556666666666777777');
  143. WriteLn(Pict,' 123456789012345678901234567890123456789012345678901234567890123456789012345');}
  144. WriteLn(Pict,l1);WriteLn(Pict,l2);
  145. for i := 1 to sizey do
  146. begin
  147. if i > 9 then Write(Pict,i,' ')
  148. else Write(Pict,' ',i,' ');
  149. for j:= 1 to sizex do
  150. case Lines^[i]^[j] of
  151. 1 : write(pict,'þ');
  152. -1: write(pict,'ú');
  153. 0 : write(pict,' ');
  154. end;
  155. Writeln(pict);
  156. end;
  157. Close(pict);
  158. end;{SavePicture}
  159. {-----------------------------------------------}
  160. Procedure DrawPicture;
  161. var
  162. i,j:word;
  163. l:string;
  164. k:real;
  165. grDriver: Integer;
  166. grMode: Integer;
  167. ErrCode: Integer;
  168. begin
  169. grDriver := Detect;
  170. InitGraph(grDriver, grMode,' ');
  171. ErrCode := GraphResult;
  172. if ErrCode <> grOk then
  173. begin
  174. WriteLN('No graph :)');
  175. Exit;
  176. end;
  177. if SizeX>SizeY then
  178. k:=600/SizeX else k:=440/SizeY;
  179. SetColor(Yellow);
  180. Rectangle(bx,by,bx+round(SizeX*k),by+round(SizeY*k));
  181. SetTextStyle(DefaultFont, VertDir, 1);
  182. SetTextJustify(CENTERTEXT,bottomtext);
  183. for i := 1 to SizeX do
  184. begin
  185. Str(i,l);
  186. OutTextXY(round(bx+(i-0.5)*k),by-2,l);
  187. Line(round(bx+i*k),by,round(bx+i*k),by+round(SizeY*k));
  188. end;
  189. SetTextStyle(DefaultFont, HorizDir, 1);
  190. SetTextJustify(RightText,Centertext);
  191. for i := 1 to SizeY do
  192. begin
  193. Str(i,l);
  194. OutTextXY(bx-2,by+round((i-0.5)*k),l);
  195. Line(bx,by+round(i*k),round(bx+SizeX*k),by+round(i*k));
  196. end;
  197. SetFillStyle(SolidFill,LightRed);
  198. SetColor(Lightred);
  199. for i := 1 to sizey do
  200. for j := 1 to sizex do
  201. case Lines^[i]^[j] of
  202. 1 : Bar(bx+round((j-1)*k)+1,by+round((i-1)*k)+1,bx+round(k*j)-1,by+round(k*i)-1);
  203. { 1 : FillEllipse(bx+round((j-0.5)*k),by+round((i-0.5)*k),round(k/2),round(k/2));}
  204. -1: FillEllipse(bx+round((j-0.5)*k),by+round((i-0.5)*k),round(k/10),round(k/10));
  205. end;
  206. end;{DrawPicture}
  207. {-----------------------------------------------}
  208. Procedure Work(Main:pLine;Other:pLines;data:pData;MainS,pos:word);
  209. var
  210. M : array [1..MAX_SIZE] of longint;
  211. poss : array [1..MAX_LEN] of byte;
  212. counter : longint;
  213. tmp : shortint;
  214. working : tLine;
  215. i,j : word;
  216. {-----------------------------}
  217. Function Test:boolean;
  218. var i:word;
  219. begin
  220. Test:=true;
  221. for i := 1 to MainS do if Main^[i]*working[i] < 0 then Test:=false;
  222. end;
  223. {-----------------------------}
  224. Procedure Setwork;
  225. var
  226. i,j:word;
  227. begin
  228. fillchar(working,MAX_SIZE,255);i:=1;
  229. while Data^[i]<>0 do
  230. begin for j := poss[i] to poss[i]+Data^[i]-1 do working[j]:=1;inc(i);end;
  231. end;
  232. {-----------------------------}
  233. Function Shift:boolean;
  234. var last:word;
  235. begin
  236. shift:=false;Last:=1;
  237. while poss[Last+1]<>0 do inc(Last);
  238. if data^[Last]+poss[Last]>MainS then
  239. begin
  240. dec(last);
  241. while (Last>0) and (poss[Last]+Data^[Last]+1 >= poss[Last+1]) do dec(last);
  242. end;
  243. if Last=0 then exit;
  244. inc(poss[Last]);
  245. { if Poss[Last+1] <> 0 then Poss[Last+1] := poss[last]+Data^[Last]+1;}
  246. while Poss[Last+1] <> 0 do begin Poss[Last+1] := poss[last]+Data^[Last]+1; inc(last);end;
  247. shift:=true;
  248. setwork;
  249. end;
  250. {-----------------------------}
  251. begin
  252. FillChar(M,MAX_SIZE*4,0);FillChar(poss,MAX_LEN,0);counter:=0;i:=1;j:=1;
  253. while Data^[i]<>0 do begin poss[i]:=j;inc(j,data^[i]+1);inc(i);end;SetWork;
  254. repeat
  255. if test then
  256. begin
  257. inc(counter);
  258. for i := 1 to MainS do inc(M[i],working[i]);
  259. end;
  260. until not shift;
  261. if counter = 0 then begin WriteLN('Error in Data!');Halt(200);end;
  262. for i := 1 to MainS do if abs(m[i])=counter then
  263. begin
  264. tmp := M[i] div abs(m[i]);
  265. if main^[i] <> tmp then
  266. begin
  267. changed := true;
  268. main^[i]:= tmp;
  269. end;
  270. Other^[i]^[pos] := tmp;
  271. end;
  272. end;
  273. {-------------------------------}
  274. Procedure Reverse(Data:pAll;Size:word);
  275. var
  276. i,j,last,rb:word;
  277. begin
  278. for i:=1 to Size do
  279. begin
  280. last:=1;while data^[i]^[Last+1]<>0 do inc(Last);
  281. for j := 1 to last div 2 do
  282. begin
  283. rb:=data^[i]^[Last-j+1];
  284. data^[i]^[Last-j+1] := data^[i]^[j];
  285. data^[i]^[j]:=rb;
  286. end;
  287. end;
  288. end;
  289. {----------------------}
  290. var
  291. i : word;
  292. total : longint;
  293. revx,revy : word;
  294. begin
  295. if ParamCount < 1 then
  296. begin
  297. WriteLn('Usage: japcross.exe <input.txt> [out.asc]');
  298. Halt(255);
  299. end;
  300. nIn := ParamStr(1);
  301. if ParamStr(2) <> '' then nOut := ParamStr(2)
  302. else nOut := Copy(nIn,1,Length(nIn)-3)+'asc';
  303. Assign(inp,nIn);
  304. {$I-}
  305. Reset(inp);
  306. if IOResult <> 0 then
  307. begin
  308. writeLn('Error opening ',nIn);
  309. Halt(244);
  310. end;
  311. {$I+}
  312. Read(inp,SizeX,SizeY,sDx,sDy,revx,revy);
  313. InitBoardAndData;
  314. ReadAlldata;
  315. if RevX = 1 then Reverse(DataX,SizeX);
  316. if RevY = 1 then Reverse(DataY,SizeY);
  317. total := 0;
  318. repeat
  319. changed := false;
  320. for i:=1 to sizeX do Work(Cols^[i],Lines,DataX^[i],SizeY,i);
  321. for i:=1 to sizeY do Work(Lines^[i],Cols,DataY^[i],SizeX,i);
  322. inc(total);
  323. Write(#13'Times: ',total,' ');
  324. if KeyPressed then
  325. begin
  326. SavePicture;
  327. Close(inp);
  328. ReleaseAllData;
  329. Halt(255);
  330. end;
  331. until not changed;
  332. SavePicture;
  333. Close(inp);
  334. { DrawPicture;}
  335. ReadLn;
  336. ReleaseAllData;
  337. { CloseGraph;}
  338. end.