POJ 1166 The Clocks(高斯消元)

46 篇文章 0 订阅
23 篇文章 0 订阅

 题目大意:9种操作可以让不同的种转动90度,求最小上升的操作方式。

这是放在高斯消元里面的,但是高斯消元直接做的话,好象有问题,我搜了一下网上的结题报告,竟然好多人暴力搞定的啊、、、后来看了一篇说是进行行交换的时候不用找最大的交换,试了一下就过了啊、、但是还是有疑问,还请路过的大神给指点一下啊、、、为什么不选最大的就可以了啊?

The Clocks
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 14390 Accepted: 5762

Description

|-------|    |-------|    |-------|

|       |    |       |    |   |   |

|---O   |    |---O   |    |   O   |

|       |    |       |    |       |

|-------|    |-------|    |-------|

    A            B            C    



|-------|    |-------|    |-------|

|       |    |       |    |       |

|   O   |    |   O   |    |   O   |

|   |   |    |   |   |    |   |   |

|-------|    |-------|    |-------|

    D            E            F    



|-------|    |-------|    |-------|

|       |    |       |    |       |

|   O   |    |   O---|    |   O   |

|   |   |    |       |    |   |   |

|-------|    |-------|    |-------|

    G            H            I    

(Figure 1)

There are nine clocks in a 3*3 array (figure 1). The goal is to return all the dials to 12 o'clock with as few moves as possible. There are nine different allowed ways to turn the dials on the clocks. Each such way is called a move. Select for each move a number 1 to 9. That number will turn the dials 90' (degrees) clockwise on those clocks which are affected according to figure 2 below. 
Move   Affected clocks

 

 1         ABDE

 2         ABC

 3         BCEF

 4         ADG

 5         BDEFH

 6         CFI

 7         DEGH

 8         GHI

 9         EFHI    

   (Figure 2)

Input

Your program is to read from standard input. Nine numbers give the start positions of the dials. 0=12 o'clock, 1=3 o'clock, 2=6 o'clock, 3=9 o'clock.

Output

Your program is to write to standard output. Output a shortest sorted sequence of moves (numbers), which returns all the dials to 12 o'clock. You are convinced that the answer is unique.

Sample Input

3 3 0
2 2 2
2 1 2

Sample Output

4 5 8 9
#include <algorithm>
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <iomanip>
#include <stdio.h>
#include <string>
#include <queue>
#include <cmath>
#include <stack>
#include <map>
#include <set>
#define eps 1e-10
///#define M 1000100
#define LL __int64
///#define LL long long
#define INF 0x7fffffff
#define PI 3.1415926535898
#define zero(x) ((fabs(x)<eps)?0:x)
#define mod 4

const int maxn = 340;

using namespace std;

int a[maxn][maxn];
int x[maxn];
int equ, var;
char str[maxn];
int sum;

int LCM(int a, int b)
{
    return (a/(__gcd(a, b)))*b;
}

int Gauss()
{
    int row, col, max_r;
    row = col = 0;
    while(row < equ && col < var)
    {
        max_r = row;
        ///for(int i = row+1; i < equ; i++)
        ///    if(abs(a[i][col]) > abs(a[max_r][col])) max_r = i;
        ///if(max_r != row)
        ///    for(int j = col; j <= var; j++) swap(a[row][j], a[max_r][j]);
        if(a[row][col]==0)
            for(int i=row+1; i<equ; i++)
                if(a[i][col])
                    for(int j=col; j<=var; j++) swap(a[i][j],a[row][j]);
        if(a[row][col] == 0)
        {
            col++;
            continue;
        }
        for(int i = row+1; i < equ; i++)
        {
            if(a[i][col] == 0) continue;
            int l = LCM(abs(a[row][col]), abs(a[i][col]));
            int ta = l/a[i][col];
            int tb = l/a[row][col];
            if(ta*tb < 0) tb *= -1;///判断是否异号
            for(int j = col; j <= var; j++)
                a[i][j] = ((a[i][j]*ta - a[row][j]*tb)%mod + mod)%mod;
        }
        row++;
        col++;
    }
    for(int i = row; i < equ; i++)///无解的情况;
        if(a[i][col] != 0) return -1;
    if(row < var)///多组解的情况
        return var-row;
    for(int i = var-1; i >= 0; i--)///唯一解的情况,根据上三角阵,迭代求出每一次的值
    {
        int tmp = a[i][var];
        for(int j = i+1; j < var; j++)
            if(a[i][j] != 0) tmp = ((tmp-a[i][j]*x[j])%mod + mod)%mod;
        while(tmp%a[i][i] != 0)
            tmp += mod;
        x[i] = tmp/a[i][i]%mod;
        sum += x[i];
        x[i] %= 4;
        sum += x[i];
    }
    return 0;
}

void init()
{
    equ = var = 9;
    sum = 0;
    memset(x, 0, sizeof(x));
    memset(a, 0, sizeof(a));
    a[0][0]=a[1][0]=a[3][0]=a[4][0]=1;
    a[0][1]=a[1][1]=a[2][1]=1;
    a[1][2]=a[2][2]=a[4][2]=a[5][2]=1;
    a[0][3]=a[3][3]=a[6][3]=1;
    a[1][4]=a[3][4]=a[4][4]=a[5][4]=a[7][4]=1;
    a[2][5]=a[5][5]=a[8][5]=1;
    a[3][6]=a[4][6]=a[6][6]=a[7][6]=1;
    a[6][7]=a[7][7]=a[8][7]=1;
    a[4][8]=a[5][8]=a[7][8]=a[8][8]=1;
}

int main()
{
    init();
    for(int i = 0; i < 9; i++)
    {
        scanf("%d",&a[i][9]);
        a[i][9] = (4-a[i][9])%4;
    }
    Gauss();
    for(int i = 0; i < 9; i++)
    {
        while(x[i])
        {
            cout<<i+1;
            x[i]--;
            sum--;
            if(sum)
                cout<<" ";
            else
                cout<<endl;
        }
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值