博客
关于我
poj1222 EXTENDED LIGHTS OUT(gauss)
阅读量:805 次
发布时间:2023-03-03

本文共 1431 字,大约阅读时间需要 4 分钟。

分析:说实话,要是单看这道题,开关是1,关是0,这样的对应关系符合XOR的特点。因此我们可以列出若干异或方程。

由于我们要求的是一个矩阵,每个灯的状态由初始状态以及周围按钮的状态有关,所以得到的方程就是[x,y]^[x-1,y]^[x,y-1]^[x+1,y]^[x,y+1]=当前灯的状态。通过高斯消元求解即可。

在构造初始阵式的时候,坐标是否合法一定要判断准了。具体来说:

if (i>1) a[bh][get(i-1,j)]=1;if (j>1) a[bh][get(i,j-1)]=1;if (i<5) a[bh][get(i+1,j)]=1;if (j<6) a[bh][get(i,j+1)]=1;

需要记住:now++不要忘了。

总结一下可能出现解方程的题目特点:

  • 可以列出方程
  • 一般没有阶段性和次序性(不同于dp或搜索)
  • 由若干个状态确定一个已知状态
  • 接下来是代码片:

    #include#include#include#include#include#include#include#include#include

    #define m 30int mp[10][10];int a[35][35];int get(int x, int y) {return (x-1)*6 + y;}int gauss() {int now = 1, to;for (int i = 1; i <= m; i++) {for (to = now; to <= m; to++)if (a[to][i]) break;if (to > m) return 0;if (to != now)for (int j = 1; j <= m+1; j++)swap(a[to][j], a[now][j]);for (int j = 1; j <= m; j++)if (j != now && a[j][i])for (int k = 1; k <= m+1; k++)a[j][k]^= a[now][k];now++;}return 1;}

    int main() {int TT;scanf("%d", &TT);for (int T = 1; T <= TT; T++) {memset(a, 0, sizeof(a));for (int i = 1; i <= 5; i++)for (int j = 1; j <= 6; j++)scanf("%d", &mp[i][j]);for (int i = 1; i <= 5; i++)for (int j = 1; j <= 6; j++) {int bh = get(i, j);a[bh][m+1] = mp[i][j];if (i > 1) a[bh][get(i-1, j)] = 1;if (j > 1) a[bh][get(i, j-1)] = 1;if (i < 5) a[bh][get(i+1, j)] = 1;if (j < 6) a[bh][get(i, j+1)] = 1;a[bh][bh] = 1;}gauss();printf("PUZZLE #%d\n", T);for (int i = 1; i <= m; i++) {printf("%d", a[i][m+1]);if (i % 6 == 0) puts("");else printf(" ");}}return 0;}

    转载地址:http://hbxfk.baihongyu.com/

    你可能感兴趣的文章
    Python 字典 vs If 语句速度
    查看>>
    python 字典sorted自定义排序,按照key or value排序
    查看>>
    python 字典和列表区别_元组列表和字典的主要区别是什么?
    查看>>
    python 字符串中特定字符替换,截取
    查看>>
    Python 字符串总结
    查看>>
    python 字符串显示中文_Python字符串开头的b"、u"、r"与中文乱码
    查看>>
    Python 字符串的几种拼装方式
    查看>>
    python 存入数据库bigint_python基础_MySQL的bigint类型
    查看>>
    python 学习 面向对象编程
    查看>>
    Python 学习小结
    查看>>
    Python 学习日记第三篇 -- 字典
    查看>>
    Python 学习笔记(一)Data type
    查看>>
    python 安装 easy_install 和 pip 流程
    查看>>
    python 安装echarts
    查看>>
    python 安装opencv及问题解决
    查看>>
    Python 安装程序:“写入文件 C:\Python27\pythonw.exe 时出错“
    查看>>
    Python 安装避坑指南:从入门到进阶
    查看>>
    Python 安装配置详解
    查看>>
    python 实现儿童算术游戏
    查看>>
    python 实现字符串转整型
    查看>>