U E D R , A S I H C RSS

Eight Queen Problem/용쟁호투SQL

왜? 이런짓을 하는가?

Powerbuilder로 해본김에 늘 같이 작업하던 MS-SQL로 해보았습니다.

MS-SQL Query

~cpp 
IF EXISTS(SELECT name FROM sysobjects WHERE name = N'temp_queen_attack' AND type = 'U') DROP TABLE temp_queen_attack
GO

CREATE TABLE temp_queen_attack(
y int NULL,
x1 int NULL, 
x2 int NULL, 
x3 int NULL, 
x4 int NULL, 
x5 int NULL, 
x6 int NULL, 
x7 int NULL, 
x8 int NULL,
fx int NULL,
fy int NULL)
GO

IF EXISTS (SELECT name FROM sysobjects WHERE name = N'p_temp_reset' AND type = 'P') DROP PROCEDURE p_temp_reset
GO

CREATE PROCEDURE p_temp_reset
AS
	TRUNCATE TABLE temp_queen_attack
	INSERT INTO temp_queen_attack VALUES(1,0,0,0,0,0,0,0,0,0,0)
	INSERT INTO temp_queen_attack VALUES(2,0,0,0,0,0,0,0,0,0,0)
	INSERT INTO temp_queen_attack VALUES(3,0,0,0,0,0,0,0,0,0,0)
	INSERT INTO temp_queen_attack VALUES(4,0,0,0,0,0,0,0,0,0,0)
	INSERT INTO temp_queen_attack VALUES(5,0,0,0,0,0,0,0,0,0,0)
	INSERT INTO temp_queen_attack VALUES(6,0,0,0,0,0,0,0,0,0,0)
	INSERT INTO temp_queen_attack VALUES(7,0,0,0,0,0,0,0,0,0,0)
	INSERT INTO temp_queen_attack VALUES(8,0,0,0,0,0,0,0,0,0,0)
GO

IF EXISTS (SELECT name FROM sysobjects WHERE name = N'p_check_attack' AND type = 'P') DROP PROCEDURE p_check_attack
GO

CREATE PROCEDURE p_check_attack @x int, @y int
AS
DECLARE @attack_check int, @local_x int, @local_y int

SELECT @attack_check = (CASE @x WHEN 1 THEN x1 WHEN 2 THEN x2 WHEN 3 THEN x3 WHEN 4 THEN x4 WHEN 5 THEN x5 WHEN 6 THEN x6 WHEN 7 THEN x7 WHEN 8 THEN x8 END)
  FROM temp_queen_attack
 WHERE y = @y
IF @attack_check = 1 RETURN 1

--@x에 해당하는 컬럼을 공격루트셋팅
IF @x = 1 UPDATE temp_queen_attack SET x1 = 1
IF @x = 2 UPDATE temp_queen_attack SET x2 = 1
IF @x = 3 UPDATE temp_queen_attack SET x3 = 1
IF @x = 4 UPDATE temp_queen_attack SET x4 = 1
IF @x = 5 UPDATE temp_queen_attack SET x5 = 1
IF @x = 6 UPDATE temp_queen_attack SET x6 = 1
IF @x = 7 UPDATE temp_queen_attack SET x7 = 1
IF @x = 8 UPDATE temp_queen_attack SET x8 = 1

--@y에 해당하는 컬럼을 공격루트셋팅
UPDATE temp_queen_attack
   SET x1 = 1,x2 = 1,x3 = 1,x4 = 1,x5 = 1,x6 = 1,x7 = 1,x8 = 1
 WHERE y = @y

--@x,@y에서 @x+n,@y+n방향으로 공격루트셋팅
SET @local_x = @x
SET @local_y = @y

IF @local_x > @local_y
	BEGIN
	SET @local_x = @local_x + (1 - @local_y)
	SET @local_y = 1
	END
IF @local_x < @local_y
	BEGIN
	SET @local_y = @local_y + (1 - @local_x)
	SET @local_x = 1
	END
IF @local_x = @local_y
	BEGIN
	SET @local_x = 1
	SET @local_y = 1
	END
