TABLIC - Tablica

Tác giả: ll931110

Ngôn ngữ: Pascal

{$MODE DELPHI}
program TABLIC;
const
  fin = '';
  fou = '';
  maxn = 10000;
  maxk = 1000;
var
  n,k: integer;
  fi,fo: text;
  a,dx,dy,posx,posy: array[1..maxk] of integer;

procedure openfile;
begin
  assign(fi, fin);
    reset(fi);

  assign(fo, fou);
    rewrite(fo);
end;

procedure solve;
var
  i,j,nx,ny: integer;
begin
  readln(fi, n, k);
  for i := 1 to k do readln(fi, a[i], dx[i], dy[i]);
  for i := 1 to k do
    begin
      posy[i] := a[i] mod n;
      if posy[i] = 0 then posy[i] := n;
      posx[i] := a[i] div n;
      if posy[i] <> n then inc(posx[i]);
    end;

  for i := 1 to k do
    begin
      nx := dy[i] - posy[i];
      if nx < 0 then nx := nx + n;
      ny := dx[i] - posx[i];
      if ny < 0 then ny := ny + n;

      writeln(fo, nx + ny);

      for j := 1 to k do
        if posx[j] = posx[i] then
          begin
            posy[j] := posy[j] + nx;
            if posy[j] > n then posy[j] := posy[j] - n;
          end;

      for j := 1 to k do
        if posy[j] = posy[i] then
          begin
            posx[j] := posx[j] + ny;
            if posx[j] > n then posx[j] := posx[j] - n;
          end;
    end;
end;

procedure closefile;
begin
  close(fo);
  close(fi);
end;

begin
  openfile;
  solve;
  closefile;
end.

Download