8659 Mine Sweeping
时间限制:500MS 内存限制:65535K
提交次数:37 通过次数:15
题型: 编程题 语言: G++;GCC
Description
The opening ceremony of the 28th Asia Game is coming. In order to prevent the game from being attacked by terrorists, Asia Compete Machinery (ACM) takes lots operations as security measures.
Mine sweeping is one of the most important operations, so a special machine has been invented to detect mines. We assume the fields which need to be detected are configuration as several continuous grids, and there is at most one mine in each grid (See Fig a).
Fig a Fig b
When using the machine to detect the ith grid, the machine will provide the number xi, which means the total number of mines in the ith, (i-1)th and (i+1)th grids. Except the first and last grid, the detect result of the first grid is the number of 1st and 2nd grid, and the result of the last grid is the last and the last but two grids. The approximate detect result for Fig a is as Fig b. However, some of these machines may have bugs and provide unreal result. For example, one of the unreal detect result for Fig a is as Fig c. The red grid is unreal result.
Fig c
It is clearly that Fig b is possible result, but Fig c is not. Now, give you the detect result for a given field, please calculate if this result is possible.
输入格式
The first line of input is an integer T (T <= 100), indicate the number of test cases.
Each test case contains two lines.
The first line of each test case is an integer N (3 <= N <= 1000), indicate the length of the given field. The second line contains N integers Xi (0 <= Xi <= 3), each Xi indicate the detect result for grid i.
输出格式
If the detect result is possible, please output “YES”, otherwise, output “NO”.
输入样例
3 13 1 1 1 1 1 1 1 1 1 0 1 2 2 13 1 1 1 1 1 2 1 1 1 0 1 2 2 7 1 2 3 3 3 2 1
输出样例
YES NO YES
思路:模拟, 枚举第一个数管辖范围可能的情况, 那么考虑第2 到 第 n - 1 个数时,只需确定该数后一个数需要填什么,特别的n同样是只管辖 n 和 n - 1两个范围,需要特殊考虑。

1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 #include <cmath> 5 #include <queue> 6 using namespace std; 7 int vis[1010], fig[1010];//vis[] = 1表示i处为炸弹,vis[] = -1表示必定没有炸弹 8 int suc, n; 9 int calc(int m) 10 { 11 int cnt = 0; 12 if(vis[m - 1] == 1) cnt++; 13 if(vis[m] == 1) cnt++; 14 return cnt; 15 } 16 bool check() 17 { 18 for(int i = 2; i < n; ++i) 19 { 20 int nd = fig[i] - calc(i); 21 if(nd == 0) vis[i + 1] = -1; 22 else if(nd == 1) vis[i + 1] = 1; 23 else return false; 24 } 25 int nd = calc(n); 26 if(nd == fig[n]) return true; 27 else return false; 28 } 29 int main() 30 { 31 // freopen("in.txt","r",stdin); 32 int _; 33 scanf("%d",&_); 34 while(_--) 35 { 36 memset(vis, 0, sizeof vis); 37 scanf("%d",&n); 38 for(int i = 1; i <= n; ++i) scanf("%d",&fig[i]); 39 suc = 0; 40 if(fig[1] == 1) { 41 vis[1] = 1, vis[2] = -1; 42 if(check()) suc = 1; 43 if(!suc) { 44 vis[1] = -1, vis[2] = 1; 45 if(check()) suc = 1; 46 } 47 } 48 else if(fig[1] == 2) { 49 vis[1] = vis[2] = 1; 50 if(check()) suc = 1; 51 } 52 else if(fig[1] == 0) { 53 vis[1] = vis[2] = -1; 54 if(check()) suc = 1; 55 } 56 else suc = 0; 57 if(suc) printf("YES\n"); 58 else printf("NO\n"); 59 } 60 }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