Day 2 | Beautiful Matrix | C++
- 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/
![Day 44 | Binary Search Practice [Leetcode] | C++](https://static.wixstatic.com/media/fbea2a_167f1b66234f4d30b91bcf5b517f83f3~mv2.jpg/v1/fill/w_980,h_490,al_c,q_85,usm_0.66_1.00_0.01,enc_avif,quality_auto/fbea2a_167f1b66234f4d30b91bcf5b517f83f3~mv2.jpg)
![Day 43 | Graph and Tree Practice [Leetcode] II | C++](https://static.wixstatic.com/media/fbea2a_361d6f7cd90c431b9f25518e1503da94~mv2.jpg/v1/fill/w_980,h_490,al_c,q_85,usm_0.66_1.00_0.01,enc_avif,quality_auto/fbea2a_361d6f7cd90c431b9f25518e1503da94~mv2.jpg)
![Day 42 | Graph Practice [LeetCode] | C++](https://static.wixstatic.com/media/fbea2a_6570bb28577149f19db06704d542789a~mv2.jpg/v1/fill/w_980,h_490,al_c,q_85,usm_0.66_1.00_0.01,enc_avif,quality_auto/fbea2a_6570bb28577149f19db06704d542789a~mv2.jpg)
Comments