Question summary-Pawn's Revenge

Question summary-Pawn's Revenge


Title description
topic

At the beginning, I didn't have any ideas for this topic (I found out later that it was not difficult, because I didn't learn it well). Later, I searched for some information on the Internet and I probably understood the idea of ​​this topic. The problem is that when there is already and only one K piece, by adding the minimum number of pawn pieces, all * pieces of the opponent can be attacked. Among them, K can attack the other eight directions, and pawn pieces can only Attack the upper left corner and the upper right corner in two directions.

In fact, this problem is solved directly by the violent enumeration method .

First, construct a mark array for all the chess pieces to determine whether the remaining eight directions of the existing K moves contain star chess, if any, mark that star chess has been visited (the boundary conditions need to be paid attention to here).

Then, start traversing from the chess pieces in the first row and the first column. If the chess piece is a star and has not been visited, first judge whether the chess piece in the lower right corner is empty. If it is empty, you can put the pawn chess piece and place the star chess. Marked as visited, the pawn pawn piece that is put down at the same time, whether it is a star or not, will be marked as visited. If the pawn chess cannot be placed in the lower right corner, then judge whether the pawn chess can be placed in the lower left corner, if possible, mark the star chess as visited (there is no need to mark the upper left corner of the placed pawn chess as visited, because the upper left corner The chess must be traversed before the chess in the upper right corner). In this whole judgment, we must pay attention to the limitations of boundary conditions (generally, this kind of problems with rows, columns, and two-dimensional arrays need to pay attention to the limitations of boundary conditions). This important point must be reflected in the code. Out, otherwise the code will not be correct.

In the above, why first judge whether the star is placed in the lower right corner instead of the lower left corner? Personally, it is because when traversing the judgment, it is judged from the right side...

Finally, traverse the entire board again to see if there are any stars. If there is, it means that it is impossible to attack all the opponent's stars and output -1; if not, output the number of stars that need to be placed.

The idea of ​​succession of the whole topic is probably like this. It is actually not easy to think of (I think so, or because the level is not enough...), and when the code is implemented, there are actually many small details that need to be paid attention to. , The details determine success or failure!

Code implementation (C++)

#include <bits/stdc++.h>
using namespace std;
int vis[1020][1020];
int main()
{
    
    
    int n, c= 0, i, j;
    char a[1020][1020];
    cin >> n;
    for (i = 0; i < n; i++)
    {
    
    
        scanf("%s",a[i]);
    }
    for (i = 0; i < n; i++)
    {
    
    
        for (j = 0; j < n; j++)
        {
    
    
            if (a[i][j] == 'K')
            {
    
    
                for (int k = -1; k <= 1; k++)
                {
    
    
                    for (int m = -1; m <= 1; m++)
                    {
    
    
                        if (i + k >= 0 && i + k < n && j + m >= 0 && j + m < n && a[i + k][j + m] == '*')
                        {
    
    
                            vis[i + k][j + m] = 1;
                        }
                    }
                }
            }
        }
    }
    for (i = 0; i < n; i++)
    {
    
    
        for (j = 0; j < n; j++)
        {
    
    
            if (a[i][j] == '*' && !vis[i][j])
            {
    
    
                if (i + 1 < n && j + 1 < n && a[i + 1][j + 1] == '-')
                {
    
    
                    a[i + 1][j + 1] = 'p';
                    vis[i][j] = 1;
                    c++;
                    if (j + 2 < n)
                    {
    
    
                        vis[i][j + 2] = 1;
                    }
                }
                else if (i + 1 < n && j - 1 >= 0 && a[i + 1][j - 1] == '-')
                {
    
    
                    a[i + 1][j - 1] = 'p';
                    vis[i][j] = 1;
                    c++;
                }
            }
        }
    }
    for (i = 0; i < n; i++)
    {
    
    
        for (j = 0; j < n; j++)
        {
    
    
            if (a[i][j] == '*' && !vis[i][j])
            {
    
    
                cout << "-1" << endl;
                return 0;
            }
        }
    }
    cout << c << endl;
    return 0;
}

This is the first summary of the summer vacation. My study is still a bit slack, and the time schedule is not reasonable enough. In the future, I must do more questions and summarize more, moving in the direction of the algorithm, come on! ! !

Guess you like

Origin blog.csdn.net/m0_46772594/article/details/107476139