NC25727. Matrix
描述
输入描述
The first line contains an integer number T, the number of test cases.For each test case :The first line contains an integer n(), the number of matrix sequence A.The second line contains n integers (), the serial number of matrices in A.
The third line contains an integer m(), the serial number of target matrix.
输出描述
For each test case print“YES”(without quotes) if it is possible, and“NO”(without quotes) otherwise.
示例1
输入:
2 4 0 2 7 3 5 4 2 3 7 0 5
输出:
YES NO
C++11(clang++ 3.9) 解法, 执行用时: 179ms, 内存消耗: 868K, 提交时间: 2019-06-29 13:19:09
#include <iostream> using namespace std; const int maxn = 100005; int tab[9][9] = { {0,0,0,0,0,0,0,0,0} ,{0,1,2,3,4,5,6,7,8} ,{0,2,5,4,6,7,8,1,3} ,{0,3,8,5,2,6,1,4,7} ,{0,4,3,7,5,8,2,6,1} ,{0,5,7,6,8,1,3,2,4} ,{0,6,4,1,7,3,5,8,2} ,{0,7,1,8,3,2,4,5,6} ,{0,8,6,2,1,4,7,3,5} }; int rev[] = {0,1,7,6,8,5,3,2,4}; int a[maxn]; int calc(int l, int r){ int ret = 1; for(int i = l; i <= r; i++){ ret = tab[ret][a[i]]; } return ret; } int main(){ ios::sync_with_stdio(0); int t; cin >> t; while(t--){ int n, m; cin >> n; int cnt = 0, pos = -1; for(int i = 0; i < n; i++){ cin >> a[i]; if(a[i] == 0){ cnt++; pos = i; } } cin >> m; bool ok = false; if(cnt == 0) ok = (calc(0, n-1) == m); else if(cnt == 1) ok = (tab[tab[rev[calc(0, pos - 1)]][m]][rev[calc(pos + 1, n - 1)]] <= 4); else ok = true; cout << (ok ? "YES\n" : "NO\n"); } }