U E D R , A S I H C RSS

Eight Queen Problem/da_answer

No older revisions available

No older revisions available



Project2.exe (Delphi)


위키는 머리털나고 처음이라.. -_-;; 제대로 쓰고 있는건지 잘 모르겠군요...

대부분 C, C++ 혹은 자바로 작성하셨던데... 전 델파이로 했습니다.
사실 위에 USES나 폼, 버튼, 클릭이벤등은 별 필요없는 부분인데... ^^;;;

전 좀 많이 허접이라..T_T 제 소스는 보시면 눈만 아프실 겁니다.

초반에 방향을 잘못잡아 많이 애먹었습니다. 그것땀시 시간도 많이 잡아먹었고...
맵을 RECORD로 정의 한 다음 한 라인에 하나씩 여왕을 올려 놓고 그 때마다 공격 가능 패스를 다 지우는(?) 방식으로 하려했는데...
이렇게 하면 쓸데없는 루프를 돌지 않아도 되지만 신경쓸게 좀 많더군요.. -_-;;

화장실에서 볼 일을 보면서 좀 생각해봤는데... 제가 너무 어렵게 진행하고 있는듯 싶더군요.
그래서 방식을 바꿨습니다.
공격 패스에 들어가지 않는 위치에 올려 놓는게 아니라 일단 올려 놓고 공격받을 수 있는가 체크하는 방식으로요.. ^^;;

제가 코딩을 할 때마다 느끼는 저의 문제점인데..
항상 코딩이 끝나면 초반에 설계한 것과는 좀 다른 방향으로 나가있더군요.. -_-;;
이번에도 왠지 모르게 좀 어설픈...

시간이 되면 코드 정리라도 좀 하려고 했는데...
벌써 시간이.. -_-;;;; 오후는 이걸로 다 날려먹었네요.


위에 소스들을 봤는데 확실히 전 허접이라는게 다시 한번 느껴지네요.. T_T
난 언제쯤이면 고수가 되려나~~




켁... 2번문제... 처음부터 계획이 2먼문제의 요구사항을 충족시키는 방향이었기 땀시..
전 당연히 그렇게 한줄 알았건만.. 제가 뭔가 잘못하고 있었었네요... -_-;;;

아무리 봐도 소스는 모든 패스를 구하는건데... 이상하다... 생각하다가...
혹시나 싶어서 출력부분을 리커시브 안쪽으로 옮겨놓으니.. 잘 나오는군요.. -_-;;


그나저나...
난 메모 컴포넌트는 왜 올려 놓은거지.. -_-;; 어차피 showMessage로 보여줄거였으면서....

~cpp 
<script>
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

const
  CBoardSize = 8;

type
  TQueenPos = record
    X: integer;
    Y: integer;
  end;

  TForm1 = class(TForm)
    Memo1: TMemo;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    procedure SearchStart();
    function getNextQueenPos(index, X, Y:integer):boolean;
    function checkRule(X,Y:integer):boolean;
    { Private declarations }
  public
    QueenPosArr: array of TQueenPos;
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
  SearchStart();
end;

procedure TForm1.SearchStart();
var
  i, j: integer;
begin
  setLength(QueenPosArr, CBoardSize);
  for i:=0 to CBoardSize-1 do
  begin
    QueenPosArr[i].X := -1;
    QueenPosArr[i].Y := -1;
  end;

  for i:=0 to CBoardSize-1 do
  begin
    QueenPosArr[0].X := 0;
    QueenPosArr[0].Y := i;
    showMessage(inttostr(i));
    if getNextQueenPos(0, QueenPosArr[i].X, QueenPosArr[i].Y) then
    begin
      for j:=0 to CBoardSize-1 do
      begin
        showMessage(inttostr(QueenPosArr[j].X)+'-'+inttostr(QueenPosArr[j].Y));
      end;
    end
    else
    begin

    end;
 end;
end;

function TForm1.getNextQueenPos(index, X, Y:integer):boolean;
var
  i: integer;
  nowIndex: integer;
