用python代码编写象棋界面,棋盘覆盖问题

编写象棋界面

import turtle
t=turtle.Pen()
t.speed(100)
def angle(x,y):
    t.penup()
    t.goto(x+3,y+3)
    t.pendown()
    t.setheading(0)
    t.forward(5)
    t.goto(x+3,y+3)
    t.left(90)
    t.forward(5)
    t.penup()
    t.goto(x+3,y-3)
    t.pendown()
    t.setheading(0)
    t.forward(5)
    t.goto(x+3,y-3)
    t.left(90)
    t.forward(-5)
    t.penup()
    t.goto(x-3,y+3)
    t.pendown()
    t.setheading(0)
    t.forward(-5)
    t.goto(x-3,y+3)
    t.left(90)
    t.forward(5)
    t.penup()
    t.goto(x-3,y-3)
    t.pendown()
    t.setheading(0)
    t.forward(-5)
    t.goto(x-3,y-3)
    t.left(90)
    t.forward(-5)
def v(x,y):
    t.penup()
    t.goto(x+3,y+3)
    t.pendown()
    t.setheading(0)
    t.forward(5)
    t.goto(x+3,y+3)
    t.left(90)
    t.forward(5)
    t.penup()
    t.goto(x+3,y-3)
    t.pendown()
    t.setheading(0)
    t.forward(5)
    t.goto(x+3,y-3)
    t.left(90)
    t.forward(-5)
    t.penup()
def a(x,y):
    t.penup()
    t.goto(x-3,y+3)
    t.pendown()
    t.setheading(0)
    t.forward(-5)
    t.goto(x-3,y+3)
    t.left(90)
    t.forward(5)
    t.penup()
    t.goto(x-3,y-3)
    t.pendown()
    t.setheading(0)
    t.forward(-5)
    t.goto(x-3,y-3)
    t.left(90)
    t.forward(-5)
#1.绘制所有横线
t.penup()
t.goto(-80,90)
t.pendown()
for i in range(1,6,1):
    t.forward(160)
    t.penup()
    t.right(90)
    t.forward(20)
    t.right(90)
    t.pendown()
    t.forward(160)
    t.penup()
    t.left(90)
    t.forward(20)
    t.left(90)
    t.pendown()
#2.绘制所有竖线
t.left(90)
t.penup()
t.forward(20)
t.pendown()
for i in range(1,5,1):
    t.forward(80)
    t.penup()
    t.forward(20)
    t.pendown()
    t.forward(80)
    t.right(90)
    t.forward(20)
    t.right(90)
    t.forward(80)
    t.penup()
    t.forward(20)
    t.pendown()
    t.forward(80)
    t.left(90)
    t.forward(20)
    t.left(90)
t.forward(180)
t.left(90)
t.forward(160)
t.left(90)
t.forward(180)
#3.绘制斜线
t.left(90)
t.forward(60)
t.left(45)
t.forward(40*1.414)
t.left(45)
t.forward(-40)
t.left(45)
t.forward(40*1.414)
t.penup()
t.goto(-20,90)
t.pendown()
t.right(180)
t.forward(40*1.414)
t.right(45)
t.forward(-40)
t.right(45)
t.forward(40*1.414)
#4.绘制炮和兵的位置
angle(60,50)
angle(-60,50)
angle(60,-50)
angle(-60,-50)
angle(40,30)
angle(-40,30)
angle(40,-30)
angle(-40,-30)
angle(0,30)
angle(0,-30)


a(80,30)
a(80,-30)
v(-80,-30)
v(-80,30)
#5.绘制外围线   绘制一个长方形,设置笔的粗细
t.penup()
t.goto(-90,-100)
t.pendown()
t.pensize(10)
t.forward(200)
t.right(90)
t.forward(180)
t.right(90)
t.forward(200)
t.right(90)
t.forward(180)
t.right(90)

棋盘覆盖问题

在2^k*2^k个方格组成的棋盘中,有一个方格被占用,用下图的4种L型骨牌覆盖所有棋盘上的其余所有方格,不能重叠。

        代码如下:

def chess(tr,tc,pr,pc,size):
global mark 
global table
mark+=1
count=mark
if size==1:
return
half=size//2
if pr<tr+half and pc<tc+half:
chess(tr,tc,pr,pc,half)
else:
table[tr+half-1][tc+half-1]=count
chess(tr,tc,tr+half-1,tc+half-1,half)
if pr<tr+half and pc>=tc+half:
chess(tr,tc+half,pr,pc,half)
else:
table[tr+half-1][tc+half]=count
chess(tr,tc+half,tr+half-1,tc+half,half)
if pr>=tr+half and pc<tc+half:
chess(tr+half,tc,pr,pc,half)
else:
table[tr+half][tc+half-1]=count
chess(tr+half,tc,tr+half,tc+half-1,half)
if pr>=tr+half and pc>=tc+half:
chess(tr+half,tc+half,pr,pc,half)
else:
table[tr+half][tc+half]=count
chess(tr+half,tc+half,tr+half,tc+half,half)

def show(table):
n=len(table)
for i in range(n):
for j in range(n):
print(table[i][j],end='    ')
print('')

mark=0
n=8
table=[[-1 for x in range(n)] for y in range(n)]
chess(0,0,2,2,n)
show(table)

n是棋盘宽度,必须是2^k,本例中n=8,特殊格子在(2,2)位置,如下图所示:

采用分治法每次把棋盘分成4份,如果特殊格子在这个小棋盘中则继续分成4份,如果不在这个小棋盘中就把该小棋盘中靠近中央的那个格子置位,表示L型骨牌的1/3占据此处,每一次递归都会遍历查询4个小棋盘,三个不含有特殊格子的棋盘置位的3个格子正好在大棋盘中央构成一个完整的L型骨牌,依次类推,找到全部覆盖方法。运行结果如下:

 

posted @ 2018-12-07 15:18  老虎死了还有狼  阅读(2934)  评论(0编辑  收藏  举报