C 中的矩阵问题

发布于 2024-09-29 18:57:31 字数 434 浏览 3 评论 0原文

在具有 k 个对象的 mxn 矩阵中,将对象放置在矩阵单元中的方式有​​多少种。(k≤n,m)。举个更好的例子,如果“k”个物体中的第一个物体被放置在位置(1,1),那么下一个物体就不能被放置在第一列或第一行,并且对于其余的剩余对象。

问题的输出将类似于:(第 k 个对象,第 n 行,第 m 列)即 (3,3,4) 或者非正式地说,“k 个对象可以有多少种方式放置在 (nxm) 矩阵单元上。

我找到了工作规则,比如: [n(n-1)(n-2)...(n-(k-1))][m(m-1)(m-2)...(m-(k-1))] -->这将为我提供在应用约束的情况下,可以将 k 个对象放置在单元格中的确切方法数。

但我无法构造“嵌套 for 循环”条件: 对于(对象) 对于(行) 对于(列)

我正在使用 C !

在构建代码时需要一些帮助!

In an mxn matrix having k objects, what are the number of ways that an object be placed in a matrix cell.(k<=n,m). By giving an example the more better illustrated, if the 1st object out of the "k" objects was placed at location (1,1),then the next object could not be placed on the 1st column or on the 1st row, and on for rest of the remaining objects.

The output of the problem will be like: (kth object, nth row, mth column) i.e., (3,3,4)
or informally,"How many ways can k objects be placed on (nxm) matrix cells.

I found out the working rule,say :
[n(n-1)(n-2)...(n-(k-1))][m(m-1)(m-2)...(m-(k-1))] --> this will give me exactly the number of ways, k objects can be placed in the cell, with the applied constraints.

But i can't construct the "nested for-loop" condition:
for(object)
for(row)
for(column)

I AM USING C !

need some help in constructing the code !

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

深巷少女 2024-10-06 18:57:31

正如我所说的这里,实现这个

/* n,m,k are constants */
int rook() {
    int i, j, mem[m+1][k+1];
    for (i=0; i<=m; i++)
        mem[i][0] = 1;
    for (j=1; j<=k; j++)
        mem[0][j] = 0;
    for (i=1; i<=m; i++)
        for (j=1; j<=k; j++)
            mem[i][j] = mem[i-1][j] + (n-j+1)*mem[i-1][j-1];
    return mem[m][k];
}

像往常一样,您可以对其进行优化以使用 O(k) 空间。

As I said here, just implement this.

/* n,m,k are constants */
int rook() {
    int i, j, mem[m+1][k+1];
    for (i=0; i<=m; i++)
        mem[i][0] = 1;
    for (j=1; j<=k; j++)
        mem[0][j] = 0;
    for (i=1; i<=m; i++)
        for (j=1; j<=k; j++)
            mem[i][j] = mem[i-1][j] + (n-j+1)*mem[i-1][j-1];
    return mem[m][k];
}

As usual, you can optimize this to use O(k) space.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文