您的位置:首页 > 其它

POJ 2965 The Pilots Brothers' refrigerator(暴力搜索)

2014-07-31 15:18 483 查看
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(因为一个点进行偶数次操作的效果和没进行操作一样,这是利用取反的原理),然后计算整型数组中‘1’的个数即为操作数,‘1’的位置为要操作的位置(其他原来操作数为偶数的因为操作并不发生效果,因此不进行操作)

代码如下:

#include<stdio.h>
int main()
{
//二维字符数组(map)储存地图;
//二维整形数组(map1)记录操作数
int i,j,k,sum=0,map1[4][4]={0},I[16],J[16];
char map[4][4];
for(i=0;i<4;i++)
{
scanf("%s",&map[i]);
}
//遍历字符数组,对每一个“+”进行一次操作,相应的行和列进行取反
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
if(map[i][j]=='+')
{
map1[i][j]=!map1[i][j];
for(k=0;k<4;k++)
{
map1[i][k]=!map1[i][k];
map1[k][j]=!map1[k][j];
}
}
}
}
//遍历整形数组,找到为“1”的点及个数即为要操作的点和操作的次数
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
if(map1[i][j]==1)
{
I[sum]=i+1;
J[sum]=j+1;
sum++;
}
}
}
printf("%d\n",sum);
for(i=0;i<sum;i++)
{
printf("%d %d\n",I[i],J[i]);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: