0

I'm coding the Knight's tour problem to find a tour for the knight in a n*n chessboard. I have made 2 answers, which I think are identical. However, when compiled, two codes produce 2 different results. I want to know the difference between two of my codes.

This is my first code: http://ideone.com/WUI7xD.

const 
  max = 10;

type 
  square = array [-1..max+1, -1..max+1] of longint;
  vector = array [1..max*max] of longint;

var 
  n : longint;
  x : array [1..8] of longint = (-2, -2, -1, -1, +1, +1, +2, +2);
  y : array [1..8] of longint = (-1, +1, -2, +2, -2, +2, -1, +1);
  c, r : square;
  a, b : vector;
  checking : boolean;

  procedure input;
  begin
    readln(n);
  end;

  procedure backTrack(i, u, v : longint);
  var 
    j : longint;
  begin
    if (i > n * n) then
    begin
      checking := true;
      exit;
    end;

    for j := 1 to 8 do
    begin
      if checking then exit;

      inc(u, x[j]);
      inc(v, y[j]);

      if (u > 0) and (u <= n) and (v > 0) and (v <= n) and (i<=n*n) and (c[u,v] = 0) then
      begin
        c[u, v] := 1;
        r[u, v] := i;
        backTrack(i + 1, u, v);
        c[u, v] := 0;
      end;

      dec(u, x[j]);
      dec(v, y[j]);
    end;
  end;

  procedure solve;
  begin
    fillchar(c, sizeof(c), 0);
    c[1, 1] := 1;
    r[1, 1] := 1;
    checking := false;
    backTrack(2, 1, 1);
  end;

  procedure output;
  var 
    j, i : longint;
  begin
    for j := 1 to n do
    begin
      for i := 1 to n do 
        write(r[i, j], ' ');
  
      writeln;
    end;
  
    readln;
  end;

begin
  input;
  solve;
  output;

end.

And my second code: http://ideone.com/FdFQuX

const 
  max = 10;
type 
  square = array [-1..max+1, -1..max+1] of longint;  
  vector = array [1..max*max] of longint;
var 
  n : longint;
  x : array [1..8] of longint = (-2, -2, -1, -1, +1, +1, +2, +2);
  y : array [1..8] of longint = (-1, +1, -2, +2, -2, +2, -1, +1);
  c, r : square;
  a, b : vector;
  checking : boolean;

  procedure input;
  begin
    readln(n);
  end;

  procedure backTrack(i, u, v : longint);
  var 
    j : longint;
  begin
    if (i > n * n) then
    begin
      checking := true;
      exit;
    end;

    r[u, v] := i;
  
    for j := 1 to 8 do
    begin
      if checking then exit;
   
      inc(u, x[j]);
      inc(v, y[j]);
  
      if (u > 0) and (u <= n) and (v > 0) and (v <= n) and (i <= n * n) and (c[u, v] = 0) then
      begin
        c[u, v] := 1;
        backTrack(i + 1, u, v);
        c[u, v] := 0;
      end;

      dec(u,x[j]);
      dec(v,y[j]);
    end;
  end;

  procedure solve;
  begin
    fillchar(c, sizeof(c), 0);
    c[1, 1] := 1;
    r[1, 1] := 1;
    checking := false;
    backTrack(1, 1, 1);
  end;

  procedure output;
  var 
    j, i : longint;
  begin
    for j := 1 to n do
    begin
      for i:=1 to n do 
        write(r[i, j], ' ');
   
      writeln;
    end;
  end;

begin
  input;
  solve;
  output;
end.
Chris
  • 26,361
  • 5
  • 21
  • 42

1 Answers1

0

r[u,v]:=i; appears before for j:=1 to 8 do in the second code but not the first.

There are differencing tools which can be used to tell where two text files differ. It is a good idea to become familiar with such tools.

John Coleman
  • 51,337
  • 7
  • 54
  • 119
  • I think its position does not have any effects on the outcome of my code. I wonder if there is a difference if I put it in 2 different positions as my above code. – Thong Nguyen Thanh Feb 04 '16 at 15:03
  • How could it not make a difference since its location in the first code is in the body of an `if` statement (hence presumably not always executed) but its position in the second code is unconditional? – John Coleman Feb 04 '16 at 15:53