Cod sursa(job #230993)

Utilizator matemariaescuMaria Mateescu matemariaescu Data 14 decembrie 2008 11:12:50
Problema Tablete Scor 20
Compilator fpc Status done
Runda Algoritmiada 2009, Runda 1, Clasele 5-8 Marime 1.99 kb
program tablete;
var n,k,i,j:longint;
begin
  assign(input,'tablete.in'); reset(input);
  assign(output,'tablete.out'); rewrite(output);
  readln(n,k);
  if (n mod 2 =0) and(k mod 2=0) then
    begin
      for i := 1 to n do
        begin
          for j := 1 to n do
            write(n*(i-1)+j,' ');
          writeln;
        end;
    end;
  if (n mod 2 =0) and (k mod 2 =1) then
    begin
      for i := 1 to n-2 do
        begin
          for j:= 1 to n do
            write(n*(i-1)+j+1,' ');
          writeln;
        end;
      for j:= 1 to n-1 do
        write(n*(n-2)+j+1,' ');
      writeln(n*(n-1)+3);
      write(1,' ',n*(n-1)+1,' ',n*(n-1)+2);
      for j := 4 to n do
        write(' ',n*(n-1)+j);
      writeln;
    end;
  if (n mod 2 =1) and (k mod 2 =0) then
    begin
      for i := 1 to n do
        write(i,' ');
      writeln;
      for i:= 2 to n do
        begin
          if i mod 2 =0 then
            begin
              for j := 1 to n do
                write(n*(i-1)+j+1,' ');
            end
            else
              begin
                write(n*(i-2)+1);
                for j:= 2 to n do
                  write(' ',n*(i-1)+j);
              end;
          writeln;
        end;
    end;
  if (n mod 2 =1) and (k mod 2 =1) then
    begin
      for i := 1 to n do
        write(i+1,' ');
      writeln;
      for i:= 2 to n-2 do
        begin
          if i mod 2 =0 then
            begin
              for j := 1 to n do
                write(n*(i-1)+j+2,' ');
            end
            else
              begin
                write(n*(i-2)+2);
                for j:= 2 to n do
                  write(' ',n*(i-1)+j+1);
              end;
          writeln;
        end;
      for j := 1 to n-1 do
        write(n*(n-2)+j+2,' ');
      writeln(n*(n-1)+4);
      write(1,' ',n*(n-2)+2,' ',n*(n-1)+2,' ',n*(n-1)+3);
      for j := 5 to n do
        write(' ',n*(n-1)+j);
    end;
  close(output);
  close(input);
end.