Time limit: 500MS memory limit: 65535K
Number of submissions: 37 Hits: 15
Question types: programming language: 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 Secu Rity measures.
Mine sweeping is one of the most important operations, so a special machine have been invented to detect mines. We assume the fields which need to being detected are configuration as several continuous grids, and there was at most one min E in each grid (see Fig a).
Fig a Fig B
When using the machine to detect the ith grid, the machine would provide the number XI, which means the total numb Er 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 T He last grid was the last, and the last, but both grids. The approximate detect result for fig A is as fig B. However, some of these machines may has 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 the fig B is possible result, but Fig C was not. Now, give your detect result for a given field, please calculate if this result is possible.
Input format
The first line of input was an integer t (t <=), indicate the number of test cases.
Each test case contains the lines.
The first line of all test case was an integer n (3 <= n <=), indicate the length of the Give n field. The second line contains N integers XI (0 <= XI <= 3), each Xi indicate the de Tect result for Grid I.
Output format
If The detect result is possible, please output "YES", otherwise, output "NO".
Input sample
3131 1 1 1 1 1 1 1 1 0 1 2 2131 1 1 1 1 2 1 1 1 0 1 2 271 2 3 3 3 2 1
Output sample
Yesnoyes
Idea: Simulation, enumeration of the first number of jurisdictions possible, then consider the 2nd to the number of n-1, only need to determine what the number of a number needs to fill in, the special n is also only jurisdiction N and n-1 two ranges, need special consideration.
1#include <cstdio>2#include <cstring>3#include <algorithm>4#include <cmath>5#include <queue>6 using namespacestd;7 intvis[1010], fig[1010];//vis[] = 1 means I is a bomb, vis[] = 1 means there must be no bombs8 intsuc, N;9 intCalcintm)Ten { One intCNT =0; A if(Vis[m-1] ==1) cnt++; - if(Vis[m] = =1) cnt++; - returnCNT; the } - BOOLCheck () - { - for(inti =2; I < n; ++i) + { - intnd = fig[i]-Calc (i); + if(nd = =0) Vis[i +1] = -1; A Else if(nd = =1) Vis[i +1] =1; at Else return false; - } - intnd =calc (n); - if(nd = = Fig[n])return true; - Else return false; - } in intMain () - { to //freopen ("In.txt", "R", stdin); + int _; -scanf"%d",&_); the while(_--) * { $memset (Vis,0,sizeofvis);Panax Notoginsengscanf"%d",&n); - for(inti =1; I <= N; ++i) scanf ("%d",&fig[i]); theSUC =0; + if(fig[1] ==1) { Avis[1] =1, vis[2] = -1; the if(check ()) suc =1; + if(!suc) { -vis[1] = -1, vis[2] =1; $ if(check ()) suc =1; $ } - } - Else if(fig[1] ==2) { thevis[1] = vis[2] =1; - if(check ()) suc =1;Wuyi } the Else if(fig[1] ==0) { -vis[1] = vis[2] = -1; Wu if(check ()) suc =1; - } About ElseSUC =0; $ if(suc) printf ("yes\n"); - Elseprintf"no\n"); - } -}
View Code
8659 Mine Sweeping