ACM篇:POJ 1166--The Clocks

来源:互联网 发布:外汇手机交易软件 编辑:程序博客网 时间:2024/06/05 16:56

暴力。

#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;}
0 0
原创粉丝点击