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