U E D R , A S I H C RSS

Eight Queen Problem/da_answer

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.0134 sec