Err, need help understanding this

You've got a 5 × 5 matrix, consisting of 24 zeroes and a single number one. Let's index the matrix rows by numbers from 1 to 5 from top to bottom, let's index the matrix columns by numbers from 1 to 5 from left to right. In one move, you are allowed to apply one of the two following transformations to the matrix:

Swap two neighboring matrix rows, that is, rows with indexes i and i + 1 for some integer i (1 ≤ i < 5).
Swap two neighboring matrix columns, that is, columns with indexes j and j + 1 for some integer j (1 ≤ j < 5).
You think that a matrix looks beautiful, if the single number one of the matrix is located in its middle (in the cell that is on the intersection of the third row and the third column). Count the minimum number of moves needed to make the matrix beautiful.

Input
The input consists of five lines, each line contains five integers: the j-th integer in the i-th line of the input represents the element of the matrix that is located on the intersection of the i-th row and the j-th column. It is guaranteed that the matrix consists of 24 zeroes and a single number one.

Output
Print a single integer — the minimum number of moves needed to make the matrix beautiful.

Examples
input
0 0 0 0 0
0 0 0 0 1
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
output
3
input
0 0 0 0 0
0 0 0 0 0
0 1 0 0 0
0 0 0 0 0
0 0 0 0 0
output
1



That's my code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#include <bits/stdc++.h>


#define fl(n) for(int i = 0; i < n; i++)


#define ll long long
#define nl endl
#define init -9999
#define INF 1e9
#define u unsigned


bool IsPerfectMatrix(int);


using namespace std;
/*
Perfect Matrix:

0 0 0 0 0
0 0 0 0 0
0 0 1 0 0
0 0 0 0 0
0 0 0 0 0
*/
struct position
{
    int rows;
    int columns;
};

int coordx = 0;
int coordy = 0;

bool IsPerfectMatrix(int arr[5][5], int zarr[5][5])
{
    return ( arr[2][2] == zarr[2][2] );
}

void WITO(int arr[5][5])//Where Is The One
{
    position pos;

    for(int r = 0; r < 5; r++)
    {
        for(int c = 0; c < 5; c++)
        {
            if(arr[r][c] == 1)
            {
                pos.rows = r;
                pos.columns = c;
                break;
            }
        }
    }
    coordx = pos.rows;
    coordy = pos.columns;
}


//ideal is row 2 col 2
int main()
{
    int Pmatrix[5][5] = {
                   {0,0,0,0,0},
                   {0,0,0,0,0},
                   {0,0,1,0,0},
                   {0,0,0,0,0},
                   {0,0,0,0,0}
                   };

    int matrix[5][5];


    for(int row = 0; row < 5; row++)
        for(int cols = 0; cols < 5; cols++)
        cin >> matrix[row][cols];


       
        if(IsPerfectMatrix(matrix,Pmatrix) == 1) {cout << 0; return 0;}

        WITO(matrix);
        
        
        int cnt = 0;

        while(true)
        {



        
        if(coordx != 4 )
        {
        ++cnt;
        matrix[coordx][coordy] = 0;
        coordy++;
        matrix[coordx][coordy] = 1;

        if(IsPerfectMatrix(matrix,Pmatrix) == 1) break;
        }
        else if(coordx == 4)
        {
            cnt = 1;
        
        coordx = 1;

        }
        }
        cout << cnt;

    return 0;
}



And it fails to work under many conditions, it only works if the 1 is at the outer most column but otherwise it doesn't work meanwhile this code, one of the solutions I found works well but I don't understand the concept behind it, can someone explain either why mine doesnt work or the concept behind the other code? or both :P
here is the code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <cstdlib>
using namespace std;

int gr[5][5];

int main()
{
	int r, c;
	for (int i = 0; i < 5; i++)
	{
		for (int j = 0; j < 5; j++)
		{
			cin >> gr[i][j];
			if (gr[i][j] == 1)
			{
				r = i;
				c = j;
			}
		}
	}
	cout << abs(2 - r) + abs(2 - c) << endl;
	return 0;
}



Thanks in advance :3
Last edited on
Using std::swap it shouldn't take more than 2 moves to make the matrix 'beautiful':
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>
#include <vector>
#include <utility>
#include <chrono>
#include <random>

constexpr auto LOWER = 0;
constexpr auto SIZE = 5;

int main()
{
    std::vector<std::vector<int>> vec(SIZE, std::vector<int>(SIZE, 0));

    auto seed = std::chrono::system_clock::now().time_since_epoch().count();//seed
    std::default_random_engine dre(seed);//engine
    std::uniform_int_distribution<size_t> di(LOWER,SIZE-1);//distribution based on lower and upper bound
    size_t row  = di(dre);
    size_t col = di(dre);
    vec[row][col] = 1;
    std::cout << "Matrix with random 1: \n\n";
    for (const auto& elem : vec)
    {
        for (const auto& elemI : elem)
        {
            std::cout << elemI << " ";
        }
        std::cout << "\n";
    }
    std::cout << "\n";
    size_t numSwaps{};
    if (row == 2 && col == 2)
    {
        numSwaps = 0;
    }
    else if(row == 2 && col != 2)//std::swap the cols
    {
        numSwaps = 1;
    }
    else if (row != 2 && col == 2)
    {
        numSwaps = 1;//std::swap the rows
    }
    else
    {
        numSwaps = 2; //std::swap rows first and then cols or vice-versa
    }
    std::cout << "Number of swaps required for beautiful matrix: " << numSwaps << "\n";
}
closed account (48T7M4Gy)
The answer is simply the total of the column and row differences between the center and the starting position of the '1'. Just swap the two elements, or even easier overwrite a '1' at the center and a '0' at the original '1' position.
Topic archived. No new replies allowed.