用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型骨牌,依次类推,找到全部覆盖方法。运行结果如下:

用python代码编写象棋界面,棋盘覆盖问题的更多相关文章
- Python代码编写规范
Python代码编写规范 编码: a) 如无特殊情况,文件一律使用UTF-8编码 b) 如无需特殊情况,文件头部必须加入#-*-coding:utf-8-*- 缩进 a) 统一 ...
- 150+行Python代码实现带界面的数独游戏
150行代码实现图形化数独游戏 Github地址,欢迎各位大佬们fork.star啥的,感谢: 今天闲着没事干,以前做过html+js版的数独,这次做个python版本的,界面由pygame完成,数独 ...
- 利用Python代码编写计算器小程序
import tkinter import tkinter.messagebox import math class JSQ: def __init__(self): #创建主界面 self.root ...
- python 代码编写规范
一 代码编排1 缩进.4个空格的缩进(编辑器都可以完成此功能),不使用Tap,更不能混合使用Tap和空格.2 每行最大长度79,换行可以使用反斜杠,最好使用圆括号.换行点要在操作符的后边敲回车.3 类 ...
- Python代码编写规范,你真的会吗?
前言 本文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理.作者:yangjiajia123456 最近两年的工作都是和运维相关,有时 ...
- 小白突破百度翻译反爬机制,33行Python代码实现汉译英小工具!
表弟17岁就没读书了,在我家呆了差不多一年吧. 呆的前几个月,每天上网打游戏,我又不好怎么在言语上管教他,就琢磨着看他要不要跟我学习Python编程.他开始问我Python编程什么?我打开了我给学生上 ...
- python代码 构建验证码
1.python代码编写 (随机验证码): #coding: utf-8 import Image, ImageDraw, ImageFont, ImageFilter import string, ...
- 使用python制作ArcGIS插件(2)代码编写
使用python制作ArcGIS插件(2)代码编写 by 李远祥 上一章节已经介绍了如何去搭建AddIn的界面,接下来要实现具体的功能,则到了具体的编程环节.由于使用的是python语言进行编程,则开 ...
- JavaScript编写棋盘覆盖
一.前言 之前做了一个算法作业,叫做棋盘覆盖,本来需要用c语言来编写的,但是因为我的c语言是半桶水(哈哈),所以索性就把网上的c语言写法改成JavaScript写法,并且把它的覆盖效果显示出来 二.关 ...
随机推荐
- Ubuntu18.04 instsall XMind_8 and crack
1.dowload XMind_8 linux install zip wget https://www.xmind.cn/xmind/downloads/xmind-8-update8-linux. ...
- spring @Transactional的自调用失效问题与事务的典型错误用法剖析
@Transactional的自调用失效问题 有时候配置了注解@Transactional,但是它会失效,这里要注意一些细节问题,以避免落入陷阱. 注解@Transaction的底层实现是Spring ...
- node.js http-server 搭建本地服务器
使用vue-cli创建的项目,能够实现浏览器中自动刷新,实时查看项目效果,其中的原理在于,webpack在本地启动了一个本地服务器,将本机当作一台服务器: 打包后的文件是一个html静态页面,在本地文 ...
- 斐波那契数列&&上台阶
使用装饰器的场景 当我们想对多个函数增加一个相同的功能时,例如计数统计,缓存计算结果,记录日志等 # coding:utf-8 # [题目1] # 斐波那契数列 又称黄金分割数列,指的是这样的一个数列 ...
- 实验1 C 语言开发环境使用和数据类型、运算符、表达式
# include <stdio.h> int main() { int x; printf("x:\n"); scanf("%d",&x) ...
- 【GStreamer开发】GStreamer基础教程10——GStreamer工具
目标 GStreamer提供了一系列方便使用的工具.这篇教程里不牵涉任何代码,但还是会讲一些有用的内容: 如何在命令行下建立一个pipeline--完全不使用C 如何找出一个element的Capab ...
- 19-js策略模式
var PriceStrategy = function() { var stragtegy = { return30: function(price) { return +price + parse ...
- social engineering toolkit
1. freebuf介绍 http://www.freebuf.com/sectool/73409.html 2. github https://github.com/trustedsec/socia ...
- 使用派生镜像(qcow2)
当创建的虚拟机越来越多,并且你发现好多虚拟机都是同一个操作系统,它们的区别就是安装的软件不大一样,那么你肯定会希望把他们公共的部分提取出来,只保存那些与公共部分不同的东西,这样镜像大小下去了,空间变多 ...
- hdu 2476 题解
题目 题意 给出两个字符串 $ s1,s2 $,每次操作可以使一段连续的子串全变成一个字母,问最少多少次操作可以使 $ s1 $ 变为 $ s2 $. 例如 $ zzzzzfzzzzz $,长度为 $ ...