ACM篇:POJ 1166--The Clocks

暴力。

#include <iostream>
#include <cstdio>
#include <vector>
#include <cstring>

using namespace std;
const int CLOCKS = 9;
const int move[CLOCKS][CLOCKS] = 
{
    {1, 1, 0, 1, 1, 0, 0, 0, 0},
    {1, 1, 1, 0, 0, 0, 0, 0, 0},
    {0, 1, 1, 0, 1, 1, 0, 0, 0},
    {1, 0, 0, 1, 0, 0, 1, 0, 0},
    {0, 1, 0, 1, 1, 1, 0, 1, 0},
    {0, 0, 1, 0, 0, 1, 0, 0, 1},
    {0, 0, 0, 1, 1, 0, 1, 1, 0},
    {0, 0, 0, 0, 0, 0, 1, 1, 1},
    {0, 0, 0, 0, 1, 1, 0, 1, 1},
};
int clock[CLOCKS];
int op[CLOCKS];
vector<int> ans;

void read_clock()
{
    for (int i = 0; i < CLOCKS; i++)
        scanf("%d", &clock[i]);
}

int is_ok()
{
    static int temp[CLOCKS];
    for (int i = 0; i < CLOCKS; i++)
        temp[i] = clock[i];

    for (int i = 0; i < CLOCKS; i++)
    {
        for (int j = 0; j < CLOCKS; j++)
            temp[j] += op[i] * move[i][j];
    }
    for (int i = 0; i < CLOCKS; i++)
        if (temp[i] % 4)
            return 0;

    int cnt = 0;
    for (int i = 0; i < CLOCKS; i++)
        cnt += op[i];
    return cnt;
}
void _dfs(int x)
{
    int sz;
    if (sz = is_ok())
    {
        if (sz < ans.size() || !ans.size())
        {
            ans.clear();
            for (int i = 0; i < CLOCKS; i++)
            {
                for (int j = 0; j < op[i]; j++)
                    ans.push_back(i+1);
            }
        }
        return;
    }
    if (x >= 8)
        return;

    for (int i = 0; i < 4; i++)
    {
        op[x+1] = i;
        _dfs(x+1);
    }
}

void _print()
{
    for (int i = 0; i < ans.size(); i++)
        printf("%d ", ans[i]);
}
int main()
{
    read_clock();
    _dfs(-1);
    _print();
    return 0;
}
發佈了58 篇原創文章 · 獲贊 0 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章