用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写法,并且把它的覆盖效果显示出来 二.关 ...
随机推荐
- 123457123457#0#-----com.tym.PuzzleGame28--前拼后广--日常pt-tym
com.tym.PuzzleGame28--前拼后广--日常pt-tym
- SQL Server 2014 清除用户名和密码
网上找来找去都是SQL Server 2008版本或者以前版本的... 后来:http://stackoverflow.com/questions/349668/removing-the-rememb ...
- Js把对象数组列表转换成数组
今天写组件的时候遇到一个问题,就是当我需要对获取到的对象列表进行删减的时候,发现没有合适的方法,比如: //获取图片列表 var imgs = document.getElementsByTagNam ...
- No section matches selector - no section to be FIRST/LAST
1. 使用KEIL MDK ,STM32F405RG,编译的时候报错 .\Objects\ks3620_stm32f405_proj.sct(): error: L6236E: No section ...
- 15点睛Spring4.1-TaskExecutor
转发:https://www.iteye.com/blog/wiselyman-2212679 15.1 TaskExecutor spring的TaskExecutor为在spring环境下进行并发 ...
- RabbitMQ官方教程三 Publish/Subscribe(GOLANG语言实现)
RabbitMQ官方教程三 Publish/Subscribe(GOLANG语言实现) 在上一个教程中,我们创建了一个工作队列. 工作队列背后的假设是,每个任务都恰好交付给一个worker处理. 在这 ...
- centos umount 卸载出错
target is busy. (In some cases useful info about processes that use the device ) or fuser()) 解决 fuse ...
- Android模拟器Genymotion安装使用教程详解
一.注册\登录 打开Genymotion官网,https://www.genymotion.com/ ,首先点击右上角的Sign in进行登录操作.如何登录就不细讲了,下面讲一下如何注册(备注:注册按 ...
- Linux下的Curses库的源码下载与安装
curses库是可以在linux终端中写出字符用户界面的一个库,现在较新的版本应该是ncurses库,现在已经很少有人再去使用他了,所以想拥有这个库并且在linux下写出自己用户界面的可以参考一下本博 ...
- GSVA的使用
GSVA的简介 Gene Set Variation Analysis,被称为基因集变异分析,是一种非参数的无监督分析方法,主要用来评估芯片核转录组的基因集富集结果.主要是通过将基因在不同样品间的表达 ...