Gambler Bo

题目连接:

http://acm.hdu.edu.cn/showproblem.php?pid=5755

Description

Gambler Bo is very proficient in a matrix game.

You have a N×M matrix, every cell has a value in {0,1,2}.

In this game, you can choose a cell in the matrix, plus 2 to this cell, and plus 1 to all the adjacent cells.

for example, you choose the cell (x,y), the value of (x,y) will be plused 2, and the value of (x−1,y)(x+1,y)(x,y−1)(x,y+1) will be plused 1.

if you choose the cell (1,2), the cell (1,2) will be plused 2, and the cell (2,2)(1,1)(1,3) will be plused 1, the cell (0,2) won't be changed because it's out of the matrix.

If the values of some cells is exceed 2, then these values will be modulo 3.

Gambler Bo gives you such a matrix, your task is making all value of this matrix to 0 by doing above operations no more than 2NM times.

Input

First line, an integer T. There are T test cases.

In each test, first line is two integers N,M, and following N lines describe the matrix of this test case.

T≤10,1≤N,M≤30, the matrix is random and guarantee that there is at least one operation solution.

Output

For each test, first line contains an integer num(0≤num≤2NM) describing the operation times.

Following num lines, each line contains two integers x,y(1≤x≤N,1≤y≤M) describing the operation cell.

The answer may not be unique, you can output any one.

Sample Input

2
2 3
2 1 2
0 2 0
3 3
1 0 1
0 1 0
1 0 1

Sample Output

1
1 2
5
1 1
1 3
2 2
3 1
3 3

Hint

题意

给你nm的矩阵,矩阵里面有值

你可以操作使得一个格子加二,然后他周围的格子加1,然后所有格子都得mod3

让你构造一个方案,使得所有格子都是0了

题解:

老题,开关灯的mod3版本。

两种做法 900^3,这个可以用bitset优化,毕竟只用存012三个状态。

30^3,知道第一行之后,剩下的可以自己推导出来,这个推倒比较烦,大家可以去试试。

下面代码是最智障版本,裸的900^3

代码

#include<bits/stdc++.h>
using namespace std;
const int maxn = 31;
const int Maxn = 31*31;
int n,m;
int mp[maxn][maxn],id[maxn][maxn],cnt=0;
int a[Maxn][Maxn],x[Maxn];
int gcd(int a,int b){if(b==0)return a;return gcd(b,a%b);
}
int lcm(int a,int b){return a*b/gcd(a,b);
}
void solve(){memset(a,0,sizeof(a));cnt=0;memset(mp,0,sizeof(mp));memset(id,0,sizeof(id));memset(x,0,sizeof(x));scanf("%d%d",&n,&m);for(int i=1;i<=n;i++)for(int j=1;j<=m;j++){scanf("%d",&mp[i][j]);id[i][j]=++cnt;}for(int i=1;i<=n;i++){for(int j=1;j<=m;j++){a[id[i][j]][n*m+1]=(3-mp[i][j])%3;a[id[i][j]][id[i][j]]=2;if(i>1)a[id[i][j]][id[i-1][j]]=1;if(i<n)a[id[i][j]][id[i+1][j]]=1;if(j>1)a[id[i][j]][id[i][j-1]]=1;if(j<m)a[id[i][j]][id[i][j+1]]=1;}}for(int i=1;i<=n*m;i++){int p=i;for(int j=i+1;j<=n*m;j++)if(abs(a[j][i])>abs(a[p][i]))p=j;if(a[p][i]){for(int j=i;j<=n*m+1;j++)swap(a[i][j],a[p][j]);for(int j=i+1;j<=n*m;j++){if(a[j][i]){int d = lcm(a[j][i],a[i][i]);int x1 = d/a[j][i],x2 = d/a[i][i];for(int t=i;t<=n*m+1;t++)a[j][t]=((a[j][t]*x1-a[i][t]*x2)%3+3)%3;}}}}for(int i=n*m;i>=1;i--){x[i]=a[i][n*m+1];for(int j=i+1;j<=n*m;j++){x[i]=((x[i]-a[i][j]*x[j])%3+3)%3;}x[i]=a[i][i]*x[i]%3;}int ans = 0;for(int i=1;i<=n*m;i++){ans+=x[i];}printf("%d\n",ans);for(int i=1;i<=n*m;i++){while(x[i]>0){printf("%d %d\n",(i-1)/m+1,(i-1)%m+1);x[i]--;}}}
int main(){int t;scanf("%d",&t);while(t--)solve();return 0;
}

