tableau problem

本文介绍了一个 m×n Young Tableau 的实现方法及其基本操作,如提取最小元素、插入元素等,并提供了 C 语言代码示例。Young Tableau 是一种矩阵结构,每一行从左到右递增排序,每一列从上到下递增排序。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Problem 1: Young tableau
An m × n Young tableau is an m × n matrix such that the entries of each row are in sorted order
from left to right and the entries of each column are in sorted order from top to bottom. Some of the
entries of a Young tableau may be ∞, which we treat as nonexistent elements. Thus a Young tableau
can be used to hold r ≤ mn numbers.
Here’s an example of a 4x4 Young tableau containing the elements {9, 16, 3, 2, 4, 8, 5, 14, 12}. Note that
this is not unique.

2 4 9

3 8 16
∞ ∞
5 14
∞ ∞ ∞
12



//the young tableau problem


#include<stdio.h>

#define M 4
#define N 4
#define BIGGEST 9999

int matrix[M][N]={{0,1,2,4},
{0,2,3,5},
{1,3,4,0},
{0,4,5,6}};

void print()
{
int i=0,j;
for(;i<M;i++)
{
for(j=0;j<N;j++)
printf("%d ",matrix[i][j]);
printf("\n");
}
}

void youngify(int a[M][N],int x,int y)
{
int smallx=x;
int smally=y;
if((x+1)<M&&a[x+1][y]<a[x][y])
{
smallx=x+1;
smally=y;
}
if((y+1)<N&&a[smallx][smally]>a[x][y+1])
{
smallx=x;
smally=y+1;
}
if(smallx!=x||smally!=y)
{
int temp=a[smallx][smally];
a[smallx][smally]=a[x][y];
a[x][y]=temp;
youngify(a,smallx,smally);
}
}


int extract_min()
{
int x=matrix[0][0];//get the miniest
matrix[0][0]=BIGGEST;
youngify(matrix,0,0);
return x;
}


void decreasekey(int a[M][N],int x,int y,int d)
{
if(a[x][y]<=d) return;
a[x][y]=d;
int threshold=BIGGEST;
int largestx=x;
int largesty=y;
int temp;
while((x>1||y>1)&&a[x][y]<threshold)
{
temp=a[x][y];
a[x][y]=a[largestx][largesty];
a[largestx][largesty]=temp;

x=largestx;
y=largesty;
if(x-1>=0&&a[largestx][largesty]<a[x-1][y])
{
largestx=x-1;
largesty=y;
}
if(y-1>=0&&a[largestx][largesty]<a[x][y-1])
{
largestx=x;
largesty=y-1;
}
threshold=a[largestx][largesty];
}
}


void insert(int a[M][N],int d)
{
decreasekey(a,M-1,N-1,d);
}


int main(int argc,char** argv)
{
print();
int i=extract_min();
printf("After Extract %d\n",i);
print();
printf("After Insert %d\n",5);
insert(matrix,5);
print();
return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值