poj2965 The Pilots Brothers' refrigerator(直接计算或枚举Enum+dfs)
转载请注明出处:http://blog.csdn.net/u012860063?
viewmode=contents
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
6
1 1
1 3
1 4
4 1
4 3
4 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);
else
dfs(row+1,3,deep+1);//从3開始的
flip(row,col);//假设不符合状态就改回来
if(col < 6)
dfs(row,col+1,deep);
else
dfs(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)的更多相关文章
- POJ - 2965 - The Pilots Brothers' refrigerator (高效贪心!!)
The Pilots Brothers' refrigerator Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 19356 ...
- POJ 2965:The Pilots Brothers' refrigerator
id=2965">The Pilots Brothers' refrigerator Time Limit: 1000MS Memory Limit: 65536K Total S ...
- poj 2965 The Pilots Brothers' refrigerator
The Pilots Brothers' refrigerator Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 18040 ...
- poj 2965 The Pilots Brothers' refrigerator(dfs 枚举 +打印路径)
链接:poj 2965 题意:给定一个4*4矩阵状态,代表门的16个把手.'+'代表关,'-'代表开.当16个把手都为开(即'-')时.门才干打开,问至少要几步门才干打开 改变状态规则:选定16个把手 ...
- POJ2965——The Pilots Brothers' refrigerator
The Pilots Brothers' refrigerator Description The game “The Pilots Brothers: following the stripy el ...
- [POJ2965]The Pilots Brothers' refrigerator (搜索/位运算)
题意 游戏“The Pilots Brothers:跟随有条纹的大象”有一个玩家需要打开冰箱的任务. 冰箱门上有16个把手.每个手柄可以处于以下两种状态之一:打开或关闭.只有当所有把手都打开时,冰箱才 ...
- The Pilots Brothers' refrigerator-DFS路径打印
I - The Pilots Brothers' refrigerator Time Limit:1000MS Memory Limit:65536KB 64bit IO Format ...
- poj2965 The Pilots Brothers' refrigerator
题目链接:http://poj.org/problem?id=2965 分析:1.这道题和之前做的poj1753题目差不多,常规思路也差不多,但是除了要输出最少步数外,还要输出路径.做这道题的时候在怎 ...
- poj2965 The Pilots Brothers' refrigerator —— 技巧性
题目链接:http://poj.org/problem?id=2965 题解:自己想到的方法是枚举搜索,结果用bfs和dfs写都超时了.网上拿别人的代码试一下只是刚好不超时的,如果自己的代码在某些方面 ...
随机推荐
- 使用Struts2和jQuery EasyUI实现简单CRUD系统(五)——jsp,json,EasyUI的结合
这部分比較复杂,之前看过自己的同学开发一个选课系统的时候用到了JSON,可是一直不知道有什么用.写东西也没用到.所以没去学他.然后如今以这样的怀着好奇心,这是做什么用的,这是怎么用的.这是怎么结合的心 ...
- Codeforces Round #168 (Div. 2)---A. Lights Out
Lights Out time limit per test 2 seconds memory limit per test 256 megabytes input standard input ou ...
- TT流程随笔
细节: 如果本地可以自动登录, 先实现本地登录,发送事件通知,再请求登录服务器 如果本地不可以登录(第一次或退出后),直接请求登录服务器 登录服务器返回消息服务器ip port / 文件服务器 链接消 ...
- 1003. 我要通过!(20) (ZJUPAT 模拟)
题目链接:http://pat.zju.edu.cn/contests/pat-b-practise/1003 "答案正确"是自己主动判题系统给出的最令人欢喜的回复.本题属于PAT ...
- 抓包函数-pcap_next
抓包函数 pcap_next_ex, pcap_next 抓包 #include <pcap/pcap.h> int pcap_next_ex(pcap_t *p, s ...
- 开源APM应用性能管理工具调研
近期在设计一个DevOps平台.希望整合一个APM工具进来,由于APM既可用于性能測试也可用于运维性能监控,是典型的Dev+Ops产品. 商业的APM工具国内外已经有不少成熟产品了,而开源的好像不多见 ...
- Linux体验之旅(一)——制作U启,安装rhel-server-6.3
U启制作: 双击UltraISO: 点击文件→打开: 选择rhel-server6.3 点击启动→选择写入硬盘映像 最后选择格式化优盘→写入→完毕 注意:启动盘制作完毕后一定记得将rhel-serve ...
- Android 对ScrollView滚动监听,实现美团、大众点评的购买悬浮效果
转帖请注明本文出自xiaanming的博客(http://blog.csdn.net/xiaanming),请尊重他人的辛勤劳动成果,谢谢! 随着移动互联网的快速发展,它已经和我们的生活息息相关了,在 ...
- tomcat 和 jboss的热部署(热发布)问题
所谓的热部署(热发布)(下面称为“热部署”),就是说,在web工程发布之后,不可避免的,会遇到修改BUG的问题.现在的热部署就是为了解决这个问题,其功能就是说:在不停止web服务的同时,对jsp和Ja ...
- Sphinx在windows下安装使用[支持中文全文检索]
原文地址:http://www.fuchaoqun.com/2008/11/sphinx-on-windows-xp/ 前 一阵子尝试使用了一下Sphinx,一个能够被各种语言(PHP/Python/ ...