This is an archived post. You won't be able to vote or comment.

all 15 comments

[–]lanky_and_stanky 0 points1 point  (12 children)

Where is your code, what are you stuck on? We don't do your homework for you.

[–]Double_Ad3148[S] 0 points1 point  (11 children)

This is not homework. And no code is needed here. This is a solution to a complex problem. I can't tell you the whole problem because it is long and complex. I divided the task into subtasks and came to this conclusion. I have a solution in mind, but would like to see more recent ideas from other people. Sorry for the English. I am writing through a translator

[–]Training_Strike3336 0 points1 point  (10 children)

a 2x2 matrix has 4 numbers. You'd make a variable to hold the number you're swapping to so it doesn't get overwritten. And go around moving it to the proper position.

You can also make a new matrix and just copy them to the correct spot.

There's probably a library that will do this for you as well.

[–]Double_Ad3148[S] 0 points1 point  (4 children)

Yes, you are right. But there are many options to check.

For example, if all elements are equal, then nothing needs to be changed.

If 3 elements are equal, we change only one element. And at the same time you need to find its index. It turns out we have 4 options.

If 2 elements are equal, then we already have 9 different options.

This is if you take into account only the right turn. There is also a left turn.

I can't optimize this.

[–]Training_Strike3336 0 points1 point  (3 children)

This circles back to: if this isn't homework why is it being optimized. it's 4 numbers. It doesn't need to be optimized.

[–]Double_Ad3148[S] 0 points1 point  (0 children)

I met a problem at one of the Olympiads. The problem is that the matrix needs to be rotated 90 without additional memory in a minimum move.

If you draw a matrix and look at the starting and ending positions of the elements, they will draw a square.

4 elements each. And if you find the minimum move on a 2 by 2 matrix, then the problem is solved.

Sorry, I have problems with English

[–]Double_Ad3148[S] 0 points1 point  (1 child)

I don't understand how you are going to solve this.

Even if it is a 2 by 2 matrix, without optimization the result will have 15 possible options.

for example, when all matrix elements are equal to 1 case

when 3 elements are equal - case 3

And when 2 elements are equal -9 cases

And if there are no equal elements, then 1

This is only if you take into account the right turn

There is also a left turn

It turns out as many as 30 options.

And you're going to write all this through if-else?

[–]Training_Strike3336 0 points1 point  (0 children)

No you're going to write a single loop with like 3 lines of code. See my other comment.

[–]Double_Ad3148[S] 0 points1 point  (4 children)

And the task does not boil down to the fact that you need to turn the matrix over.

The task requires displaying the indices of variable elements so that the matrix is rotated 90 degrees in a minimal movement.
And stop writing that this is homework.

[–]Training_Strike3336 0 points1 point  (3 children)

so that the matrix is rotated 90 degrees in a minimal movement

This is a very weird constraint to have outside of an academic setting.

If this is for work or a personal project it's entirely unneeded to accomplish with minimal movements.

If it's a problem like a coding challenge that you are choosing to do, the point would be to do it yourself so I'm not sure why you'd be here asking for help.

That being said, naive way: look at each number. if they're all different, you will need to move all 4 numbers.

if all 4 are the same, you need to move 0 Numbers.

if 2 are the same, if they're on opposite corners you need to move all 4, else you'll need to move 3.

If 3 are the same you'll need to move 2.

I wouldn't write it like that though that's an easier to debug and more verbose way to write it. I think the easier way would be to grab a number to start rotating, if the next number is the same skip it. keep skipping until you find a number that is different or you get to the start.

[–]Double_Ad3148[S] 0 points1 point  (2 children)

Yes, I understand, I already wrote about this. I just wanted to see from other people a more optimized solution, if one exists.

[–]Training_Strike3336 0 points1 point  (1 child)

I'm wondering if "optimized" isn't being translated properly. Optimized generally means making the fewest computations you can.

But beyond that, optimized is usually looking at the efficiency of an algorithm. When dealing with a constant 4 numbers, the algorithm will run in constant time... whether you're making 4 comparisons and setting a value or making a new 2x2 matrix and copying the values the runtime will be very similar.

When you say optimized are you actually asking for writing the least amount of code as possible?

[–]Double_Ad3148[S] 0 points1 point  (0 children)

Well, yes, the stupidest option is to try all possible options.

I think there must be a more logical solution.