描述
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).
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.
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
简单的模拟扫雷,问你是否存在合理的炸弹摆放位置输出YES或者NO
首先先举例摆放第一个位置是否放置炸弹,后一个位置放置炸弹跟前一个位置是有关系的,一直推倒最后一个位置,我们还得判断最后一个位置是否符合条件。
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define N 1001
int num[N],x[N],flag;
int main(){
int t,n;
scanf("%d",&t);
while(t--){
scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%d",&num[i]);
for(int i=0;i<=1;i++){
flag=1;
x[1]=i;
for(int j=2;j<=n;j++){
x[j]=num[j-1]-x[j-1]-x[j-2];
if(x[j]>1||x[j]<0){
flag=0;break;
}
}
if(flag){//如果推倒最后一个也是成立的才能跳出,否则就继续循环
if(num[n]==x[n]+x[n-1]) break;
}
}
if(num[n]!=x[n]+x[n-1])flag=0;
if(flag)printf("YES\n");
else printf("NO\n");
}
}