Użytkownik: reason
Tytuł: NEWS_bezpowtorzen3
Język: Pascal
Data: 8 lut 2010, 17:51
Kody powiązane:
Ocena:
// tablica jest of string i jezeli w napisie w danej komorce jest jedna z liter N, S, E, W to // mozemy z tego pola poruszac sie w odpowiednich kierunkach (np. jezeli mamy N i E to polnoc i wschod). // program sprawdza wszystkie mozliwe drogi i wypisuje kazda z nich w osobnej linii. // jedyny warunek jest nastepujacy - w kazdej drodze kazde pole tablicy mozna przejsc tylko raz. program drogastring; uses crt; const n = 4; type tab = array[1..n, 1..n] of string; var qn, qs, qw, qe : boolean; t : tab; i, j : integer; droga : string; procedure kierunki(a : string); // procedura zmieniajaca wartosci globalne kierunkow w ktore mozna sie ruszyc var i, p : integer; begin p := length(a); for i := 1 to p do begin if a[i] = 'n' then qn := true; if a[i] = 's' then qs := true; if a[i] = 'e' then qe := true; if a[i] = 'w' then qw := true; end; end; procedure wypisz(droga : string); begin writeln ('Dalo sie przejsc drogą: ',droga); droga := ''; // czyszcze droge dla kolejnego przejscia end; procedure skok(i,j : integer; droga : string; t : tab); var a : string; begin a := t[i,j]; kierunki(a); t[i,j] := '#'; if (i = n) and (j = n) then wypisz(droga) else begin if (qn) and (j-1 > 0) then if (t[i,j-1] <> '#') then skok(i,j-1,droga + 'n ',t); if (qs) and (j+1 <= n) then if (t[i,j+1] <> '#') then skok(i,j+1,droga + 's ',t); if (qe) and (i+1 <= n) then if (t[i+1,j] <> '#') then skok(i+1,j,droga + 'e ',t); if (qw) and (i-1 > 0) then if (t[i-1,j] <> '#') then skok(i-1,j,droga + 'w ',t); end; end; begin clrscr; droga := ''; qn := false; qw := false; qe := false; qs := false; for j := 1 to n do for i := 1 to n do t[i,j] := 'nes'; skok(1,1,droga,t); end.



