poj 2965 The Pilots Brothers' refrigerator (枚举)
来源:互联网 发布:java做计算器 编辑:程序博客网 时间:2024/05/18 09:55
题意是类似黑白棋的游戏,当翻转一个 + 的时候,那一行列的都会被翻转。
求最小的翻转次数以及翻转过程。
直接上解析:
先看一个简单的问题,如何把'+'变成'-'而不改变其他位置上的状态?
答案是将该位置(i,j)及位置所在的行(i)和列(j)上所有的handle更新一次。
结果该位置被更新了7次,相应行(i)和列(j)的handle被更新了4次,剩下的被更新了2次.
被更新偶数次的handle不会造成最终状态的改变.
因此得出高效解法,在每次输入碰到'+'的时候, 计算所在行和列的需要改变的次数
当输入结束后,遍历数组,所有为奇数的位置则是操作的位置,而奇数位置的个数之和则是最终的操作次数.
注:该题不会有不可能的情况。
代码:
#include <iostream>#include <string>using namespace std;int main(){ char GIRD[5][5]; int gird[5][5]; string str[5]; while (cin >> str[0]) { cin >> str[1]; cin >> str[2]; cin >> str[3]; for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { gird[i][j] = 0; } } for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { GIRD[i][j] = str[i][j]; if (GIRD[i][j] == '+') { gird[i][j]++; for (int k = 0; k < 4; k++) { gird[i][k]++; gird[k][j]++; } } } } int count = 0; for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { //cout<<gird[i][j]<<" "; if (gird[i][j] % 2 == 1) count++; } //cout<<endl; } cout << count << endl; for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { if (gird[i][j] % 2 == 1) cout << i + 1 <<" "<< j + 1 << endl; } } } return 0;}
0 0
- POJ 2965 The Pilots Brothers' refrigerator 枚举
- POJ 2965 The Pilots Brothers' refrigerator(枚举)
- POJ 2965 The Pilots Brothers' refrigerator 枚举
- poj 2965 The Pilots Brothers' refrigerator (枚举)
- poj 2965 The Pilots Brothers' refrigerator[ 枚举 ]
- poj-2965 The Pilots Brothers' refrigerator -- 枚举
- poj 2965 The Pilots Brothers' refrigerator【枚举】
- The Pilots Brothers' refrigerator 枚举 Poj 2965
- POJ 2965 The Pilots Brothers' refrigerator(枚举+位运算)
- poj 2965 The Pilots Brothers' refrigerator(技巧枚举)
- POJ 2965 The Pilots Brothers' refrigerator【枚举+dfs】
- POJ 2965 The Pilots Brothers' refrigerator (枚举)
- POJ:2965 The Pilots Brothers' refrigerator(二进制枚举)
- POJ 2965 The Pilots Brothers' refrigerator 搜索+枚举
- POJ 2965-The Pilots Brothers' refrigerator(贪心+枚举)
- POJ 2965 The Pilots Brothers' refrigerator(dfs+枚举 || 规律)
- POJ 2965 The Pilots Brothers' refrigerator 枚举dfs
- POJ-2965-The Pilots Brothers' refrigerator-bfs枚举-位运算
- spring mvc 经典讲解
- 2014谷歌I/O上的Google Cloud Platform和Cloud Dataflow
- IRP_完成例程(一)-返回status_success
- Android常用组件之四大天王
- 美颜相机照片恢复,相机内存卡照片恢复
- poj 2965 The Pilots Brothers' refrigerator (枚举)
- Android的单位以及屏幕分辨率详解
- zoj 1158 判断2线段完全相交
- 移植tiny210的 触摸屏驱动
- Android开发中处理图片OOM (OutOfMemoryError) 的若干方法小结
- 一种典型的Content Provider 代码架构
- 云安全三大趋势:纵深防御、软件定义安全、设备虚拟化
- 闲来看看View.java的Developer Guides
- IE浏览器 应用程序发生异常 未知的软件异常(0x0000417),位置为 0x6546120