hdu 5755 Gambler Bo 高斯消元相关推荐

  1. hdu 5755 Gambler Bo 高斯消元

    题目链接 给n*m的方格, 每个格子有值{0, 1, 2}. 然后可以对格子进行操作, 如果选择了一个格子, 那么这个格子的值+2, 这个格子上下左右的格子+1, 并且模3. 问你将所有格子变成0的操 ...

  2. HDU 5755 Gambler Bo(高斯消元裸题)——2016 Multi-University Training Contest 3

    传送门 Gambler Bo Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) ...

  3. [HDU 5755] Gambler Bo (高斯消元)

    HDU - 5755 给定一个 N×MN \times M的矩阵,每个格子都有一个数, 并且这些数都是 mod 3mod\ 3的 每次选一个格子操作,可以在这个格子加 2 mod 32\ mod\ 3 ...

  4. hdu 5755 Gambler Bo 三进制高斯消元(开关问题变形)

    题目链接 题意: 给出一个模3意义下的矩阵,每次选中一个元素就可以使得自身+2,上下左右元素+1.求所有元素变成0的一种解. 思路: 这个题目高斯消元复杂度明明是O(n^3)的啊,这个题O((n*m) ...

  5. HDU 5755 Gambler Bo(数论)

    题目链接: http://acm.split.hdu.edu.cn/showproblem.php?pid=5755 题目大意: 给你一个矩阵,矩阵中是0,1,2三个数,你每次可以给其中一个数加2,但 ...

  6. hdu 5755 Gambler Bo【gauss】

    思路: n*m个数变化,设置n*m个变量,每个位置对应5个变量发生改变,连立n*m个方程,高斯消元求解,注意取模. // hdu 5755 #include <cstdio> #inclu ...

  7. HDU 4870 Rating(高斯消元 )

    HDU 4870   Rating 这是前几天多校的题目,高了好久突然听旁边的大神推出来说是可以用高斯消元,一直喊着赶快敲模板,对于从来没有接触过高斯消元的我来说根本就是一头雾水,无赖之下这几天做DP ...

  8. 【HDU 5755】Gambler Bo(高斯消元)

    [HDU 5755]Gambler Bo(高斯消元) Gambler Bo Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 131072 ...

  9. hdu5755 Gambler Bo(高斯消元)

    思路:一个mod3版本的开关灯问题,对每个格子分别设操作了xi次,那么就可以列出一个n*m的方程组,然后高斯消元就可以了 #include<bits/stdc++.h> using nam ...

最新文章

  1. 轻松删除git本地创建的仓库
  2. 我是学计算机的上银行考什么条件,计算机专业能报考银行哪些岗位
  3. 大学计算机专业全英文论文,计算机专业大学生英文简历模板
  4. 设计模式之-命令模式(Command Pattern)
  5. 8 线程安全且高效的单例模式
  6. 关闭应用程序的几种方法
  7. 【Elasticsearch】Elasticsearch底层系列之Shard Allocation机制
  8. 用Node操作Firebird。
  9. 深度学习系列(二)【人类语言处理--语音辨识】
  10. 【Love2d从青铜到王者】第十六篇:Love2d之动画(Animation)
  11. 网易云音乐Eason Chen 歌词词云
  12. Python之建模数值逼近篇–最小二乘拟合
  13. 四步搞定小菊花 Loading 动画
  14. yum命令的基本用法
  15. 务虚:大局观、方法与关键点
  16. 人人车总部维权现场:员工无奈强冲 维权群近千人(图)
  17. python django 图片管理系统
  18. CMA大段设备内存分配
  19. Windows Media Player 修复
  20. 关闭Win10休眠和快速启动

热门文章

  1. EOS代码架构及分析(二)
  2. 商务与经济统计 笔记
  3. 大数据应用对企业税务风险管理影响
  4. JavaScript之caller和caller属性
  5. MATLAB与DSP(C6657)的TCP/IP通信实现
  6. 微信打开网址提示在浏览器中打开的办法
  7. 防止暴利破解,拒绝ip登陆
  8. 小姜杂谈:屏幕分辨率一次讲清楚
  9. MFC软件欢迎界面(基于对话框,VS2013)
  10. 如何选择一台适合个人使用的云服务器?