fn change_row_column(matrix: &mut Vec<Vec<i32>>, row: usize, col: usize) {
let rows = matrix.len();
let cols = matrix[0].len();
for j in 0..cols {
if matrix[row][j] != 0 {
matrix[row][j] = -1;
}
}
for i in 0..rows {
if matrix[i][col] != 0 {
matrix[i][col] = -1;
}
}
}
fn change_binary_matrix(matrix: &mut Vec<Vec<i32>>) {
let rows = matrix.len();
if rows == 0 {
return;
}
let cols = matrix[0].len();
if cols == 0 {
return;
}
// First pass to mark affected rows and columns
let mut positions = Vec::new();
for i in 0..rows {
for j in 0..cols {
if matrix[i][j] == 0 {
positions.push((i, j));
}
}
}
// Mark rows and columns
for (i, j) in positions {
change_row_column(matrix, i, j);
}
// Second pass to replace -1 with 0
for i in 0..rows {
for j in 0..cols {
if matrix[i][j] == -1 {
matrix[i][j] = 0;
}
}
}
}
fn print_matrix(matrix: &Vec<Vec<i32>>) {
for row in matrix {
for val in row {
print!("{} ", val);
}
println!();
}
}
fn main() {
let mut matrix = vec![
vec![1, 1, 0, 1, 1, 1],
vec![1, 1, 1, 1, 1, 1],
vec![1, 1, 0, 1, 1, 1],
vec![1, 1, 1, 1, 1, 1],
vec![1, 0, 1, 1, 1, 1],
];
change_binary_matrix(&mut matrix);
print_matrix(&matrix);
}
/*
run:
0 0 0 0 0 0
1 0 0 1 1 1
0 0 0 0 0 0
1 0 0 1 1 1
0 0 0 0 0 0
*/