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