poj2965 The Pilots Brothers' refrigerator(直接计算或枚举Enum+dfs)
来源:互联网 发布:soap php 编辑:程序博客网 时间:2024/06/03 14:14
转载请注明出处:http://blog.csdn.net/u012860063?viewmode=contents
题目链接:http://poj.org/problem?id=2965
----------------------------------------------------------------------------------------------------------------------------------------------------------
欢迎光临天资小屋:http://user.qzone.qq.com/593830943/main
----------------------------------------------------------------------------------------------------------------------------------------------------------
Description
The game “The Pilots Brothers: following the stripy elephant” has a quest where a player needs to open a refrigerator.
There are 16 handles on the refrigerator door. Every handle can be in one of two states: open or closed. The refrigerator is open only when all handles are open. The handles are represented as a matrix 4х4. You can change the state of a handle in any location [i, j] (1 ≤ i, j ≤ 4). However, this also changes states of all handles in row i and all handles in column j.
The task is to determine the minimum number of handle switching necessary to open the refrigerator.
Input
The input contains four lines. Each of the four lines contains four characters describing the initial state of appropriate handles. A symbol “+” means that the handle is in closed state, whereas the symbol “−” means “open”. At least one of the handles is initially closed.
Output
The first line of the input contains N – the minimum number of switching. The rest N lines describe switching sequence. Each of the lines contains a row number and a column number of the matrix separated by one or more spaces. If there are several solutions, you may give any one of them.
Sample Input
-+-----------+--
Sample Output
61 11 31 44 14 34 4
转载():
证明:要使一个为'+'的符号变为'-',必须其相应的行和列的操作数为奇数;
可以证明,如果'+'位置对应的行和列上每一个位置都进行一次操作,则整个图只有这一'+'位置的符号改变,其余都不会改变.
> 设置一个4*4的整型数组,初值为零,用于记录每个点的操作数,那么在每个'+'上的行和列的的位置都加1,得到结果模2
(因为一个点进行偶数次操作的效果和没进行操作一样,这就是楼上说的取反的原理),
然后计算整型数组中一的
> 个数即为操作数,一的位置为要操作的位置(其他原来操作数为偶数的因为操作并不发生效果,因此不进行操作)
代码一如下:(直接计算结果)
#include <iostream>#include <cstring>using namespace std;int map[5][5];int ans;char a[8];void count(int i,int j){int k;for(k=0;k<4;k++){map[i][k]++;map[k][j]++;}map[i][j]--;//因为上面的for进行了两次map[i][j]++操作,此处减掉一次}void result(){ans=0;int i,j;for(i=0;i<4;i++)for(j=0;j<4;j++)ans+=map[i][j]%2;}void output(){cout<<ans<<endl;int i,j;for(i=0;i<4;i++)for(j=0;j<4;j++)if(map[i][j]%2==1)cout<<i+1<<' '<<j+1<<endl;}int main(){memset(map,0,sizeof(map));int i;int j;for(i=0;i<4;i++){cin>>a;for(j=0;j<4;j++)if(a[j]=='+')count(i,j);}result();output();return 0;}
代码二(Enum+dfs)如下:
#include <cstring>#include <iostream>using namespace std;//本题由于要输出每次翻转的棋子,因此不适宜用BFS,应该使用DFS输出完整路径bool lock[10][10];//实际只用了其中的row3:→row6,col3:→col6;bool flag;int step;int ri[17],cj[17];bool isopen(){for(int i = 3; i < 7; i++){for(int j = 3; j < 7; j++){if(lock[i][j]!=true)return false;}}return true;}void flip(int row, int col)//改变状态{lock[row][col] = !lock[row][col];for(int i = 3; i < 7; i++){lock[i][col] = !lock[i][col];}for(int j = 3; j < 7; j++){lock[row][j] = !lock[row][j];}return ;}void dfs(int row, int col, int deep){if(step == deep){flag = isopen();return;}if(flag || row == 7)return;flip(row,col);ri[deep] = row;//记录路径cj[deep] = col;if(col < 6)dfs(row,col+1,deep+1);elsedfs(row+1,3,deep+1);//从3开始的flip(row,col);//如果不符合状态就改回来if(col < 6)dfs(row,col+1,deep);elsedfs(row+1,3,deep);return;}int main(){char temp;int i, j;memset(lock,false,sizeof(lock));for(i = 3; i < 7; i++){for(j = 3; j < 7; j++){cin>>temp;if(temp == '-'){lock[i][j] = true;}}}for(step = 0; step <= 16; step++)//枚举步数{dfs(3,3,0);if(flag)break;}cout<<step<<endl;for(i = 0; i < step; i++){cout<<ri[i]-2<<' '<<cj[i]-2<<endl;}return 0;}
- poj2965 The Pilots Brothers' refrigerator(直接计算或枚举Enum+dfs)
- POJ2965,The Pilots Brothers' refrigerator,枚举,DFS
- poj2965 The Pilots Brothers' refrigerator dfs+枚举
- 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 dfs
- POJ2965 The Pilots Brothers' refrigerator(枚举+思维题)
- POJ1753 Flip Game & POJ2965 The Pilots Brothers' refrigerator (DFS)
- 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
- Java注解@interface的用法【转】
- Android Studio安装、配置
- 一张图理解AMQP
- 【源码】用1,2,2,3,4,5这六个数字,写一个函数,打印出所有不同的排序,要求:4不能放在第三位,3与5不能相连(C语言实现)
- Delphi 7连接Sybase 12.5数据库
- poj2965 The Pilots Brothers' refrigerator(直接计算或枚举Enum+dfs)
- Qt 单元测试
- 初探STL容器之Vector
- Microchip推出公司首款蓝牙® 4.1低功耗模块RN4020
- 【操作系统】进程
- jsp页面获取ip
- oracle常用表
- Android自动化测试之Monkeyrunner使用方法及实例
- xfire身份验证