【SPOJ-BTTNS】Buttons【高斯消元】【异或方程组】
2016-02-18 12:19
411 查看
题意:
n*m的灯泡,每个灯泡有一个开关,每次按下开关可以使曼哈顿距离不大于k的灯泡都变成相反的状态(灭->亮,亮->灭)。给出最终的灯泡状态,求最少需要按多少次。每个开关最多按一次。
还是模板题...
一共n*m个方程。对于一个点,与它曼哈顿距离不小于k的点,方程系数为1,其余为0,。方程右边等于最后的状态。
直接消元就好了。
没找到好看的模板,就自己脑补了个,估计有点丑。
建方程组时不要枚举点,而是直接算出曼哈顿距离符合要求的点的坐标,直接填值。否则TLE。
注意n和m不要搞混了。
n*m的灯泡,每个灯泡有一个开关,每次按下开关可以使曼哈顿距离不大于k的灯泡都变成相反的状态(灭->亮,亮->灭)。给出最终的灯泡状态,求最少需要按多少次。每个开关最多按一次。
还是模板题...
一共n*m个方程。对于一个点,与它曼哈顿距离不小于k的点,方程系数为1,其余为0,。方程右边等于最后的状态。
直接消元就好了。
没找到好看的模板,就自己脑补了个,估计有点丑。
建方程组时不要枚举点,而是直接算出曼哈顿距离符合要求的点的坐标,直接填值。否则TLE。
注意n和m不要搞混了。
#include <cstdio> #include <algorithm> using namespace std; const int maxm = 405; int n, m, k, tot; bool a[maxm][maxm]; inline int iread() { int f = 1, x = 0; char ch = getchar(); for(; ch < '0' || ch > '9'; ch = getchar()) f = ch == '-' ? -1 : 1; for(; ch >= '0' && ch <= '9'; ch = getchar()) x = x * 10 + ch - '0'; return f * x; } inline int _abs(int x) { return x > 0 ? x : -x; } inline void get(int x, int y) { int l = max(1, x - k), r = min(n, x + k); for(int i = l; i <= r; i++) { int ll = max(1, y - k + _abs(i - x)), rr = min(m, y + k - _abs(i - x)); for(int j = ll; j <= rr; j++) a[(x - 1) * m + y][(i - 1) * m + j] = 1; } } void gauss() { for(int i = 1; i <= tot; i++) { if(!a[i][i]) for(int j = i + 1; j <= tot; j++) if(a[j][i]) { for(int k = 1; k <= tot + 1; k++) swap(a[j][k], a[i][k]); break; } for(int j = i + 1; j <= tot; j++) if(a[j][i]) for(int k = 1; k <= tot + 1; k++) a[j][k] ^= a[i][k]; } for(int i = tot; i >= 1; i--) for(int j = tot; j > i; j--) if(a[i][j]) a[i][tot + 1] ^= a[j][tot + 1]; } int main() { int T = iread(); while(T--) { n = iread(); m = iread(); k = iread(); tot = n * m; for(int i = 1; i <= tot; i++) for(int j = 1; j <= tot; j++) a[i][j] = 0; for(int i = 1; i <= n; i++) for(int j = 1; j <= m; j++) { a[(i - 1) * m + j][tot + 1] = iread(); get(i, j); } gauss(); int ans = 0; for(int i = 1; i <= tot; i++) ans += a[i][tot + 1]; printf("%d\n", ans); } return 0; }
相关文章推荐
- [iOS]The app icon set named "AppIcon" did not have any applicable content
- __stdcall,__cdecl,__fastcall的区别
- 初窥Linux 之 我最常用的20条命令
- 人物动画的控制
- HDU 5626 Clarke and points
- C#的闭包
- MySQL LEFT JOIN
- 安卓 ImageView ACTION_UP&DOWN监听
- HUST 1358 Uiwurerirexb jeqvad
- 取得编码后的HTTP字符串
- 信息指纹
- Android编程之LayoutInflater的inflate方法详解
- 信息指纹
- oracle重复数据操作
- LNK1104:无法打开文件'mfc90.lib“
- android学习参考资料
- PHP程序中 解析XML,遇到的问题
- java encapsulation
- HUST 1354 Rubiks
- android指定数据库的路径