| | Discussion of Problem 1386. Maze| Show all threads     Hide all threads     Show all messages     Hide all messages |  | Overrated | FaNato4kA_TiMoFeYa | 1386. Maze | 17 Aug 2024 11:16 | 2 |  | Overrated FaNato4kA_TiMoFeYa 14 Aug 2024 20:59 |  | Why my code get TL? | Felix_Mate | 1386. Maze | 4 Jan 2017 23:10 | 1 |  | There is about nmax*nmax*smax=40 844 804 iterations and time must be about 0.1,0.2 sec. But program works >0.5 sec.How I can to optimize my code?
 
 const nmax=101;
 kmax=4;
 smax=4004;
 type myint=0..smax+1;
 var
 ans:array[1..nmax,1..nmax] of boolean;
 count,s,t:myint;
 n,m,i,j,k,x,y,ii,jj:myint;
 command:array[1..nmax,1..nmax,1..kmax,1..2] of byte;
 memory:array[1..nmax,1..nmax,1..smax] of boolean;
 list:array[1..smax] of byte;
 
 begin
 readln(n,m);
 
 for k:=1 to kmax do begin
 for i:=1 to n do begin
 for j:=1 to m do read(command[i,j,k,1],command[i,j,k,2]);
 end;
 end;
 
 read(s);
 for i:=1 to s do read(list[i]);
 
 for t:=1 to s do begin
 for i:=1 to n do begin
 for j:=1 to m do memory[i,j,t]:=false;
 end;
 end;
 
 count:=0;
 for i:=1 to n do begin
 for j:=1 to m do begin
 ans[i,j]:=false;
 end;
 end;
 
 for i:=1 to n do begin
 for j:=1 to m do begin
 t:=1;
 ii:=i;
 jj:=j;
 while(t<=s)and(not memory[ii,jj,t]) do begin
 memory[ii,jj,t]:=true;
 x:=command[ii,jj,list[t],1];
 y:=command[ii,jj,list[t],2];
 ii:=x;
 jj:=y;
 inc(t);
 end;
 if(t>s)and(not ans[ii,jj]) then begin
 inc(count);
 ans[ii,jj]:=true;
 end;
 end;
 end;
 
 writeln(count);
 for i:=1 to n do begin
 for j:=1 to m do begin
 if(ans[i,j]) then writeln(i,' ',j);
 end;
 end;
 end.
 
 Edited by author 04.01.2017 23:24
 Now AC.
 When I used a lot of memory, I got TL32. When I used a little memory, I got AC.Perhaps the cache helped.
 
 Edited by author 23.06.2017 19:24
 |  | can someone help me? | Alexandar | 1386. Maze | 30 Dec 2016 15:00 | 11 |  | I really can't solve this task?Alwaus got TLE on test 39 :-(
 Can someone give me hint or ...
As for me, simpliest way is to use assembler instructions.There are some optimizations like this:
 Bad code:
 for (x = 0; x < W; x++)
 for (y = 0; y < H; y++)
 A[y][x] = 1;
 
 This code works much faster than previous one:
 for (y = 0; y < H; y++)
 for (x = 0; x < W; x++)
 A[y][x] = 1;
Still nothing :-(This problem is driving me crazy!!!!
There is one optimization, that speeds up simple solution in about 5 times. So, keep on solving.Please send me some hint!!a:array[1..4,1..10000] of word;
 for k:=1 to 4 do
 for i:=1 to n*m do
 begin
 if i mod m=0 then readln(p1,p2) else read(p1,p2);
 a[k,i]:=(p1-1)*m+p2;
 end;
 this be fast
 i think it more faster :)
 
 a:array[1..4,101..10100] of word;
 
 for k:=1 to 4 do
 for i:=1 to n*m do
 begin
 if i mod m=0 then readln(p1,p2) else read(p1,p2);
 a[k,i]:=p1*m+p2;
 end;
Вообще если про скорость проверка if i mod m=0все тормозит
 k++;
 if (k==m){
 readln(p1,p2);
 k=0;
 }
 else
 read(p1,p2);
 надо так
 и еще очень ускоряет (покрайней мере у меня разные там битовые сдвиги)
I wrote a starightforward O(N*M*S) solution without any optimizations and got AC.Had TLE in java. Rewritten in C -> AC. |  | to ADMINS (and not only) | Tolstobrov_Anatoliy[Ivanovo SPU] | 1386. Maze | 29 Sep 2005 09:45 | 2 |  | I can solve it in Pascal in 0.265 277 KB (see statistic)It's almost my C++ solution, just rewritten in Pascal (without manual i/o :))) ).
 May be there exists some Pascal-depended optimizations.
 |  | why it is wrong? maybe i dont understansd problem? | Rage | 1386. Maze | 26 Sep 2005 16:19 | 4 |  | I got WA on test 3.I just run over all positions from which player can start game and then perform list of instruction.
 
 #include<stdio.h>
 
 #define FOR(i,n) for(i=0;i<(n);i++)
 
 int s,i,j,n,m,k,ci,cj,cnt;
 int a[4][110][110][2];
 bool ans[110][110];
 int c[4010];
 
 int main(void)
 {
 scanf("%d %d",&n,&m);
 FOR(k,4)
 FOR(i,n)FOR(j,m)
 {
 scanf("%d %d",&a[k][i][j][0],&a[k][i][j][1]);
 a[k][i][j][0]--;
 a[k][i][j][1]--;
 }
 scanf("%d",&s);
 FOR(k,s){scanf("%d",&c[k]);c[k]--;}
 memset(ans,0,sizeof(ans));
 FOR(i,n)FOR(j,m)
 {
 ci=i;cj=j;
 FOR(k,s)
 {
 ci=a[c[k]][ci][cj][0];
 cj=a[c[k]][ci][cj][1];
 }
 ans[ci][cj]=1;
 }
 cnt=0;
 FOR(i,n)FOR(j,m)if(ans[i][j])cnt++;
 printf("%d\n",cnt);
 FOR(i,n)FOR(j,m)if(ans[i][j])printf("%d %d\n",i+1,j+1);
 return 0;
 }
Tests Rage 25 Sep 2005 03:12 Mayby someone gives me testcases. Thanks in advance!ci=a[c[k]][ci][cj][0];   --- ci changed herecj=a[c[k]][ * ci * ][cj][1];
 
 
 : )
 
 
 Edited by author 26.09.2005 11:28
Thanks a lot! That silly mistake make me crazy :) | 
 | 
 |