S
S
svarnoi4202015-05-22 17:29:34
Java
svarnoi420, 2015-05-22 17:29:34

Search by cells in a table?

For example, there is a table like this:
3 0 0 7
0 4 0 0
0 0 5 6
You need to build a closed loop (cycle). You should get a certain square (rectangle), roughly speaking.
The algorithm, in theory, is as follows:
Find any two cells with a non-zero load (necessarily in different rows and columns) and give them a "-" sign. Values ​​will be subtracted from them (the minimum value of two cells is 3).
To close the path, select two more cells (whether with values ​​or empty) that will form a rectangle with the previous two and give them a "+" sign. We add a value to them (that is, also the minimum number - 3).
As a result, it should turn out, for example, like this:
-3 0 0 +7
0 4 0 0
+0 0 5 -6
After manipulations:
0 0 0 10
0 4 0 0
3 0 5 3
I tried a different algorithm (Java implementation if anything, just the first sandbox that came to mind):
jsfiddle.net/bn55ftca
Result:
-3 +0 0 7
+0 - 4 0 0
0 0 5 6
All is well too.
But it breaks down when I issue a random table. Spin endlessly.
Nevertheless, it is necessary to implement it in such a way that at first only two minuses are placed in non-zero, and then pluses.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
svarnoi420, 2015-05-28
@inomdzhon92

solved the issue

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question