Advertisement
Guest User

Untitled

a guest
Aug 17th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 1.58 KB | None | 0 0
  1. var a:array [1..1000,1..1000] of integer;
  2. solution:array [1..1000,1..1000] of boolean;
  3. m,n,i,j,tmp:longint;
  4.  
  5. function finished:boolean;
  6. var i,j : longint;
  7. begin
  8.    finished:= true;
  9.    for i:= 1 to m do
  10.       for j:= 1 to n do
  11.      if a[i,j] = 1 then
  12.      begin
  13.         finished := false;
  14.         exit;
  15.      end;
  16. end; { finished }
  17.  
  18. procedure change(x,y : longint);
  19. var i,j : longint;
  20. begin
  21.    solution[x,y]:= not solution[x,y];
  22.    for i:= y downto 1 do
  23.       if a[x,i] = 2 then break
  24.       else if (i <> y) then a[x,i] := integer(not(a[x,i]=1));
  25.    for i:= y to n do
  26.       if a[x,i] = 2 then break
  27.       else if (i <> y) then a[x,i] := integer(not(a[x,i]=1));
  28.    for i:= x downto 1 do
  29.       if a[i,y] = 2 then break
  30.       else if (i <> x) then a[i,y] := integer(not(a[i,y]=1));
  31.    for i:= x to m do
  32.       if a[i,y] = 2 then break
  33.       else if (i <> x) then a[i,y] := integer(not(a[i,y]=1));
  34.    a[x,y]:=integer(not(a[x,y]=1));
  35. end; { change }
  36.  
  37. procedure solve (x,y         : longint);
  38. var i,j : longint;
  39. begin
  40.    for i:=x to m do
  41.       for j:= y to n do
  42.       if not finished then
  43.       begin
  44.      begin
  45.         if not(a[i,j] = 2) then change(i,j);
  46.         if finished then exit;
  47.         if j = n then solve(i+1,1)
  48.         else solve(i,j+1);
  49.         if finished then exit;
  50.         if not(a[i,j] = 2) then change(i,j);
  51.      end;
  52.       end;
  53. end; { solve }
  54.  
  55. begin
  56.    readln(m,n);
  57.    for i:= 1 to m do
  58.    begin
  59.       for j:= 1 to n do
  60.       begin
  61.      read(a[i,j]);
  62.      solution[i,j]:= false;
  63.       end;
  64.    end;
  65.    solve(1,1);
  66.    for i:= 1 to m do
  67.       for j:= 1 to n do
  68.      write(integer(solution[i,j]));
  69. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement