poj The Clocks(暴搜)

http://poj.org/problem?id=1166


大致题意:输入一个3*3的矩阵,其中0=12 o'clock, 1=3 o'clock, 2=6 o'clock, 3=9 o'clock;现在需要最少的移动使9个时钟都拨到12点的位置。题中共有9种不同的移动方法,每移动一次,其对应时钟都会顺时针旋转90度。


思路:这题与模2的开关问题类似。开关只有两种状态,即开和关。时钟有4种状态,即0,1,2,3。看了网上的解法,都用暴搜做的,因为每种移动方法都只能移动0,1,2,3次,所以可以对每种移动方法进行枚举。共4^9种可能,不会TLE。所以直接暴搜吧。


#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <set>
#include <map>
#include <vector>
#include <math.h>
#include <string.h>
#include <queue>
#include <string>
#define LL long long
#define _LL __int64
#define eps 1e-8

using namespace std;

int main()
{
    int a[10],b[10],c[10];

    for(int i = 1; i <= 9; i++)
        scanf("%d",&a[i]);
    //枚举每一种移动方法,每一种可实施0,1,2,3次
    for(b[1] = 0; b[1] <= 3; b[1]++)
    for(b[2] = 0; b[2] <= 3; b[2]++)
    for(b[3] = 0; b[3] <= 3; b[3]++)
    for(b[4] = 0; b[4] <= 3; b[4]++)
    for(b[5] = 0; b[5] <= 3; b[5]++)
    for(b[6] = 0; b[6] <= 3; b[6]++)
    for(b[7] = 0; b[7] <= 3; b[7]++)
    for(b[8] = 0; b[8] <= 3; b[8]++)
    for(b[9] = 0; b[9] <= 3; b[9]++)
    {
        c[1] = (a[1] + b[1] + b[2] + b[4])%4;
        c[2] = (a[2] + b[1] + b[2] + b[3] + b[5])%4;
        c[3] = (a[3] + b[2] + b[3] + b[6])%4;
        c[4] = (a[4] + b[1] + b[4] + b[5] + b[7])%4;
        c[5] = (a[5] + b[1] + b[3] + b[5] + b[7] + b[9])%4;
        c[6] = (a[6] + b[3] + b[5] + b[6] + b[9])%4;
        c[7] = (a[7] + b[4] + b[7] + b[8])%4;
        c[8] = (a[8] + b[5] + b[7] + b[8] + b[9])%4;
        c[9] = (a[9] + b[6] + b[8] + b[9])%4;
        if(c[1] + c[2] + c[3] + c[4] + c[5] + c[6] + c[7] + c[8] + c[9] == 0)
        {
            for(int i = 0; i < b[1]; i++) printf("1 ");
            for(int i = 0; i < b[2]; i++) printf("2 ");
            for(int i = 0; i < b[3]; i++) printf("3 ");
            for(int i = 0; i < b[4]; i++) printf("4 ");
            for(int i = 0; i < b[5]; i++) printf("5 ");
            for(int i = 0; i < b[6]; i++) printf("6 ");
            for(int i = 0; i < b[7]; i++) printf("7 ");
            for(int i = 0; i < b[8]; i++) printf("8 ");
            for(int i = 0; i < b[9]; i++) printf("9 ");
            printf("\n");
            return 0;
        }
    }
}


你可能感兴趣的:(暴搜)