Archive

Posts Tagged ‘Python’

八皇后问题的 Python 解法和多维数组的初始化

February 26th, 2010 leeing No comments

以前用 Python 写过一个八皇后的小程序,遇到过一个多维数组初始化的问题,现在整理出来。

8 queens problems

首先是八皇后问题的解决:

#
#Author:leeing
#Version:Python 3.1.1
#License:GPL V3
chess=[[0 for col in range(8)] for row in range(8)]
pos =0
i=0
flag =0  

def check(i,pos):
    if i==0:
        return True
    for counter in range(i):
        if chess[counter][pos]==1:
            return False
    for x in range(min(i,pos)):
        if chess[i-x-1][pos-x-1]==1:
            return False
    if pos<7:
        for y in range(min(i,7-pos)):
            if  chess[i-y-1][pos+y+1]==1:
                return False
    return True  

while i<8:
    chess[i][pos]=1
    if check(i,pos):
        i=i+1
        pos=0
    else:
        chess[i][pos]=0
        while pos<7 and not check(i,pos):
            pos=pos+1  

        if pos<=7 and check(i,pos):
            chess[i][pos]=1
            i=i+1
            pos=0
        else:
            if (chess[i-1].index(1))<7:
                pos = chess[i-1].index(1)+1
                chess[i-1][pos-1]=0
                i=i-1
            else:
                if i>=2:
                    pos =chess[i-2].index(1)+1
                    chess[i-1][7]=0
                    chess[i-2][pos-1]=0
                    i=i-2
                else:
                    break  

    if 1 in chess[7]:
        flag=flag+1
        print(str(flag)+" found:")
        for k in range(len(chess)):
            print(chess[k])   

        print("\n"+"*"*25)   

        if (chess[7].index(1))<7:
            pos = chess[7].index(1)+1
            chess[7][pos-1]=0
            i=7
        else:
            pos =chess[6].index(1)+1
            chess[7][7]=0
            chess[6][pos-1]=0
            i=6
print ("八皇后问题共有 "+ str(flag) + " 个解。")

运行结果如下:

.....
.....

91 found:
[0, 0, 0, 0, 0, 0, 0, 1]
[0, 0, 1, 0, 0, 0, 0, 0]
[1, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 1, 0, 0]
[0, 1, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 1, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 1, 0]
[0, 0, 0, 1, 0, 0, 0, 0]

*************************
92 found:
[0, 0, 0, 0, 0, 0, 0, 1]
[0, 0, 0, 1, 0, 0, 0, 0]
[1, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 1, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 1, 0, 0]
[0, 1, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 1, 0]
[0, 0, 0, 0, 1, 0, 0, 0]

*************************
八皇后问题共有 92 个解。

Read more…

Categories: Python, 编程语言 Tags:

Gvim 的 Python 自动补全插件 Pydiction 安装

February 6th, 2010 leeing No comments

Gvim  Pydiction 安装

  1. 放置 python_pydiction.vim 文件到 C:\Program Files\Vim\vimfiles\ftplugin 目录。
  2. 放置 complete-dict 和 pydiction.py 到 C:\Program Files\Vim\vimfiles\ftplugin\pydiction\ 目录。(这两个文件可以放在任何目录,对应于 pydiction_location 变量)
  3. 在 _vimrc 文件中加入内容:
filetype plugin on
let g:pydiction_location = 'C:/Program Files/Vim/vimfiles/ftplugin/pydiction/complete-dict'
let g:pydiction_menu_height = 20

使用时用 Tab 键就可以进行自动补全。

Categories: Python, Vim Tags: ,

EBook:《The Quick Python Book》2nd Edition

January 20th, 2010 leeing No comments
the quick python book 2nd edition
the quick python book

The Quick Python Book, Second Edition, is a clear, concise introduction to Python 3, aimed at programmers new to Python. This updated edition includes all the changes in Python 3, itself a significant shift from earlier versions of Python. The book begins with basic but useful programs that teach the core features of syntax, control flow, and data structures. It then moves to larger applications involving code management, object-oriented programming, web development, and converting code from earlier versions of Python. True to his audience of experienced developers, the author covers common programming language features concisely, while giving more detail to those features unique to Python.

格式: PDF

ISBN: 193518220X

出版日期:  2010 年 1 月 15 日 

大小:2.88 MB

下载:>> 这里 <<

Categories: Python Tags: ,