top of page
Search

Day 2 | Beautiful Matrix | C++

  • Writer: Ayush Mahajan
    Ayush Mahajan
  • Aug 28, 2022
  • 1 min read

We will be solving the second question of our competitive journey today. Let's solve this question today - Beautiful Matrix: https://codeforces.com/problemset/problem/263/A… Give it a try yourself first


So here we just need to observe one simple thing- Swapping a row containing 1 with another row only produces an effect similar to moving 1 up or down. Similarly, for columns, it is like moving 1 left or right.


So one operation is like shifting 1 up, down, left, or right. So total moves required to move to the center - 3, 3 is the manhattan distance between 1 and the center. I calculated for 0-based indexing You can check out the code here:


Again remember it is cool if you make any mistake in writing code, it happens. A silly mistake I made while coding: https://codeforces.com/contest/263/submission/169091687


Btw did you know about Manhattan distances already? If not, you can read about it here:

https://logicplum.com/knowledge-base/manhattan-distance/

 
 
 

Comments


bottom of page