当前位置: 首页 > news >正文

25fall做题记录 - November - Amy

2025.11.1

括号生成

向右或向上走,不能超过对角线。
Catalan数枚举。

class Solution:def generateParenthesis(self, n: int) -> list[str]:ans=[]def dfs(l,res,r):if(l<r):returnif(l+r==2*n-1):ans.append(res+')')returnif(l<n):dfs(l+1,res+'(',r)if(l>r):dfs(l,res+')',r+1)dfs(1,"(",0)return ans
print(Solution().generateParenthesis((3)))

N 皇后

class Solution:def solveNQueens(self, n: int) -> list[list[str]]:y=[0]*nz=[0]*(2*n+1)w=[0]*(2*n+1)ans=[]def dfs(x,res):if(x==n):tmp=[]for i in range(n):t='.'*res[i]+'Q'+'.'*(n-1-res[i])tmp.append(t)ans.append(tmp)returnfor i in range(n):if(y[i]==0 and z[i+x]==0 and w[i-x+n]==0):y[i]=1z[i+x]=1w[i-x+n]=1temp=res.copy()temp.append(i)dfs(x+1,temp)y[i]=0z[i+x]=0w[i-x+n]=0dfs(0,[])return ans
print(Solution().solveNQueens(4))

熄灯问题

根据题目提示,枚举第一行的所有情况,然后一行行往下推,直到遇到刚好全部关闭的情况。
二维数组的深拷贝需用copy库中的deepcopy().

from copy import deepcopy
a=[]
a.append([0]*8)
for i in range(5):t=list(map(int,input().split()))t.insert(0,0)t.append(0)a.append(t)
a.append([0]*8)
qaq=deepcopy(a)
ans=[[0 for _ in range(8)]for _ in range(7)]
dx = [0, 1, 0, -1]
dy = [1, 0, -1, 0]
def button(i,j):for k in range(4):x0 = i + dx[k]y0 = j + dy[k]a[x0][y0] ^= 1a[i][j] ^= 1ans[i][j] ^= 1
def change():for i in range(2,6):for j in range(1,7):if(a[i-1][j]==1):button(i,j)
for i in range(1<<6):a=deepcopy(qaq)ans = [[0 for _ in range(8)] for _ in range(7)]# if(i==37):#     for j in range(1,6):#         print(" ".join(map(str, a[j][1:7])))#     print("TEST")for k in range(6):if(i&(1<<k)):button(1,k+1)# if(i==37):#     for j in range(1,6):#         print(" ".join(map(str, a[j][1:7])))#     print("TEST")change()if(sum(a[5][1:7])==0):for j in range(1, 6):print(" ".join(map(str,ans[j][1:7])))break# if(i==37):#     for j in range(1,6):#         print(" ".join(map(str, a[j][1:7])))#     print("TEST")#     for j in range(1,6):#         print(" ".join(map(str, ans[j][1:7])))

2025.11.4

马走日

t=int(input())
dx=[-2,-1,1,2,2,1,-1,-2]
dy=[1,2,2,1,-1,-2,-2,-1]
ans=0
def dfs(x,y,step):global ansif(step==m*n):ans+=1returnfor i in range(8):x0=x+dx[i]y0=y+dy[i]if(0<=x0<n and 0<=y0<m and vis[x0][y0]==0):vis[x0][y0]=1dfs(x0,y0,step+1)vis[x0][y0]=0
for i in range(t):n,m,x,y=map(int,input().split())vis=[[0 for _ in range(m)]for _ in range(n)]vis[x][y]=1ans=0dfs(x,y,1)print(ans)

排列

1.康托展开+逆康托展开。还原的过程中使用小根堆。阶乘使用math库中的factorial.
康托展开
2.image
image

from math import factorial
import heapq
m=int(input())
for p in range(m):n,k=map(int,input().split())a=list(map(int,input().split()))ans=[]t=0count=[0]*nfor i in range(n):for j in range(i,n):if(a[j]<a[i]):count[i]+=1for i in range(n):t+=factorial(n-1-i)*count[i]t+=kt=(t+1)%(factorial(n))-1for i in range(n):temp=t//factorial(n-1-i)ans.append(temp)t=t-temp*factorial(n-1-i)res=[]nums=[]heapq.heapify(nums)for i in range(1,n+1):nums.append(i)for i in range(n):qwq=nums[ans[i]]res.append(qwq)nums.remove(qwq)print(" ".join(map(str,res)))
http://www.jsqmd.com/news/31357/

相关文章:

  • YACS2025年10月乙组
  • Google Driver 读写 excel
  • 2025年河南镶牙机构权威推荐榜单:河南老人镶牙机构源头精选
  • Windows11升级专业版密钥
  • 分享一个自动化进行Oracle 重做日志组管理的脚本
  • 强化学习值函数与策略搜索两种方法对比和疑问解读
  • 中文分词手艺全解析
  • 2025qwb 线上赛wp
  • 2025年钢带波纹管批发厂家权威推荐榜单:hdpe钢带波纹管/钢带管/钢带增强聚乙烯螺旋波纹管源头厂家精选
  • 11.4每日总结
  • 深入解析:探索大语言模型(LLM):一文读懂通用大模型的定义、特点与分类
  • 2025年聚氨酯预聚体公司新排行榜,浇注聚氨酯原材料企业推荐
  • 2025年乐博智家保鲜盒直销厂家权威推荐榜单:乐博智家冰沙杯/乐博智家炒冰机/乐博智家刨冰机源头厂家精选
  • 2025 年打标机厂家最新推荐排行榜:结合协会测评权威数据,聚焦技术创新与行业适配的优质品牌全解析手持/点阵/金属/铭牌打标机公司推荐
  • 2025年注射成型烧结炉生产厂商新排行榜,碳化硅反应烧结炉厂家推荐
  • 多项式学习小记
  • Oracle Exadata存储节点主动替换磁盘最佳实践
  • 计算机视觉的数据收集与标注 - 实践
  • 2025年东北围挡租售公司口碑排名:八达围挡租售基地
  • 训练现象
  • 2025年五大豪宅床垫源头工厂推荐,实力品牌全解析
  • AI驱动全链路监测精确防护:构建新一代政务数据安全平台
  • 河北金属家具企业口碑排名:河北优美金属客户评价如何?
  • 【为美好CTF献上祝福】 ISCTF2024 逆向笔记
  • 2025年宾馆布草实力厂家年度排行榜,宾馆布草生产商推荐
  • 2025中国API安全产品全景解析:厂商排名与发展趋势
  • 2025年交通涂料厂家推荐排名,艾仕得客车交通涂料电话多少
  • Python uv 包管理
  • 2025年消防器材十大品牌推荐排行榜白皮书
  • 运营商数据治理新范式:AI大模型赋能的低成本场景适配分类分级系统