首页 > 程序开发 > 软件开发 > 其他 >

HDU 4200 Bad Wiring(高斯消元)

2016-09-14

HDU 4200 Bad Wiring(高斯消元)

Bad Wiring

Time Limit: 2000/1000 MS (Java/Others)Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 925Accepted Submission(s): 301

Problem Description The ninja Ryu has infiltrated the Shadow Clan fortress and finds himself in a long hallway. Although ninjas are excellent fighters, they primarily rely on stealth to complete their missions. However, many lights are turned on in the hallway, and this way it will not take long before Ryu is spotted by a guard. To remain unseen, Ryu will need to turn off all the lights as quickly as possible.

The hallway contains a sequence of n lights L1……Ln. Some of these lights are turned on. Destroy-ing the lights with his shurikens would be too loud, so he needs to turn them off the old-fashioned way, using light switches. Luckily, there is a switch box nearby with a light switch Si for every light Li. However, after trying one of the switches, he notices something funny. When he

ips the switch Si, it does not only turn on/off light Li, but also some of the neighboring lights. Ryu notices that there is a parameter D such that

ipping switch Si turns on/off all the lights L(i-D)……L(i+D), if they exist(This means that S1 turns on/off all the lights L1 ……L(D+1) and Sn turns on/off all the lights L(n-D)……Ln. Of course, if D>=n, then L(D+1) and L(n-D) will not exist either.). Turning on or off lights can attract the attention of the guards, so Ryu would like to turn off all the lights with the minimum number of times

ipping a switch. Can you help him out?

Input The first line of the input contains a single number: the number of test cases to follow. Each test case has the following format:

1.One line with two integers n (1 <= n <= 100) and D (0 <= D <= 15): the number of lights and the parameter mentioned above.

2.One line with n integers. The i(th) integer describes the current state of light Li, where 0 means off and 1 means on.

Output For every test case in the input, the output should contain one integer on a single line: the minimum number of times Ryu needs to flip a switch to turn off all the lights. If it is impossible to turn off all the lights, then output the string "impossible" instead.

In the first example below,flipping switch S4 followed by S7 will turn off all the lights.

Sample Input

2
7 3

1 1 1 0 0 0 0

5 1

1 0 1 0 1

Sample Output

2
3

题目大意:

给了 n 个灯 n 个开关,第 i 个开关可以控制 i?d —— i+d 之间的灯全部发生变化,现在给你 n 个灯的初始状态,让你求使这些灯全部关闭的最小步数。

解题思路:

这就是一个裸的高斯消元,但是有一些需要注意的问题,就是那个自由变元可能在中间,这就有点恶心了,我调了好久才发现这个错误,都是 POJ 数据太弱,我以为以前的模板应该没什么问题呢。。。不说啦 上代码

MyCode:

/**
2016 - 09 - 09  晚上
Author: ITAK

Motto:

今日的我要超越昨日的我,明日的我要胜过今日的我,
以创作出更好的代码为目标,不断地超越自己。
**/

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
const int INF = 1e9+5;
const int MAXN = 400;
const int MOD = 1e9+7;
const double eps = 1e-7;
const double PI = acos(-1);
using namespace std;
int equ, var;///equ个方程 var个变量
int a[MAXN][MAXN];///增广矩阵
int x[MAXN];///解集
int x_i[MAXN];
bool free_x[MAXN];///判断是不是自由变元
int free_num;///自由变元的个数
int Solve()
{
    int sum = 0;
    for(int i=0; i abs(a[Max_r][col]))
        Max_r = i;
        if(a[Max_r][col] == 0)
        {
            row--;
            continue;
        }
        if(Max_r != row)
            for(int i=col; i>=1;
        }
        for(int ii=row-1; ii>=0; ii--)
        {
            x[ii] = a[ii][var];
            for(int j=ii+1; j 0)
            a[i-1][i] = 1;
        if(i < equ)
            a[i+1][i] = 1;
    }
}
int main()
{
    equ = var = 20;
    int xx;
    while(cin>>xx)
    {
        Init();
        a[0][var] = xx;
        for(int i=1; i>a[i][var];
        Gauss();
    }
    return 0;
}
相关文章
最新文章
热点推荐