WHILE @local_x <= 8 AND @local_y <= 8
	BEGIN
	IF @local_x = 1 UPDATE temp_queen_attack SET x1 = 1 WHERE y = @local_y
	IF @local_x = 2 UPDATE temp_queen_attack SET x2 = 1 WHERE y = @local_y
	IF @local_x = 3 UPDATE temp_queen_attack SET x3 = 1 WHERE y = @local_y
	IF @local_x = 4 UPDATE temp_queen_attack SET x4 = 1 WHERE y = @local_y
	IF @local_x = 5 UPDATE temp_queen_attack SET x5 = 1 WHERE y = @local_y
	IF @local_x = 6 UPDATE temp_queen_attack SET x6 = 1 WHERE y = @local_y
	IF @local_x = 7 UPDATE temp_queen_attack SET x7 = 1 WHERE y = @local_y
	IF @local_x = 8 UPDATE temp_queen_attack SET x8 = 1 WHERE y = @local_y
	SET @local_x = @local_x + 1
	SET @local_y = @local_y + 1
	END

--@x,@y에서 @x-n,@y+n방향으로 공격루트셋팅
SET @local_x = 9 - @x
SET @local_y = @y

IF @local_x > @local_y
	BEGIN
	SET @local_x = @local_x + (1 - @local_y)
	SET @local_y = 1
	END
IF @local_x < @local_y
	BEGIN
	SET @local_y = @local_y + (1 - @local_x)
	SET @local_x = 1
	END
IF @local_x = @local_y
	BEGIN
	SET @local_x = 1
	SET @local_y = 1
	END

WHILE @local_x <= 8 AND @local_y <= 8
	BEGIN
	IF 9- @local_x = 1 UPDATE temp_queen_attack SET x1 = 1 WHERE y = @local_y
	IF 9- @local_x = 2 UPDATE temp_queen_attack SET x2 = 1 WHERE y = @local_y
	IF 9- @local_x = 3 UPDATE temp_queen_attack SET x3 = 1 WHERE y = @local_y
	IF 9- @local_x = 4 UPDATE temp_queen_attack SET x4 = 1 WHERE y = @local_y
	IF 9- @local_x = 5 UPDATE temp_queen_attack SET x5 = 1 WHERE y = @local_y
	IF 9- @local_x = 6 UPDATE temp_queen_attack SET x6 = 1 WHERE y = @local_y
	IF 9- @local_x = 7 UPDATE temp_queen_attack SET x7 = 1 WHERE y = @local_y
	IF 9- @local_x = 8 UPDATE temp_queen_attack SET x8 = 1 WHERE y = @local_y
--	il_attack[9 - li_x,li_y] = 1 
	SET @local_x = @local_x + 1
	SET @local_y = @local_y + 1
	END
RETURN 0
GO

IF EXISTS (SELECT name FROM sysobjects WHERE name = N'p_create_queen' AND type = 'P') DROP PROCEDURE p_create_queen
GO

CREATE PROCEDURE p_create_queen @queen_count int, @create_limit int
AS
DECLARE @l_x int, @l_y int, @rtn_check int

WHILE @queen_count <= 8
	BEGIN
	SET @l_x = convert(int, RAND() * 8) + 1
	SET @l_y = convert(int, RAND() * 8) + 1
	SET @create_limit = @create_limit + 1
	IF @create_limit >= 64
		BEGIN
		SET @queen_count  = 1
		SET @create_limit = 1
		RETURN 0
		END
	EXEC @rtn_check = p_check_attack @l_x, @l_y
	IF @rtn_check = 1 CONTINUE
	SET @queen_count = @queen_count + 1
	SET @create_limit = 1
	UPDATE temp_queen_attack SET fx = @l_x, fy = @l_y WHERE y = @queen_count - 1
	END
RETURN 1
GO

IF EXISTS (SELECT name FROM sysobjects WHERE name = N'p_queen' AND type = 'P') DROP PROCEDURE p_queen
GO

CREATE PROCEDURE p_queen
AS
EXEC p_temp_reset
DECLARE @queen_count int, @create_limit int, @rtn_create int
SET @queen_count = 1
SET @create_limit = 1
EXEC @rtn_create = p_create_queen @queen_count,@create_limit
IF @rtn_create = 0 EXEC p_queen
GO

EXEC p_queen
GO
SELECT y  AS 'Queen',
       fx AS 'X Position',
       fy AS 'Y Position'
  FROM temp_queen_attack
GO
Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:23:12
Processing time 0.0224 sec