Waterloo Local Contest 2019 (22 June, 29 September)
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\n",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- //-----
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!