矩阵运算逻辑 [英] Matrix Manipulation Logic

查看:92
本文介绍了矩阵运算逻辑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

void project::step_nine()
{
	int **faltu_matrix;
	faltu_matrix = new int *[temp_tasks];
	for( int i=0; i<temp_tasks; i++)
	{
		faltu_matrix[i] = new int[temp_processors];
	}
	for(int i=0; i<temp_tasks;i++)
	{
		for(int j=0; j<temp_processors;j++)
		{
			faltu_matrix[i][j] = 0;
		}
	}

	for(int i=0; i<temp_tasks;i++)
	{
		for(int j=0; j<temp_processors;j++)
		{
			if(temp_Matrix[i][j] == 0)
			{
				faltu_matrix[i][j] =1;
			}
			
		}
	}

	for(int i=0; i<temp_tasks;i++)
	{
		for(int j=0; j<temp_processors;j++)
		{
			cout<<faltu_matrix[i][j]<<"    ";
		}
		cout<<endl;
	}	
}



假设我有一个矩阵

0 1 0 0 2
0 0 0 4 0
5 0 2 1 3
4 4 5 0 0
0 3 2 0 2
通过选择一组零来选择匹配项,以便每一行或每一列都只有一个被选择.这意味着我应该从每一行和每一列中只选择一个零!
我考虑了我的几种逻辑,但是每个逻辑都有问题!请帮助我!由于这个愚蠢的步骤,我的项目在此时严重卡住了!
我必须制作一个新的矩阵2d,其中应以唯一的方式选择行和列中的零!!
在这种情况下,输出应为
1 0 0 0 0
0 0 1 0 0
0 1 0 0 0
0 0 0 0 1
0 0 0 1 0
问题IAM面对:

我所做的实现存在以下问题:如果在第二行中选择第二个元素
1 0 0 0 0
0 1 0 0 0
然后对于第三行,由于原始矩阵中没有零来维持唯一性而违反了该规则.



suppose i got a matrix

0 1 0 0 2
0 0 0 4 0
5 0 2 1 3
4 4 5 0 0
0 3 2 0 2
Select a matching by choosing a set of zeros so that each row or column has only one selected. It means that i should have only one zero selected from each row and column!
i implemented several of my logics in mind but there comes a problem in every one! kindly help me! my project is badly stuck at this point just because of this stupid step!
i have to make a new matrix 2d in which the zeros from rows and columns should be selected in a way that they are unique!!

like in this case output should be
1 0 0 0 0
0 0 1 0 0
0 1 0 0 0
0 0 0 0 1
0 0 0 1 0
PROBLEM IAM FACING:

the implementation i did had the problem that if in the 2nd row i select the second element
1 0 0 0 0
0 1 0 0 0
then for row three the rule was violated as there was no zero in the original matrix to maintain uniqueness

推荐答案

:
您遍历各行,在每一行中选择第一个零,到达最后一行后,您将得到以下内容:
1.选择的第一个零
2.选择的第一个零
...
N.首先选择零个
然后检查此选择是否满足要求,如果是,则完成,否则,请在最后一行中选择第二个零(如果有):
1.第一个零
2.第一个零
...
N.第二个零
并再次检查(如果现在适合),则完成;否则,请选择下一个零,依此类推,直到用完零为止.然后,您将在上一行中选择第二个零,并在最后一行中选择第一个:
1.第一个零
2.第一个零
...
(N-1).第二个零
N.第一个零
然后再次检查它是否适合,如果适合,则完成,如果不合适,请移至下一个零:
1.第一个零
2.第一个零
...
(N-1).第二个零
N.第二个零
依此类推,直到找到有效的设置或组合用完为止.我认为这称为引用算法".可以相对容易地通过递归实现.
How about this, there''re probably much better solutions though:
You iterate through the rows, in every row you select the very first zero, after you reach the last row, you have this:
1. first zero selected
2. first zero selected
...
N. first zero selected
Then you check if this selection fits the requirement, if yes, then you are done, if not, then you select the second zero (if any) in the last row:
1. first zero
2. first zero
...
N. second zero
and check again, if it fits now, you are done, if not, then you select the next zero and so on until you run out of zeroes. Then you will select the second zero on the previous row and the first in the last row:
1. first zero
2. first zero
...
(N-1). second zero
N. first zero
And check again if it fits, if it does, you are done, if it doesn''t, you move on to the next zero:
1. first zero
2. first zero
...
(N-1). second zero
N. second zero
and so on until you either find a setup that works or you run out of combinations. I think this is called a "trackback algorithm". Can be relatively easily implemented with recursion.


上面的解决方案也可以像(n)个数字一样实现.

1.从该行的0位置的每个数字开始.

2.测试看看这是否是解决方案.每个数字应该是唯一的.

3.然后像增加基数(n)一样增加它.但是,如果数字不为零,则保持递增.如果自动换行,请携带"10"并增加下一列.

4.这是解决方案吗?
一个.是->退出
b.错误->转到3.
C.用零位数字包裹吗? ->退出,无解.
The above solution could also be implemented like a (n) digit number.

1. Start with each digit at the location of the 0 for the row.

2. Test to see if this a solution. Each digit should be unique.

3. Then increment it like you would a base (n) number. But if the digit doesn''t fall on a zero, then keep incrementing. If it wraps, carry the ''10'' and increment the next column.

4. Is this the solution?
a. True -> Quit
b. False -> goto 3.
c. Wrapped on zero digit? -> Quit, no solution.


这篇关于矩阵运算逻辑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