young tableaus
Young tableaus 这是 Introduction_to_algorithms一个 路学校运动, 我也难倒,互联网没有找到现有的应答。 今天 python 代码贴,供你参考。
#! /usr/bin/python
""" young tableau
m x n matrix
"""
import sys
class element():
def __init__(self, x, y):
self.x = x
self.y = y
def min_youngtableau(arry, p, m, n):
minimum = element(p.x, p.y)
if p.y < n and arry[p.x-1][p.y+1-1] < arry[p.x-1][p.y-1]:
minimum.y = p.y + 1
if p.x < m and arry[p.x+1-1][p.y-1] < arry[minimum.x-1][minimum.y-1]:
minimum.x = p.x + 1
minimum.y = p.y
if minimum.x != p.x or minimum.y != p.y:
temp = arry[p.x-1][p.y-1]
arry[p.x-1][p.y-1] = arry[minimum.x-1][minimum.y-1]
arry[minimum.x-1][minimum.y-1] = temp
p = element(minimum.x, minimum.y)
min_youngtableau(arry, p, m, n)
def extract_min(arry, m, n):
temp = arry[1-1][1-1]
arry[1-1][1-1] = 0xffff
p = element(1,1)
min_youngtableau(arry, p, m, n)
return temp
def inseart(arry, v, m, n):
if arry[m-1][n-1] != 0xffff:
print "arrary is full"
return
arry[m-1][n-1] = v
p = element(m, n)
while p.x > 1 or p.y > 1:
maxmum = element(p.x, p.y)
if p.x > 1 and arry[p.x-1-1][p.y-1] > arry[p.x-1][p.y-1]:
maxmum.x = p.x - 1
if p.y > 1 and arry[p.x-1][p.y-1-1] > arry[maxmum.x-1][maxmum.y-1]:
maxmum.x = p.x
maxmum.y = p.y-1
if maxmum.x == p.x and maxmum.y == p.y:
break
temp = arry[p.x-1][p.y-1]
arry[p.x-1][p.y-1] = arry[maxmum.x-1][maxmum.y-1]
arry[maxmum.x-1][maxmum.y-1] = temp
p = element(maxmum.x, maxmum.y)
def build_youngtaleau(arry, m, n):
arry2 = [
[0xffff, 0xffff, 0xffff],
[0xffff, 0xffff, 0xffff],
[0xffff, 0xffff, 0xffff],
[0xffff, 0xffff, 0xffff],
]
for i in range(m):
for j in range(n):
if arry[i][j] != 0xffff:
inseart(arry2, arry[i][j], m, n)
return arry2
def youngtableau_sort(arry, m, n):
arry = build_youngtaleau(arry, m, n)
result = []
for i in range(m):
for j in range(n):
result.append(extract_min(arry, m, n))
print result
if __name__ == '__main__':
arry = [
[12, 45, 50],
[34, 54, 13],
[77, 88, 99],
[87, 0xffff, 0xffff],
]
youngtableau_sort(arry, 4, 3)
版权声明:本文博客原创文章。博客,未经同意,不得转载。
young tableaus的更多相关文章
- Lesson 17 Always young
Text My aunt Jennifer is an actress. She must be at least thirty-five years old. In spit of this, sh ...
- 斯考特·杨(Scott Young)快速学习方法
上午在网上看到了斯考特·杨(Scott Young)的快速学习方法,感觉很受鼓舞. 现在已经读研究生了,可是发现自己自从上大学以来到现在,发现自己的学习方法有很大的问题. 我是个特别喜欢读书的人,在大 ...
- Young氏矩阵
一个 m x n 的Young氏矩阵是指,每一行数据都是从左到右排好序,每一列的数据也都是从上到下排好序.其中也可能存在一些INF的数据,表示不存在的元素,一个mxn的Young氏矩阵最多用来存放 r ...
- ural 1157. Young Tiler
1157. Young Tiler Time limit: 1.0 secondMemory limit: 64 MB One young boy had many-many identical sq ...
- 算法导论 第六章 思考题6-3 Young氏矩阵
这题利用二叉堆维持堆性质的办法来维持Young氏矩阵的性质,题目提示中写得很清楚,不过确实容易转不过弯来. a,b两问很简单.直接看c小问: 按照Young氏矩阵的性质,最小值肯定在左上角取得,问题在 ...
- 【杨氏矩阵+勾长公式】POJ 2279 Mr. Young's Picture Permutations
Description Mr. Young wishes to take a picture of his class. The students will stand in rows with ea ...
- The Sorrows of Young Werther
The Sorrows of Young Werther J.W. von Goethe Thomas Carlyle and R.D. Boylan Edited by Nathen Haskell ...
- Young Table(暴力,交换位置)
Young Table Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u Submi ...
- 一次young gc耗时过长优化过程
1 问题源起 上游系统通过公司rpc框架调用我们系统接口超时(默认超时时间为100ms)数量从50次/分突然上涨到2000次/分,在发生变化时间段里我们的系统也没有做过代码变更,但上游系统的调用 ...
随机推荐
- c++实现atoi()和itoa()函数(字符串和整数转化)
(0) c++类型所占的字节和表示范围 c 语言里 类型转换那些事儿(补码 反码) 应届生面试准备之道 最值得学习阅读的10个C语言开源项目代码 一:起因 (1)字符串类型转化为整数型(Integer ...
- UVA 10201 Adventures in Moving - Part IV(dp)
Problem A: Adventures in Moving - Part IV To help you move from Waterloo to the big city, you are co ...
- java实现小九机器人接口
package com.iask.webchat.chatmachine; import java.net.URLEncoder; import org.apache.http.HttpEntity; ...
- Swift - 使用Core Data进行数据持久化存储
一,Core Data介绍 1,Core Data是iOS5之后才出现的一个数据持久化存储框架,它提供了对象-关系映射(ORM)的功能,即能够将对象转化成数据,也能够将保存在数据库中的数据还原成对象. ...
- Ubuntu 环境安装整理
Ubuntu11.04下Java开发环境搭建和配置 转自:http://guoyunsky.iteye.com/blog/1175861 类似的搭建,网上一搜一大把,但每次去搜索比较麻烦.我这里就整理 ...
- 我在使用的Chrome插件
首先本人为一名Android程序员,故下面的很多插件很多都是关于开发辅助相关的.当然还有涉及到其他方面的插件,比如社交,浏览,工具等.以下按照字母排序. 1.AdBlock The most popu ...
- DFA最小化 -- Hopcroft算法 Python实现
wiki 伪代码看上去一直以为怪.发现葡萄牙语和俄罗斯语那里的 if 推断都还缺少一个条件. 国内的资料比較少.这几份学习资料不错.比我稀里糊涂的思路要好,分享下: http://www.liafa. ...
- C/C++ Resources
C/C++ Resources 一.C/C++相关资源 语言标准及文档● ISO C99http://www.open-std.org/JTC1/SC22/WG14/www/docs/n1124.pd ...
- COCOS2D-X之帧动画的一种实现Demo
这个Demo主要是实现帧动画,建议游戏中少用帧动画.废话少说直接上代码. 一.我们直接在COCOS2D-X自带的HelloCpp的工程中添加代码即可.我们在初始化中添加如下代码并附上图片资源. CCS ...
- hdu 1665 That Nice Euler Circuit(欧拉定理)
输入n个点,然后从第一个点开始,依次链接点i->点i+1,最后回到第一点(输入中的点n),求得到的图形将平面分成了多少部分. 根据欧拉定理 v_num + f_num - e_num = 2可知 ...