forked from gutty333/Hard-Programming-Challenges
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path40_MatrixBorder.cpp
More file actions
172 lines (149 loc) · 4.12 KB
/
40_MatrixBorder.cpp
File metadata and controls
172 lines (149 loc) · 4.12 KB
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
// For this challenge you will be transposing rows and columns within an array.
/*
have the function MatrixBorder(strArr) read the strArr parameter being passed which will represent an NxN matrix filled with 1's and 0's. Your program should determine the number of swaps between two rows or two columns that must be made to change the matrix such that the border of the matrix contains all 1's and the inside contains 0's. The format of strArr will be: ["(n,n,n...)","(...)",...] where n represents either a 1 or 0. The smallest matrix will be a 3x3 and the largest will be a 6x6 matrix.
For example: if strArr is: ["(0,1,1)","(1,1,1)","(1,1,1)"] then you can swap the first two columns and then swap the first two rows to create a matrix with the 1's on the border and the 0 on the inside, therefore your program should output 2.
*/
#include <iostream>
#include <string>
#include <vector>
using namespace std;
void swapColumn(vector <vector<char> >&, int);
void swapRow(vector <vector<char> >&, int);
/*
create a matrix based on the input string
simple solution would be to analyze the border rows and columns
as we traverse we check for any 0
if 0 is at a column border swap with a column that has all 1s
if 0 is at a row border swap with a row that all 1s
keep track of the number of swaps made
*/
int MatrixBorder(string strArr[], int size)
{
vector < vector <char> > matrix;
int swapCount = 0;
// traverse input to create the matrix
for (int x = 0; x < size; x++)
{
vector <char> temp;
for (int y = 0; y < strArr[x].length(); y++)
{
if (strArr[x][y] == '1' || strArr[x][y] == '0')
{
temp.push_back(strArr[x][y]);
}
}
// fill in the row
matrix.push_back(temp);
}
// analyzing the matrix to assure no border has a 0
for (int row = 0; row < matrix.size(); row++)
{
for (int col = 0; col < matrix[row].size(); col++)
{
// condition to check the borders only
if (row == 0 || row == matrix.size() - 1 || col == 0 || col == matrix[row].size()-1)
{
if (matrix[row][col] == '0')
{
// column needs to be swapped
if (col == 0 || col == matrix[row].size() - 1)
{
swapColumn(matrix, col);
swapCount++;
}
// row needs to be swapped
else if (row == 0 || row == matrix.size()-1)
{
swapRow(matrix, row);
swapCount++;
}
}
}
}
}
return swapCount;
}
// method to swap 2 columns
void swapColumn(vector <vector<char> >& matrix, int col)
{
// traverse to find a column that we can swap with
for (int y = 0; y < matrix[0].size(); y++)
{
// ignore same column or border columns
if (y == col || y == 0 || y == matrix[0].size()-1)
{
continue;
}
else
{
bool valid = true;
// analyze current column to check if is valid for swapping
for (int row = 0; row < matrix.size(); row++)
{
if (matrix[row][y] == '0')
{
valid = false;
break;
}
}
// preform the swap between the 2 columns
if (valid)
{
char temp;
for (int x = 0; x < matrix.size(); x++)
{
temp = matrix[x][col];
matrix[x][col] = matrix[x][y];
matrix[x][y] = temp;
}
return;
}
}
}
}
// method to swap 2 rows
void swapRow(vector <vector<char> >& matrix, int row)
{
// traverse to find a row that we can swap with
for (int x = 0; x < matrix.size(); x++)
{
// ignore the same row, or any border rows
if (x == row || x== 0 || x == matrix.size()-1)
{
continue;
}
else
{
bool valid = true;
// analyze current row to check if is valid for swapping
for (int col = 0; col < matrix[x].size(); col++)
{
if (matrix[x][col] == '0')
{
valid = false;
break;
}
}
// preform the swap between the 2 rows
if (valid)
{
char temp;
for (int y = 0; y < matrix.size(); y++)
{
temp = matrix[row][y];
matrix[row][y] = matrix[x][y];
matrix[x][y] = temp;
}
return;
}
}
}
}
int main()
{
string A[] = { "(0,1,1)", "(1,1,1)", "(1,1,1)" };
string B[] = { "(0,1,0,1)", "(1,1,1,1)", "(0,1,0,1)", "(1,1,1,1)" };
cout << MatrixBorder(A, sizeof(A) / sizeof(A[0])) << endl; // 2
cout << MatrixBorder(B, sizeof(B) / sizeof(B[0])) << endl; // 2
return 0;
}