标签:
时间限制:500MS 内存限制:65535K
提交次数:37 通过次数:15
题型: 编程题 语言: G++;GCC
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 }
标签:
原文地址:http://www.cnblogs.com/orchidzjl/p/4565735.html