USACO 1.4.2 —— 暴搜

The Clocks
IOI'94 - Day 2
Consider nine clocks arranged in a 3x3 array thusly:
|-------|    |-------|    |-------|    
|       |    |       |    |   |   |    
|---O   |    |---O   |    |   O   |          
|       |    |       |    |       |           
|-------|    |-------|    |-------|    
    A            B            C

|-------|    |-------|    |-------|
|       |    |       |    |       |
|   O   |    |   O   |    |   O   |
|   |   |    |   |   |    |   |   |
|-------|    |-------|    |-------|
    D            E            F

|-------|    |-------|    |-------|
|       |    |       |    |       |
|   O   |    |   O---|    |   O   |
|   |   |    |       |    |   |   |
|-------|    |-------|    |-------|
    G            H            I

The goal is to find a minimal sequence of moves to return all the dials to 12 o'clock. Nine different ways to turn the dials on the clocks are supplied via a table below; each way is called a move. Select for each move a number 1 through 9 which will cause the dials of the affected clocks (see next table) to be turned 90 degrees clockwise.

MoveAffected clocks
1ABDE
2ABC
3BCEF
4ADG
5BDEFH
6CFI
7DEGH
8GHI
9EFHI

Example

Each number represents a time according to following table:
9 9 12       9 12 12       9 12 12        12 12 12      12 12 12 
6 6 6  5 ->  9  9  9  8->  9  9  9  4 ->  12  9  9  9-> 12 12 12 
6 3 6        6  6  6       9  9  9        12  9  9      12 12 12 

[But this might or might not be the `correct' answer; see below.]

PROGRAM NAME: clocks

INPUT FORMAT

Lines 1-3:Three lines of three space-separated numbers; each number represents the start time of one clock, 3, 6, 9, or 12. The ordering of the numbers corresponds to the first example above.

SAMPLE INPUT (file clocks.in)

9 9 12
6 6 6
6 3 6

OUTPUT FORMAT

A single line that contains a space separated list of the shortest sequence of moves (designated by numbers) which returns all the clocks to 12:00. If there is more than one solution, print the one which gives the lowest number when the moves are concatenated (e.g., 5 2 4 6 < 9 3 1 1).

SAMPLE OUTPUT (file clocks.out)

4 5 8 9
本题解法有很多,可以暴力,可以BFS,可以DFS,可以推数学公式。我是直接暴力的。
/*
ID: XMzhou
LANG: C++
TASK: clocks
*/
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <map>
#include <string>
#include <stack>
#include <cctype>
#include <vector>
#include <queue>
#include <set>
#include <utility>
#include <cassert>
#include <numeric>
using namespace std;
#define Online_Judge
#define outstars cout << "***********************" << endl;
#define clr(a,b) memset(a,b,sizeof(a))
#define lson l , mid  , rt << 1
#define rson mid + 1 , r , rt << 1 | 1
#define mk make_pair

const int MAXN = 10000 + 50;
const int MAXS = 10000 + 50;
const int sigma_size = 26;
const long long LLMAX = 0x7fffffffffffffffLL;
const long long LLMIN = 0x8000000000000000LL;
const int INF = 0x7fffffff;
const int IMIN = 0x80000000;
const int inf = 1 << 30;
#define eps 1e-10
const long long MOD = 1000000000 + 7;
const int mod = 10007;
typedef long long LL;
const double PI = acos(-1.0);
typedef double D;
typedef pair<int , int> pii;
typedef vector<int> vec;
typedef vector<vec> mat;


#define Bug(s) cout << "s = " << s << endl;
///#pragma comment(linker, "/STACK:102400000,102400000")
#define FOR(x) for(s[x] = 0; s[x] < 4 ; s[x]++)
int a[10] , b[10] , s[10] ,res[10] , cnt ;
int main()
{
    //ios::sync_with_stdio(false);
    #ifdef Online_Judge
        freopen("clocks.in","r",stdin);
        freopen("clocks.out","w",stdout);
    #endif // Online_Judge
    for(int i = 0 ; i < 9 ; i++)
    {
        scanf("%d" , &a[i]);
        a[i] = a[i] / 3 % 4;
    }
    int ans = INF;
    FOR(1)FOR(2)FOR(3)FOR(4)FOR(5)FOR(6)FOR(7)FOR(8)FOR(9)
    {
        b[0] = (a[0] + s[1] + s[2] + s[4]) % 4;
        b[1] = (a[1] + s[1] + s[2] + s[3] + s[5]) % 4;
        b[2] = (a[2] + s[2] + s[3] + s[6]) % 4;
        b[3] = (a[3] + s[1] + s[4] + s[5] + s[7]) % 4;
        b[4] = (a[4] + s[1] + s[3] + s[5] + s[7] + s[9]) % 4;
        b[5] = (a[5] + s[3] + s[5] + s[6] + s[9]) % 4;
        b[6] = (a[6] + s[4] + s[7] + s[8]) % 4;
        b[7] = (a[7] + s[5] + s[7] + s[8] + s[9]) % 4;
        b[8] = (a[8] + s[6] + s[8] + s[9]) % 4;
        if(accumulate(b , b + 9 , 0))continue;///保证最后都为0
        cnt = accumulate(s + 1 , s + 10 , 0);///计算s中所有数之和
        if(cnt < ans)
        {
            ans = cnt;
            copy(s + 1 , s+ 10 , res + 1);///把每个操作进行的次数存到res中。
        }
    }
    for(int i = 1 ; i < 10 ; i++)
    {
        while(res[i] --)
        {
            printf("%d%c" , i , --cnt ? ' ' : '\n');
        }
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值