ZeroJudge K554: Landmines are Dangerous

There are two kinds of landmines:
Landmine No. 1: Its explosion affects not only its position but also one grid in each of the four cardinal directions (up, down, left, and right).
Landmine No. 2: Its explosion affects not only its position but extends vertically and horizontally to the boundaries of the map, excluding its position.

You are given a map of size N*M, where some locations are buried with landmines. If a location may be affected by 1 landmine, its risk value is 1. If it may be affected by 2 landmines, its risk value is 2. If it will not be affected by any landmines, its risk value is 0.

Sample Inputs/Outputs

Sample Input(s)Sample Output(s)
The first line consists of two integers, N and M, which represent an N by M grid map. (1 <= N, M <= 100).
Next, there are N rows, each row containing M numbers (the number 0 indicates no landmine, the number 1 indicates a landmine of type 1, and the number 2 indicates a landmine of type 2, with numbers separated by a single space).
Please output the risk values of each position in an N x M map, separated by a single space between numbers, referring to the example output.
4 6
0 0 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 0 0 0
0 0 1 0 0 0
0 1 1 1 0 0
0 0 1 0 0 0
4 6
0 0 0 0 0 0
0 0 0 0 0 0
0 0 2 0 0 0
0 0 0 0 0 0
0 0 1 0 0 0
0 0 1 0 0 0
1 1 1 1 1 1
0 0 1 0 0 0
4 6
0 0 0 1 0 0
0 0 2 0 0 0
0 0 0 0 0 1
0 0 2 0 0 0
0 0 3 1 1 0
1 1 2 2 1 2
0 0 2 0 1 1
1 1 2 1 1 2
ZeroJudge K554 Sample Test Cases

Thought Process

Store the map into a two-dimensional array, and declare another two-dimensional array "ans," where all data inside is initialized to 0.

Iterate over each position in the map, and determine if there is a landmine. Perform operations based on the type of landmine.

If it's a type 1 landmine, then increment the current position in ans, as well as its adjacent positions (top, bottom, left, right). Ensure to check boundaries to avoid index out-of-range errors in the N x M grid.

If it's a type 2 landmine, use a for loop to iterate over the top, bottom, left, and right directions, incrementing the entire row of ans by 1 for each direction.

Sample Code-ZeroJudge K554: Landmines are Dangerous

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

int main() {
    cin.sync_with_stdio(0);
    cin.tie(0);
    int N, M, loc[4][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
    cin >> N >> M;
    vector<vector<int>>num, ans;
    for (int i = 0; i<N; i++) {
        vector<int>hi, hii;
        for (int j = 0; j<M; j++) {
            int tmp;
            cin >> tmp;
            hi.push_back(tmp);
            hii.push_back(0);
        }
        ans.push_back(hii);
        num.push_back(hi);
    }
    for (int i = 0; i<N; i++) {
        for (int j = 0; j<M; j++) {
            if (num[i][j] == 1) {
                ans[i][j]++;
                for (int k = 0; k<4; k++) {
                    int yy = i+loc[k][0], xx = j+loc[k][1];
                    if (yy >= 0 && yy < N && xx >= 0 && xx < M) ans[yy][xx]++;
                }
                continue;
            }
            if (num[i][j] == 2) {
                ans[i][j]++;
                for (int k = i-1; k >= 0; k--) ans[k][j]++;
                for (int k = i+1; k<N; k++) ans[k][j]++;
                for (int k = j-1; k >= 0; k--) ans[i][k]++;
                for (int k = j+1; k<M; k++) ans[i][k]++;
            }
        }
    }
    for (int i = 0; i<N; i++) {
        for (int j = 0; j<M; j++) cout << ans[i][j] << ' ';
        cout << "\n";
    }
    cout << "\n";
}

//ZeroJudge K554
//Dr. SeanXD

Comments