POJ 2965--The Pilots Brothers' refrigerator
来源:互联网 发布:python可以建网站吗 编辑:程序博客网 时间:2024/05/25 08:15
2017-10-02
原题链接
题目大意:有4*4个开关,以‘+’表示开关的关状态,‘-’表示开关的开状态。现在可以通过给出一对值[i,j]来同时改变第i行和第j列所有开关的状态。求使得所有开关状态变为开所需的最少值对数,并给出这些值对。
样例输入:
-+-----------+--
样例输出:
61 11 31 44 14 34 4
本题的思想和POJ1753这题非常接近,也是枚举+DFS。需要注意的是,在同时改变第i行和第j列全部开关的状态时,第i行第j列的那个开关状态被改变了两次。
代码:
#include <iostream>using namespace std;bool swts[6][6]; //所有开关int chans = 1; //改变开关状态次数bool isSolved = false; //是否有解int ans[18][2]; //记录解void isAnswer() { //是否所有开关已经打开,是则为true for(int i = 1; i < 5; i++) for(int j = 1; j < 5; j++) if(swts[i][j] != true) return; isSolved = true; return;}void switchSwts(int x,int y) {//把x行y列的开关状态改变 for(int i = 1; i < 5; i++) { swts[x][i] = !swts[x][i]; swts[i][y] = !swts[i][y]; } swts[x][y] = !swts[x][y]; //*在上个代码段中,该开关状态被改变了两次*}void dfs(int x,int y,int dep) { isAnswer(); if(dep == chans) return; if(isSolved || x > 4) return; ans[dep][0] = x; ans[dep][1] = y; switchSwts(x,y); //改变开关状态 if(y < 4) dfs(x,y+1,dep+1); else dfs(x+1,1,dep+1); ans[dep][0] = x; ans[dep][1] = y; switchSwts(x,y); //还原 if(y < 4) dfs(x,y+1,dep); else dfs(x+1,1,dep);}int main(){ char c; //初始时开关的状态,-为true,+为false for(int i = 0; i < 4; i++) for(int j = 0; j < 4; j++) { cin >> c; swts[i+1][j+1] = (c == '-' ? true : false); } for(chans = 1; chans <= 16; chans++) {//枚举1~16步 dfs(1,1,0); if(isSolved) break; } cout << chans << endl; for(int i = 0; i < chans; i++) cout << ans[i][0] << " " << ans[i][1] << endl; return 0;}
阅读全文
0 0
- poj 2965 The Pilots Brothers' refrigerator
- POJ 2965 The Pilots Brothers' refrigerator
- poj 2965The 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
- 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
- POJ 2965 The Pilots Brothers' refrigerator 枚举
- POJ-2965-The Pilots Brothers' refrigerator
- c的部分算术运算符和算术表达式:
- DES算法的实现
- 【深入PHP 面向对象】读书笔记(八)
- 数据库的锁机制
- leetcode之链表逆序翻转类-----92/206 逆序 24/25/61/143 按规则翻转 86/234 双指针分治 19/82/83/203 按规则删除
- POJ 2965--The Pilots Brothers' refrigerator
- 《java基础与案例开发详解》(四)
- Mac下Nginx安装环境配置详解
- memcached的常用命令
- git使用流程及常用命令
- Java300StudyNote(8)-快速理解JDK&JRE&JVM
- Node.js学习-环境安装与配置
- 数字货币开发的底层技术如何实现执行智能合约代码
- 跳转语句----break和continue