POJ-2965-ThePilotsBrothers'refrigerator
2016-07-25 16:38
399 查看
The Pilots Brothers' refrigerator
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
Source
Northeastern Europe 2004, Western Subregion
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<cstring>
using namespace std;
bool Map[4][4];
bool flag = false;
int step, top = 0;
struct node
{
int x, y;
}xt[20];
void fold(int x, int y)
{
for(int i = 0; i<4; ++i)
{
Map[x][i] = !Map[x][i];
Map[i][y]= !Map[i][y];
}
Map[x][y] = !Map[x][y];
}
int Make_sure()
{
for(int i = 0; i<4; ++i)
for(int j = 0; j<4; ++j)
if(!Map[i][j])return false;
return true;
}
void dfs(int x, int y, int s)
{
if(s==step)
{
flag = Make_sure();
return;
}
if(y==4)
{
++x;
y = 0;
}
if(x==4||flag)return;
fold(x,y);
xt[top].x = x;
xt[top].y = y;
++top;
dfs(x,y+1,s+1);
fold(x,y);
if(flag)return;
--top;
dfs(x,y+1, s);
}
int main()
{
char s[6];
for(int i = 0; i<4; ++i)
{
scanf("%s", s);
for(int j = 0; j<4; ++j)
{
if(s[j]=='+')Map[i][j] = 0;
else Map[i][j] = 1;
}
}
for(step = 0; step<=16;++step)
{
dfs(0,0,0);
if(flag)break;
}
printf("%d\n", step);
for(int i = 0; i<top; ++i)
printf("%d %d\n",xt[i].x+1, xt[i].y+1);
return 0;
}
Time Limit: 1000MS | Memory Limit: 65536K | |||
Total Submissions: 23718 | Accepted: 9141 | Special Judge |
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
Source
Northeastern Europe 2004, Western Subregion
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<cstring>
using namespace std;
bool Map[4][4];
bool flag = false;
int step, top = 0;
struct node
{
int x, y;
}xt[20];
void fold(int x, int y)
{
for(int i = 0; i<4; ++i)
{
Map[x][i] = !Map[x][i];
Map[i][y]= !Map[i][y];
}
Map[x][y] = !Map[x][y];
}
int Make_sure()
{
for(int i = 0; i<4; ++i)
for(int j = 0; j<4; ++j)
if(!Map[i][j])return false;
return true;
}
void dfs(int x, int y, int s)
{
if(s==step)
{
flag = Make_sure();
return;
}
if(y==4)
{
++x;
y = 0;
}
if(x==4||flag)return;
fold(x,y);
xt[top].x = x;
xt[top].y = y;
++top;
dfs(x,y+1,s+1);
fold(x,y);
if(flag)return;
--top;
dfs(x,y+1, s);
}
int main()
{
char s[6];
for(int i = 0; i<4; ++i)
{
scanf("%s", s);
for(int j = 0; j<4; ++j)
{
if(s[j]=='+')Map[i][j] = 0;
else Map[i][j] = 1;
}
}
for(step = 0; step<=16;++step)
{
dfs(0,0,0);
if(flag)break;
}
printf("%d\n", step);
for(int i = 0; i<top; ++i)
printf("%d %d\n",xt[i].x+1, xt[i].y+1);
return 0;
}
相关文章推荐
- Android Camera 相机开发详解
- ios8以上 ble 实现 bt sink
- mysql入门(1)--- SQL分类
- 在android中运行java main方法
- 求逆序数的函数
- Swift语法之 ---- ?和!区别
- Emgu在引用openCV时提示:无法加载 DLL“opencv_core2410”: 找不到指定的模块。
- tab选项卡不同样式的效果
- Oracle | 给表和字段添加注释
- 针对回车键触发的事件
- retrofit与响应式编程一:rxjava简介
- Apache实现Tomcat集群
- 使用jquery获取ur中指定参数值
- 一个不错的代码搜索引擎
- js闭包
- 【CentOS】比较有用的find系列工具
- HTML 标签速查表
- fisher vector
- android中jni,内存优化,设计模式总结
- JAVA ClassLoader 自定义类加载器