Advertisement
Guest User

Untitled

a guest
Aug 17th, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 2.34 KB | None | 0 0
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls;
  9.  
  10. type
  11.  
  12.   { TForm1 }
  13.  
  14.   TForm1 = class(TForm)
  15.       Memo1 : TMemo;
  16.   private
  17.     { private declarations }
  18.   public
  19.     { public declarations }
  20.   end;
  21.  
  22. var
  23.   Form1 : TForm1;
  24.  
  25. implementation
  26.  
  27. {$R *.lfm}
  28.  
  29. const
  30.     A = 3;
  31.     B = 3;
  32.     N = 7;
  33.  
  34. var
  35.     Checkboard : array [1..A] of array [1..B] of Integer;
  36.     Visited: array [1..A] of array [1..B] of Boolean;
  37.     X, Y, Sum, Count : Integer;
  38.  
  39. function IsNeighbour(X1, Y1, X2, Y2 : Integer) : Boolean;
  40. begin
  41.     Result := False;
  42.     // valid index
  43.     if (X2 >= 1) and (X2 <= A) and (Y2 >= 1) and (Y2 <= B) then
  44.     begin
  45.         // to left
  46.         if (X1 - X2 = -1) and (Y1 = Y2) then
  47.             Result := True
  48.         // to right
  49.         else if (X1 - X2 = 1) and (Y1 = Y2) then
  50.             Result := True
  51.         // to down
  52.         else if (X1 = X2) and (Y1 - Y2 = -1) then
  53.             Result := True
  54.         // to up
  55.         else if (X1 = X2) and (Y1 - Y2 = 1) then
  56.             Result := True;
  57.     end;
  58. end;
  59.  
  60. procedure Backtracking(X1, Y1, X2, Y2 : Integer);
  61. var
  62.     X, Y : Integer;
  63. begin
  64.     Visited[X1][Y1] := True;
  65.  
  66.     if (X1 = X2) and (Y1 = Y2) then
  67.     begin
  68.         if Sum = N then
  69.         begin
  70.           Inc(Count);
  71.         end;
  72.     end
  73.     else
  74.     for X := Low(Checkboard) to High(Checkboard) do
  75.         for Y := Low(Checkboard[X]) to High(Checkboard[X]) do
  76.             if (Visited[X][Y] = False) and IsNeighbour(X1,Y1,X,Y) then
  77.             begin
  78.                 Form1.Memo1.Append('[' + IntToStr(X) + ',' + IntToStr(Y) + ']');
  79.                 Inc(Sum, Checkboard[X][Y]);
  80.          //       Sleep(100);
  81.  
  82.                 if Sum <= N then
  83.                   Backtracking(X, Y, X2, Y2);
  84.  
  85.                 Dec(Sum, Checkboard[X][Y]);
  86.             end;
  87.  
  88.     Visited[X1][Y1] := False;
  89. end;
  90.  
  91. begin
  92.     Sum := 0;
  93.     Count := 0;
  94.  
  95.     // Fill arrays
  96.     for X := Low(Checkboard) to High(Checkboard) do
  97.         for Y := Low(Checkboard[X]) to High(Checkboard[X]) do
  98.             Checkboard[X][Y] := Random(N div 2);
  99.     for X := Low(Visited) to High(Visited) do
  100.         for Y := Low(Visited[X]) to High(Visited[X]) do
  101.             Visited[X][Y] := False;
  102.  
  103.     // Start backtrack
  104.     Backtracking(1, 1, A, B);
  105. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement