Sicily 1693. Cube

1693. Cube

Constraints

Time Limit: 20 secs, Memory Limit: 32 MB

Description

Klins has a dramatic toy made of 27 blocks. The toy is illustrated in picture 1 below:

The toy is made of 17 groups. Each group is composed of 3 or 2 blocks. Each junction between two consecutive groups can rotate 360 degrees. So if Klins is smart enough, he can use the toy to form a 3*3*3 cube (See picture 3).

In picture 1 above, the first group has 3 blocks, the second group has 2 blocks, and the third has 3 ones, and so on. As the blocks in each group are connected by a bungee, they can never move out of the group whatever possible rotation is performed (See picture 2). 

But not all kinds of such toys that are made up of 27 blocks are possible to form a cube. Klins wants to know whether the given toy can form a cube or not. 

Input

The input contains multiple cases. The first line of input is the number of test cases (<=1500). For each case, there are 17 integers in a line, each of which is either 3 or 2, denoting the number of blocks in each group. These numbers are given in consecutive order, i.e. any two consecutive groups are assumed to be connected with each other.

Output

For each test case, print one line of “yes” if a cube of 3*3*3 can be formed using the given toy, otherwise print “no”.

Sample Input

23 2 3 2 3 2 3 2 3 2 3 2 3 2 3 2 33 3 3 3 2 2 2 3 3 2 2 3 2 3 2 2 3

Sample Output

yesyes

// Problem#: 1693
// Submission#: 3420596
// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License
// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/
// All Copyright reserved by Informatic Lab of Sun Yat-sen University
#include <stdio.h>
#include <iostream>
#include <vector>
#include <string>
#include <stack>
#include <iomanip>
#include <algorithm>
#include <queue>
#include <functional>
#include <map>
#include <string.h>
#include <math.h>
#include <list>
using namespace std;

const int MAX_N = 55;

const int d[3][3] = {0, 0, 1, 0, 1, 0, 1, 0, 0};
int n[17];
bool cube[3][3][3];

bool isOK(int x, int y, int z) {
    return 0 <= x && x < 3 && 0 <= y && y < 3 && 0 <= z && z < 3 && !cube[x][y][z];
}

bool search(int x, int y, int z, int dir, int flag, int step) {
    if (step >= 17) return true;
    int tx = x, ty = y, tz = z, i, k;
    for (k = 1; k < n[step]; k++) {
        tx += flag * d[dir][0];
        ty += flag * d[dir][1];
        tz += flag * d[dir][2];
        if (!isOK(tx, ty, tz)) break;
    }
    if (k < n[step]) return false;
    else {
        tx = x;
        ty = y;
        tz = z;
        for (k = 1; k < n[step]; k++) {
            tx += flag * d[dir][0];
            ty += flag * d[dir][1];
            tz += flag * d[dir][2];
            cube[tx][ty][tz] = true;
        }
        if (search(tx, ty, tz, (dir + 1) % 3, 1, step + 1) || search(tx, ty, tz, (dir + 1) % 3, -1, step + 1) || search(tx, ty, tz, (dir + 2) % 3, 1, step + 1) || search(tx, ty, tz, (dir + 2) % 3, -1, step + 1)) return true;
        tx = x;
        ty = y;
        tz = z;
        for (k = 1; k < n[step]; k++) {
            tx += flag * d[dir][0];
            ty += flag * d[dir][1];
            tz += flag * d[dir][2];
            cube[tx][ty][tz] = 0;
        }
        return false;
    }
}

bool check(int x, int y, int z, int dir, int flag) {
    memset(cube, false, sizeof(cube));
    cube[x][y][z] = true;
    return search(x, y, z, dir, flag, 0);
}

int main() {

    std::ios::sync_with_stdio(false);

    int caseNum;
    cin >> caseNum;

    while (caseNum--) {
        int sum = 0;
        for (int i = 0; i < 17; i++) {
            cin >> n[i];
            sum += n[i];
        }
        if (sum == 43 && (check(0, 0, 0, 0, 1) || check(1, 0, 1, 0, 1) || check(1, 0, 1, 1, 1))) cout << "yes" << endl;
        else cout << "no" << endl;
    }

    return 0;
}                                 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值