程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Luogu pythonp1228 carpet filling problem divide and conquer

編輯:Python

import math
k=int(input())
x,y=map(int,input().split())
length=pow(2,k)
#x1,y1 Where the princess is x2,y2 The upper left corner of the square
def solve(x1,y1,x2,y2,length):
if(length==1):
return
if(x1-x2<=length/2-1 and y1-y2<=length/2-1):# top left corner
print(int(x2+length/2),end=' ')
print(int(y2+length/2),end=' ')
print(1)
solve(x1,y1,x2,y2,length/2)# Upper left square
solve(x2+length/2-1,y2+length/2,x2,y2+length/2,length/2)# Lower left square
solve(x2+length/2,y2+length/2-1,x2+length/2,y2,length/2)# The upper right
solve(x2+length/2,y2+length/2,x2+length/2,y2+length/2,length/2)# Small square at the bottom right
elif (x1-x2<=length/2-1 and y1-y2>length/2-1):# The lower left corner
print(int(x2+length/2),end=' ')
print(int(y2+length/2-1),end=' ')
print(2)
solve(x2+length/2-1,y2+length/2-1,x2,y2,length/2)# Upper left square
solve(x1,y1,x2,y2+length/2,length/2)# Lower left square
solve(x2+length/2,y2+length/2-1,x2+length/2,y2,length/2)# The upper right
solve(x2+length/2,y2+length/2,x2+length/2,y2+length/2,length/2)# Small square at the bottom right
elif (x1-x2>length/2-1 and y1-y2<=length/2-1):# Upper right corner
print(int(x2+length/2-1),end=' ')
print(int(y2+length/2),end=' ')
print(3)
solve(x2+length/2-1,y2+length/2-1,x2,y2,length/2)# Upper left square
solve(x2+length/2,y2+length/2,x2,y2+length/2,length/2)# Lower left square
solve(x1,y1,x2+length/2,y2,length/2)# The upper right
solve(x2+length/2,y2+length/2,x2+length/2,y2+length/2,length/2)# Small square at the bottom right
else:
print(int(x2+length/2-1),end=' ')
print(int(y2+length/2-1),end=' ')
print(4)
solve(x2+length/2-1,y2+length/2-1,x2,y2,length/2)# Upper left square
solve(x2+length/2-1,y2+length/2,x2,y2+length/2,length/2)# Lower left square
solve(x2+length/2,y2+length/2-1,x2+length/2,y2,length/2)# The upper right
solve(x1,y1,x2+length/2,y2+length/2,length/2)# Small square at the bottom right
solve(x,y,1,1,length)

 


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved