poj2965:
The Pilots Brothers’ refrigerator
题目描述:
he 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.
输入:
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.
输出:
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.
例如,输入:(引号内为实际输入)
“-+–”
“—-”
“—-”
“-+–”
输出:
6
1 1
1 3
1 4
4 1
4 3
4 4
一个冰箱上有4*4共16个开关,改变任意一个开关的状态(即开变成关,关变成开)时,此开关的同一行、同一列所有的开关都会自动改变状态。要想打开冰箱,要所有开关全部打开才行。
输入:一个4×4的矩阵,+表示关闭,-表示打开;
输出:使冰箱打开所需要执行的最少操作次数,以及所操作的开关坐标。
#include <stdio.h>
#include <string.h>
char handle[4][4];//存储字符
int record[4][4]; //记录每一位操作次数
//函数:输入,即设置开关
void set_handle()
{
int i;
for(i = 0; i < 4; i++)
{
scanf("%s", handle[i]);
}
memset(record, 0, sizeof(record));
}
char change_state(char state)
{
if(state == '+')
return '-';
return '+';
}
//函数:把m行n列的开关按下
void flip(int m, int n)
{
int i, j;
for(j = 0; j < 4; j++) //行操作
{
handle[m][j] = change_state(handle[m][j]);
}
for(i = 0; i < 4; i++) //列操作
{
if(i != m)
handle[i][n] = change_state(handle[i][n]);
}
record[m][n]++; //执行一次操作
}
//函数:对数组中所有为+位置的行和列所有的开关执行一次flip操作
void full_flip()
{
int i, j;
for(i = 0; i < 4; i++)
{
for(j = 0; j < 4; j++)
{
if(handle[i][j] == '+')
{
int t;
for(t = 0; t < 4; t++)
{
flip(i, t);
}
for(t = 0; t < 4; t++)
{
if(t != i)
{
flip(t,j);
}
}
}
}
}
}
//函数:遍历record数组,统计操作次数
void solute()
{
int count = 0;
int i, j;
for(i = 0; i < 4; i++)
{
for(j = 0; j < 4; j++)
{
if(record[i][j] & 1 != 0)
{
count ++;
}
}
}
printf("%d\n", count);
for(i = 0; i < 4; i++)
{
for(j = 0; j < 4; j++)
{
if(record[i][j] & 1 != 0)
{
printf("%d %d\n", i+1, j+1);
}
}
}
}
int main(void)
{
set_handle();
full_flip();
solute();
return 0;
}
代码在VC++6.0中正确运行,也可以在poj中正确运行。