/2048 - Trò chơi 2048

<Problem>

http://ntucoder.net/Problem/Details/1175
              Uses Crt;
        Var  n,i,j,num,last:longint;
             a,f:array[0..1000,0..1000] of longint;
        Begin
            readln(n);
            for i:=1 to n do
             for j:=1 to n do read(a[i,j]);
        
            for i:=1 to n do
             begin
                last:=n; num:=a[n,i];
        
                for j:=n-1 downto 1 do
                 begin
                  if a[j,i]<>0 then
                  begin
                     if num=0 then num:=a[j,i] else
                     begin
                      if num=a[j,i] then
                       begin
                           f[last,i]:=num+a[j,i];
                           dec(last); num:=0;
                       end
                       else
                        if (num<>a[j,i]) then
                         begin
                             f[last,i]:=num;
                             num:=a[j,i];
                             dec(last);
                         end;
                      end;
                  end;
        
                 end;
                 if num<>0 then f[last,i]:=num;
             end;
             for i:=1 to n do
              begin
                  for j:=1 to n do write(f[i,j],' ');
                  writeln;
              end;
             readln;
        End.