poj2965 The Pilots Brothers' refrigerator
来源:互联网 发布:linux源码包安装步骤 编辑:程序博客网 时间:2024/06/06 18:28
枚举次数,DFS
和poj1753类似,区别是开关影响不同,本题是那行的横纵列都要改变状态。另一个区别是要记录每次改变的方法。使用结构体或者两个数组存储。
注意再改变状态的时候点(a,b)改变了两次,要再一次改回来。
#include <iostream>#include <cstring>bool table[6][6] = {0};int ans;bool find = 0;struct solution{ int row, col;}N[20];void output_table() { for(int i = 1; i < 5; i++) { std::cout << "\n"; for(int j = 1; j < 5; j++) { std::cout << table[i][j]; } }}void turn(int a, int b) { for(int i = 1; i < 5; i++) { table[i][b] = !table[i][b]; } for(int i = 1; i < 5; i++) { table[a][i] = !table[a][i]; } table[a][b] = !table[a][b];}bool is_open() { for(int i = 1; i <= 4; i++) { for(int j = 1; j <= 4; j++) { if(table[i][j] == 1) { return 0; } } } return 1;}void dfs(int a, int b, int depth) { if(depth == ans){ find = is_open(); if(find == 1) { std::cout << ans << std::endl; for(int i = 0; i < ans; i++) { std::cout << N[i].row << " " << N[i].col << std::endl; } return; } return; } if(a == 5 || find) { return; } turn(a, b); N[depth].row = a; N[depth].col = b; if(b < 4) { dfs(a, b+1, depth + 1); } else { dfs(a+1, 1, depth + 1); } turn(a, b); if(b < 4) { dfs(a, b+1, depth); } else { dfs(a+1, 1, depth); }}int main(int argc, char *argv[]) { std::ios::sync_with_stdio(false); memset(table, 0, sizeof(table)); char temp[10]; for(int i = 0; i < 4; i++) { std::cin >> temp; for(int j = 0; j < 4; j++) { if(temp[j] == '-') { table[i+1][j+1] = 0; } else if(temp[j] == '+') { table[i+1][j+1] = 1; } else { continue; } } } for(ans = 0; ans <= 16; ans++){ dfs(1, 1, 0); } return 0;}
阅读全文
0 0
- POJ2965 The Pilots Brothers' refrigerator
- POJ2965----The Pilots Brothers' refrigerator
- poj2965 The Pilots Brothers' refrigerator
- POJ2965-The Pilots Brothers' refrigerator
- poj2965--The Pilots Brothers' refrigerator
- poj2965 The Pilots Brothers' refrigerator
- poj2965 The Pilots Brothers' refrigerator
- The Pilots Brothers' refrigerator Poj2965
- POJ2965-The Pilots Brothers' refrigerator
- POJ2965-The Pilots Brothers' refrigerator
- poj2965-The Pilots Brothers' refrigerator
- poj2965-The Pilots Brothers' refrigerator
- poj2965 The Pilots Brothers' refrigerator
- POJ2965-The Pilots Brothers' refrigerator
- poj2965 The Pilots Brothers\' refrigerator
- poj2965 The Pilots Brothers' refrigerator
- POJ2965 The Pilots Brothers' refrigerator 枚举
- poj2965——The Pilots Brothers' refrigerator
- 比较两个字符串类型日期的相差天数
- 关于android6.0运行时权限(定位)
- EntityFramework Core 1.1 Add、Attach、Update、Remove方法如何高效使用详解
- 看懂中国寺庙的佛和菩萨
- 敏捷开发学习笔记-Agile development(AM)
- poj2965 The Pilots Brothers' refrigerator
- EntityFramework Core 1.1是如何创建DbContext实例的呢?
- 罗升阳 51test 博客
- MySQL数据库安装(CentOS操作系统/tar.gz方式)
- 为什么volatile不能保证原子性
- MySQL创建数据库指定字符集
- 464. Can I Win
- Java中使用动态代理实现拦截器
- 使用Redux和ngrx构建更好的Angular2应用(一)