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