Advertisement
Guest User

Serpenteador de Matriz

a guest
Apr 21st, 2010
330
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 2.72 KB | None | 0 0
  1. { Problema: dada uma matriz NxN, preenchê-la com números naturais em forma de espiral.
  2.  
  3. Ex: matriz 4 x 4
  4.  
  5. 01  02  03  04
  6. 12  13  14  05
  7. 11  16  15  06
  8. 10  09  08  07
  9. }
  10.  
  11. // by Dirlei Dionísio - 03/2010
  12. program Serpenteador;
  13.  
  14. uses Dialogs, SysUtils;
  15.  
  16. type
  17.   TVetor = array of Integer;
  18.   TMatriz = array of TVetor;
  19.   TSentido = (seDireita, seAbaixo, seEsquerda, seAcima);
  20.  
  21. procedure alocarMatriz(const largura, altura: Word; out matriz: TMatriz);
  22. var i: Word;
  23. begin
  24.   SetLength(matriz, largura);
  25.   for i:=0 to largura-1 do
  26.       SetLength(matriz[i], altura);
  27. end;
  28.  
  29. procedure serpentearMatriz(const largura, altura: Word; var matriz: TMatriz);
  30. var
  31.   totalElementos,contador,posHorizontal,posVertical,nivel: Word;
  32.   sentido: TSentido;
  33. begin
  34.   totalElementos := largura*altura;
  35.  
  36.   posHorizontal := 1;
  37.   posVertical := 1;
  38.  
  39.   sentido := seDireita;
  40.  
  41.   nivel := 1;
  42.   contador := 1;
  43.   while contador<=totalElementos do
  44.   begin
  45.     matriz[posHorizontal-1][posVertical-1] := contador;
  46.  
  47.     if sentido=seDireita then
  48.     begin
  49.       if posVertical=altura-nivel+1 then
  50.       begin
  51.         inc(posHorizontal,1);
  52.         sentido := seAbaixo;
  53.       end
  54.       else
  55.         inc(posVertical,1);
  56.     end
  57.     else if sentido=seAbaixo then
  58.     begin
  59.       if posHorizontal=largura-nivel+1 then
  60.       begin
  61.         inc(posVertical,-1);
  62.         sentido := seEsquerda;
  63.       end
  64.       else
  65.         inc(posHorizontal,1);
  66.     end
  67.     else if sentido=seEsquerda then
  68.     begin
  69.       if posVertical=nivel then
  70.       begin
  71.         inc(posHorizontal,-1);
  72.         sentido := seAcima;
  73.       end
  74.       else
  75.         inc(posVertical,-1);
  76.     end
  77.     else if sentido=seAcima then
  78.     begin
  79.       if posHorizontal=nivel+1 then
  80.       begin
  81.         inc(posVertical,1);
  82.         sentido := seDireita;
  83.         inc(nivel);
  84.       end
  85.       else
  86.         inc(posHorizontal,-1);
  87.     end;
  88.  
  89.     inc(contador);
  90.   end;
  91. end;
  92.  
  93. procedure exibirMatriz(const largura, altura: Word; var matriz: TMatriz);
  94. var
  95.   i, j: integer;
  96.   resultado: string;
  97. begin
  98.   for i:=0 to largura-1 do
  99.   begin
  100.     for j:=0 to altura-1 do
  101.       resultado := resultado + formatFloat('000000    ',matriz[i,j]);
  102.  
  103.     resultado := resultado + #13;
  104.   end;
  105.  
  106.   showMessage(resultado);
  107. end;
  108.  
  109. var
  110.   matriz: TMatriz;
  111.   largura, altura: Word;
  112. begin
  113.   repeat
  114.     largura := strToIntDef(inputbox('Largura', 'Qual a largura da matriz? (0 para sair)', ''),0);
  115.     if largura<1 then break;
  116.  
  117.     altura := strToIntDef(inputbox('Altura', 'Qual a altura da matriz? (0 para sair)', ''),0);
  118.     if altura<1 then break;
  119.  
  120.     alocarMatriz(largura,altura,matriz);
  121.     serpentearMatriz(largura,altura,matriz);
  122.     exibirMatriz(largura,altura,matriz);
  123.   until false;
  124. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement