ZeroJudge C833: 2D-Array

There is a rectangular city that can be viewed as a two-dimensional grid plane. It has a total of N rows from top to bottom and M columns from left to right, divided into N × M squares. Due to poor security, there is a chance of encountering a wild baton in each square, which can be picked up for fighting. You want to collect some batons, but you know that some of the squares in the city have police stations. If the police inside a police station or in any square in the same row or column as it detects someone picking up a baton, they will mercilessly detain the person preventively. Therefore, for safety reasons, you will definitely not go to those squares to pick up batons. You want to know, under the above conditions, where you can still go to pick up batons.

Sample Inputs/Outputs

Sample Input(s)Sample Output(s)
The first line of input consists of two positive integers N and M, indicating that the city has N rows from top to bottom and M columns from left to right, divided into N × M squares.
Following are N lines, each line containing a string of length exactly M. Each character in the string can be either '•' (ASCII code 46) or '#' (ASCII code 35). If the j-th character in the i-th line is '#', it indicates that there is a police station in the square of the i-th row and the j-th column of the city. Otherwise, it indicates that there is no police station. There won't be any other characters in each line.

Please print N lines, each line consisting of a string of exact length M. For the j-th character of the i-th line, if the square in the i-th row and j-th column of the city is accessible for picking up a baton, then output "X" (capital letter), otherwise output "#". Do not output any other characters or spaces.
2 2
#.
..
##
#X
3 2
##
..
#.
##
##
##
1 4
..#.
####

Thought Process

Using a two-dimensional array to collect characters, you can check whether the currently received character is "#" while gathering data. If it is "#", record the current i and j positions.

You can declare two Map structures, one for storing rows and the other for storing columns. To store, you increment Row[i] and Column[j] if the character is "#".

After collecting the data, you can then evaluate all the characters. If the current character is "#", output "#". If the current character is ".", then check if both Row[i] and Column[j] are 0. If they are both 0, output "X"; otherwise, output "#".

Sample Code-ZeroJudge C833: 2D-Array

#include <iostream>
#include <vector>
#include <map>
using namespace std;

int main() {
    cin.sync_with_stdio(0);
    cin.tie(0);
    int N, M;
    cin >> N >> M;
    vector<vector<char>>num;
    map<int, int>row, column;
    for (int i = 0; i<N; i++)
    {
        vector<char>v;
        for (int j = 0; j<M; j++)
        {
            char ch;
            cin >> ch;
            if (ch == '#')
            {
                row[i]++;
                column[j]++;
            }
            v.push_back(ch);
        }
        num.push_back(v);
    }
    for (int i = 0; i<N; i++)
    {
        for (int j = 0; j<M; j++)
        {
            if (num[i][j] == '#') cout << '#';
            else
            {
                if (row[i] == 0 && column[j] == 0) cout << 'X';
                else cout << '#';
            }
        }
        cout << "\n";
    }
}

//ZeroJudge C833
//Dr. SeanXD

Comments