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

ZOJ Problem Set - 1002 Fire Net (Python)

闲的无聊啊,重拾ACM啊…… 可是还用C/JAVA就没意思啦。最近对Python挺感兴趣,但是一直没有机会使用一下,终于找到个支持Python的Online Judge——Zhejiang University Online Judge(ZOJ)(http://acm.zju.edu.cn),好吧,开始。

 

P1002,变形的八皇后问题,果断深度搜索。

   1 import sys

  2 
  3  class point:
  4     x=0
  5     y=0
  6      def  __init__(self,p,N):
  7         self.x=p/N
  8         self.y=p%N
  9 
 10 
 11 
 12 maxN=0
 13 
 14  def check(now,lst,p):
 15      global lstAll
 16     
 17      for t  in lst:
 18          if p.x==t.x:
 19             res=False
 20             rangeX=range(0,1)
 21              if p.y<t.y:
 22                 rangeX=range(p.y,t.y)
 23              else:
 24                 rangeX=range(t.y,p.y)
 25             
 26              for py  in rangeX:
 27                  if lstAll[p.x][py]== ' X ':
 28                     res=True
 29                      break
 30              if res==False:
 31                  return False
 32          elif p.y==t.y:
 33             res=False
 34             rangeX=range(0,1)
 35              if p.x<t.x:
 36                 rangeX= range(p.x,t.x)
 37              else:
 38                 rangeX=  range(t.x,p.x)
 39              for px  in rangeX:
 40                  if lstAll[px][p.y]== ' X ':
 41                     res=True
 42                      break
 43              if res==False:
 44                  return False
 45          else:
 46              continue
 47      return True
 48 
 49 
 50  def dg(N,now,lst):
 51      global maxN
 52      global lstAll
 53     
 54      if now==0:
 55         p=getNextPoint(None)
 56          while p!=None:
 57             lst.append(p)
 58             dg(N,now+1,lst)
 59             lst.remove(p)
 60             p=getNextPoint(p)
 61      else:
 62         p=getNextPoint(lst[now-1])
 63          while p!=None:
 64              if check(now, lst,p):
 65                 lst.append(p)
 66                 dg(N,now+1,lst)
 67                 lst.remove(p)
 68             p=getNextPoint(p)
 69          if maxN<now:
 70             maxN=now
 71     
 72 
 73  def getNextPoint(nowPoint):
 74      global N
 75      global lstAll
 76      if nowPoint==None:
 77         p=-1
 78      else:
 79         p=nowPoint.x*N+nowPoint.y
 80      while True:
 81         p=p+1
 82         np=point(p=p,N=N)
 83          if np.y==N:
 84             np.x=np.x+1
 85          if np.x==N:
 86              return None
 87          if lstAll[np.x][np.y]!= ' X ':
 88              return np
 89 
 90 
 91 
 92  # fileHandle = open ('g:/input.txt') 
 93  # N=int(fileHandle.readline())
 94  N=int(raw_input())
 95  while N!=0:
 96     lstAll=[]
 97      for i  in range(0,N):
 98          # strTemp=fileHandle.readline()
 99          strTemp=raw_input()
100         lstT=[]
101          for j  in range(0,N):
102             lstT.append(strTemp[j])
103         lstAll.append(lstT)
104     
105     dg(N,0,[])
106      print maxN;
107     maxN=0
108      # N=int(fileHandle.readline())
109      N=int(raw_input())
110  # fileHandle.close()

第一次用Python写这么长的程序啊,很多Python的特殊语法都是现查现用的,不用说,很不优美,日后一定会有改进的~ 

转载于:https://www.cnblogs.com/vistach/archive/2011/09/22/2185124.html

相关文章:

  • 计划任务不能启动
  • 传苹果产品总设计师欲离职 地位堪比乔布斯
  • 金山游戏研发改制新进展:计划本周发出正式公告
  • exec-timeout
  • 登陆系统的两个界面以及忘记root密码怎么办
  • linux发行版适合做服务器排名
  • 读取指定目录下文件-- ---LIST+递归。(跳出结构思想。利用数据结构思路+程序写出更简单的逻辑。)...
  • CentOS 6.0 安装配置rails 2.3.11 + redmine 1.2.1 笔记
  • 望江南
  • 输不起慢的代价,赢不了休息的时间
  • P42S25C查看开机次数、时间、坏点的方法
  • 关于linux颜色
  • poj3226
  • 简单使用ubuntu
  • 清除Windows Server Backup 备份副本
  • 5、React组件事件详解
  • Fastjson的基本使用方法大全
  • WePY 在小程序性能调优上做出的探究
  • 包装类对象
  • 当SetTimeout遇到了字符串
  • 构建工具 - 收藏集 - 掘金
  • 前端js -- this指向总结。
  • 数组大概知多少
  • 详解移动APP与web APP的区别
  • 一个项目push到多个远程Git仓库
  • 中文输入法与React文本输入框的问题与解决方案
  • ​人工智能之父图灵诞辰纪念日,一起来看最受读者欢迎的AI技术好书
  • ###C语言程序设计-----C语言学习(3)#
  • #NOIP 2014#day.2 T1 无限网络发射器选址
  • #免费 苹果M系芯片Macbook电脑MacOS使用Bash脚本写入(读写)NTFS硬盘教程
  • #我与Java虚拟机的故事#连载05:Java虚拟机的修炼之道
  • $(document).ready(function(){}), $().ready(function(){})和$(function(){})三者区别
  • (1综述)从零开始的嵌入式图像图像处理(PI+QT+OpenCV)实战演练
  • (2009.11版)《网络管理员考试 考前冲刺预测卷及考点解析》复习重点
  • (C语言)共用体union的用法举例
  • (Repost) Getting Genode with TrustZone on the i.MX
  • (安全基本功)磁盘MBR,分区表,活动分区,引导扇区。。。详解与区别
  • (八)光盘的挂载与解挂、挂载CentOS镜像、rpm安装软件详细学习笔记
  • (二)基于wpr_simulation 的Ros机器人运动控制,gazebo仿真
  • (附源码)springboot人体健康检测微信小程序 毕业设计 012142
  • (规划)24届春招和25届暑假实习路线准备规划
  • (利用IDEA+Maven)定制属于自己的jar包
  • (五) 一起学 Unix 环境高级编程 (APUE) 之 进程环境
  • (幽默漫画)有个程序员老公,是怎样的体验?
  • (原創) 如何將struct塞進vector? (C/C++) (STL)
  • (转) ns2/nam与nam实现相关的文件
  • (转)重识new
  • (转载)OpenStack Hacker养成指南
  • * 论文笔记 【Wide Deep Learning for Recommender Systems】
  • .describe() python_Python-Win32com-Excel
  • .NET Core IdentityServer4实战-开篇介绍与规划
  • .NET Core6.0 MVC+layui+SqlSugar 简单增删改查
  • .net 程序发生了一个不可捕获的异常
  • .NET 的程序集加载上下文
  • .NET 事件模型教程(二)