begin
  for i:=0 to CBoardSize-1 do
  begin
    nowIndex := index+1;
    QueenPosArr[nowIndex].X := nowIndex;
    QueenPosArr[nowIndex].Y := i;
    if checkRule(QueenPosArr[nowIndex].X, QueenPosArr[nowIndex].Y) then
    begin
      if nowIndex = 7 then
      begin
        result := true;
        exit;
      end
      else
      begin
        if getNextQueenPos(nowIndex, QueenPosArr[nowIndex].X, QueenPosArr[nowIndex].Y) then
        begin
          result := true;
          exit;
        end
        else
        begin
          QueenPosArr[nowIndex].X := -1;
          QueenPosArr[nowIndex].Y := -1;
        end;
      end;
    end
    else
    begin
      QueenPosArr[nowIndex].X := -1;
      QueenPosArr[nowIndex].Y := -1;
      if i = CBoardSize-1 then
      begin
        result := false;
      end;
    end;
  end;
end;

function TForm1.checkRule(X,Y:integer):boolean;
var
  i: integer;
begin
  for i:=0 to X-1 do
  begin
    if QueenPosArr[i].X > -1 then
    begin
      if (X=QueenPosArr[i].X) or (Y=QueenPosArr[i].Y) or ((Y-X)=(QueenPosArr[i].Y-QueenPosArr[i].X)) or ((Y+X)=(QueenPosArr[i].Y+QueenPosArr[i].X)) then
      begin
        result := false;
        exit;
      end;
    end;
  end;
  result := true;
end;

end.




~cpp 
<script>
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

const
  CBoardSize = 8;

type
  TQueenPos = record
    X: integer;
    Y: integer;
  end;

  TForm1 = class(TForm)
    Memo1: TMemo;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    procedure SearchStart();
    function getNextQueenPos(index, X, Y:integer):boolean;
    function checkRule(X,Y:integer):boolean;
    { Private declarations }
  public
    QueenPosArr: array of TQueenPos;
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
  SearchStart();
end;

procedure TForm1.SearchStart();
var
  i: integer;                          // <------ 변수 삭제...
begin
  setLength(QueenPosArr, CBoardSize);
  for i:=0 to CBoardSize-1 do
  begin
    QueenPosArr[i].X := -1;
    QueenPosArr[i].Y := -1;
  end;

  for i:=0 to CBoardSize-1 do
  begin
    QueenPosArr[0].X := 0;
    QueenPosArr[0].Y := i;
    showMessage(inttostr(i));
    if getNextQueenPos(0, QueenPosArr[i].X, QueenPosArr[i].Y) then
    begin

    end
    else
    begin

    end;
 end;
end;

function TForm1.getNextQueenPos(index, X, Y:integer):boolean;
var
  i, j: integer;                //<------- 변수 추가
  nowIndex: integer;
begin
  for i:=0 to CBoardSize-1 do
  begin
    nowIndex := index+1;
    QueenPosArr[nowIndex].X := nowIndex;
    QueenPosArr[nowIndex].Y := i;
    if checkRule(QueenPosArr[nowIndex].X, QueenPosArr[nowIndex].Y) then
    begin
      if nowIndex = 7 then
      begin

//************************************* 이쪽에서 출력하는 방식으로 변환
        for j:=0 to CBoardSize-1 do
        begin
          showMessage(inttostr(QueenPosArr[j].X)+'-'+inttostr(QueenPosArr[j].Y));
        end;
//*************************************
        result := true;
        exit;
      end
      else
      begin
        if getNextQueenPos(nowIndex, QueenPosArr[nowIndex].X, QueenPosArr[nowIndex].Y) then
        begin
          result := true;
          exit;
        end
        else
        begin
          QueenPosArr[nowIndex].X := -1;
          QueenPosArr[nowIndex].Y := -1;
        end;
      end;
    end
    else
    begin
      QueenPosArr[nowIndex].X := -1;
      QueenPosArr[nowIndex].Y := -1;
      if i = CBoardSize-1 then
      begin
        result := false;
      end;
    end;
  end;
end;

function TForm1.checkRule(X,Y:integer):boolean;
var
  i: integer;
begin
  for i:=0 to X-1 do
  begin
    if QueenPosArr[i].X > -1 then
    begin
      if (X=QueenPosArr[i].X) or (Y=QueenPosArr[i].Y) or ((Y-X)=(QueenPosArr[i].Y-QueenPosArr[i].X)) or ((Y+X)=(QueenPosArr[i].Y+QueenPosArr[i].X)) then
      begin
        result := false;
        exit;
      end;
    end;
  end;
  result := true;
end;

end.
Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:23:12
Processing time 0.0170 sec