https://www.jisuanke.com/contest/11360/challenges
其他题慢慢补
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 NNN, the dimension of the board, with 8≤N≤10008 ≤ N ≤ 10008≤N≤1000. The game is played on an NNN by NNN chessboard. The next NNN lines have NNN 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 if it is impossible to do so.
样例输入复制
8
-*-*----
--------
--------
--------
-----*K-
--------
--*-----
--------
样例输出复制
2
首先清掉K周围的*,然后遍历棋盘,对于每个*可以选左下或者右下,如果都能选的话尽可能选右下,然后把右下位置能清掉的*打标记,如果都不能放的话说明不行。
代码又写挫了...
#include <bits/stdc++.h> using namespace std; char mmap[1005][1005] = {'.'}; bool ok[1005][1005] = {0}; int n; int main() { cin >> n; int ans = 0; bool flag = 1; for(int i = 1; i <= n; i++) scanf("%s", mmap[i] + 1); for(int i = 1; i <= n; i++) { for(int j = 1; j <= n; j++) { if(mmap[i][j] == 'K') { ok[i - 1][j - 1] = ok[i - 1][j] = ok[i - 1][j + 1] = ok[i][j - 1] = ok[i][j + 1] = ok[i + 1][j - 1] = ok[i + 1][j] = ok[i + 1][j + 1] = 1; } } } //for(int i = 1; i <= n; i++) printf("%s ",mmap[i] + 1); for(int i = 1; i <= n; i++) { for(int j = 1; j <= n; j++) { if(mmap[i][j] == '*' && !ok[i][j]) { if(mmap[i + 1][j - 1] == '-' && (mmap[i + 1][j + 1] != '-'))//可处理越界 { ans++; mmap[i + 1][j - 1] = 'S'; ok[i][j] = ok[i][j - 2] = 1; } else if(mmap[i + 1][j + 1] == '-' && (mmap[i + 1][j - 1] != '-')) { ans++; mmap[i + 1][j + 1] = 'S'; ok[i][j] = ok[i][j + 2] = 1; } else if(mmap[i + 1][j - 1] == '-' && mmap[i + 1][j + 1] == '-') { ans++; mmap[i + 1][j + 1] = 'S';//? ok[i][j] = ok[i][j + 2] = 1; } else { flag = 0; break; } } } } if(flag) { cout << ans << endl; } else cout << -1 << endl; return 0; } //5 //--*-- //*--*- //----- //--*K- //-----