拉丁矩阵问题(回溯法),我想要程序?

来源:百度知道 编辑:UC知道 时间:2024/07/02 16:52:43
现有n种不同形状的宝石,每种宝石有足够多颗。欲将这些宝石排列成m行n列的一个矩阵,m n,使矩阵中每一行和每一列的宝石都没有相同形状。试设计一个算法,计算出对于给定的m和n有多少种不同的宝石排列方案。

#include"iostream"
using namespace std;
int **x;
int count=0;
int s;
int t;
int m;
int n;
int no=0;
void OutPut()
{
for(int i=1;i<=m;i++)
{
for(int j=1;j<=n;j++)
cout<<x[i][j]<<ends;
cout<<endl;
}
// cout<<endl;
}
void swap(int &a,int &b)//要用引用
{
int p;
p=a;
a=b;
b=p;
}
int OK(int**x,int s,int t)
{
for(int k=1;k<s;k++)
for(int j=1;j<=n;j++)
if(x[k][j]==x[s][j]) return 0;
return 1;
}
void Backtrack(int s,int t)
{
int j;
if(s>m)
{
count++;
// no++;
// cout<<"矩阵:"<<no<<endl;
// OutPut();
return;
}

if(t>n)
{
if(OK(x,s,t))
Backtrack(s+1,1);
}
for(j=t;j<=n;j++)
{
swap(x[s][j],x[s][t]);
Backtr