Gym - 102367C Pawn‘s Revenge

Pawn’s Revenge

评测地址:https://vjudge.net/problem/Gym-102367C

You are playing a special chess game against a professor and you’ve almost lost: the only piece you have left is a king. The professor just left the room to take a call, so you decided to cheat a little bit and put some extra pawns on the board so that each of opponent’s pieces is under attack. As a reminder, a pawn attacks the two diagonally adjacent squares to the upper-left and upper-right of itself and a king attacks the eight adjacent squares (including the diagonally adjacent ones). You cannot put one piece on top of another piece. What is the smallest number of pawns you need to accomplish this task?

Input
The first row contains $ N N $, the dimension of the board, with $ 8 N 1000 8 \leq N \leq 1000 $. The game is played on an $ N N $ by $ N N $ chessboard. The next $ N N $ lines have $ N N $ symbols each describing the board. The symbol - means that the square is empty, * denotes a professor’s piece and K denotes your king. Your pawns move upward (i.e. towards rows that appear earlier in the input).

Output
Output a line containing one number, the smallest number of pawns needed to attack all of the professor’s chess pieces, or $ 1 -1 $ if it is impossible to do so.

Example
Input
8
------



-----*K-

–*-----

Output
2

比赛的时候连这个题都没有做出来,太久不写代码真的是不行啊

// 读完题觉得这题枚举答案+检验 稳了
// 简直是自作聪明  贪心就完全足够  从左向右考虑 右下角的肯定比左下角的更优
// 更在右下角放置守卫 肯定现在右下角放置守卫
#include <iostream>
#include <cstring>
#include <cstdio>
#define  Maxn 1005
using namespace std;
char s[Maxn][Maxn];
int vis[Maxn][Maxn];
int main(int argc,char * argv[]) {
    int n,sum = 0; scanf("%d",&n);
    for(int i=1; i<=n; i++) scanf("%s",s[i] + 1);
    for(int i=1; i<=n; i++)
        for(int j=1; j<=n; j++) {
            if(s[i][j] == 'K') {
                vis[i - 1][j - 1] = 1; vis[i - 1][j ] = 1;
                vis[i ][j - 1] = 1; vis[i + 1][j ] = 1;
                vis[i ][j + 1] = 1; vis[i + 1][j + 1] = 1;
                vis[i + 1][j - 1] = 1; vis[i - 1][j + 1] = 1;
                break;
            }
        }

    for(int j=1; j<=n; j++) {
        if(s[n][j] == '*' && vis[n][j] == 0) {
            printf("-1");
            return 0;
        }
    }
    for(int i=1; i<=n; i++)
        for(int j=1; j<=n; j++) {
            if(s[i][j] == '*' && vis[i][j] == 0 && s[i + 1][ j - 1] != 'w') {
                if(s[i + 1][j + 1] == '-') {
                    sum ++;
                    s[i + 1][j + 1] = 'w';
                    vis[i][j + 2] = 1;
                }
                else if(s[i + 1][j - 1] == '-') {
                    sum ++;
                    s[i + 1][j - 1] = 'w';
                }
                else { printf("-1"); return 0; }
            }
        }
    printf("%d\n",sum);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_35776409/article/details/107475669
今日推荐