POJ 1166 The Clocks(高斯消元)
2014-07-27 09:23
288 查看
题目大意:9种操作可以让不同的种转动90度,求最小上升的操作方式。
这是放在高斯消元里面的,但是高斯消元直接做的话,好象有问题,我搜了一下网上的结题报告,竟然好多人暴力搞定的啊、、、后来看了一篇说是进行行交换的时候不用找最大的交换,试了一下就过了啊、、但是还是有疑问,还请路过的大神给指点一下啊、、、为什么不选最大的就可以了啊?
The Clocks
Description
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.
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
Sample Output
这是放在高斯消元里面的,但是高斯消元直接做的话,好象有问题,我搜了一下网上的结题报告,竟然好多人暴力搞定的啊、、、后来看了一篇说是进行行交换的时候不用找最大的交换,试了一下就过了啊、、但是还是有疑问,还请路过的大神给指点一下啊、、、为什么不选最大的就可以了啊?
The Clocks
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 14390 | Accepted: 5762 |
|-------| |-------| |-------| | | | | | | | |---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; }
相关文章推荐
- POJ 1166 The Clocks 高斯消元 + exgcd(纯属瞎搞)
- POJ 1166 The Clocks (爆搜 || 高斯消元)
- poj_1166 The Clocks(高斯消元)
- POJ 1166 The Clocks 高斯消元 + exgcd(纯属瞎搞)
- poj 1166 The Clocks BFS or 高斯消元
- POJ 1166 枚举或者高斯消元
- POJ 1166 高斯消元 疑惑的思考
- poj1753--Flip Game(高斯消元问题2,枚举自由元的首杀)
- POJ 1487:Single-Player Games 浮点数高斯消元
- 【POJ 2947】Widget Factory(高斯消元+逆元)
- POJ 1830 开关问题(高斯消元)
- poj 2065SETI <高斯消元>
- POJ 1830 开关问题 高斯消元
- poj1830 高斯消元[3](经典的开关问题)
- POJ 2065 SETI(高斯消元解同余方程组)
- poj 1681 Painter's Problem(高斯消元)
- poj_1487 Single-Player Games(高斯消元+自由元)
- poj 1681(高斯消元。。。)
- poj 1222 高斯消元详解
- poj 2065 高斯消元取模解方程组