POJ 2965.The Pilots Brothers' refrigerator
2013-03-27 12:41
477 查看
题目连接:http://poj.org/problem?id=2965
题目大意:一个4*4的矩阵,标号为'+'或'-',当标号全为'-'时就算达到目的。一次操作可以改变‘+’为‘-’,或是‘-’为‘+’。(即符号改变)但是对于坐标[i,j]的改变将会同样施加到i行和j列。要求得出最小操作次数,以及有哪些操作,给出最小操作的坐标,有多个答案输出一周即可。
解题思路: 直接暴力求解,用0--->65535代表中共的2^16种操作方式。
#include <cstdio>
#include <cstring>
using namespace std;
int data;
int operate[16];
//将data的第pos位设置为flag
void set(int &data,int pos,int flag)
{
if (flag == 1)
data|=(1<<pos);
else
data&=(~(1<<pos));
}
//获得data的pos位置的值0 or 1
bool getbit(const int &data,int pos)
{
return data&(1<<pos);
}
//生成16种操作
void make_operate(int operate[16])
{
memset (operate, 0, 16*sizeof(int));
for (int i = 0; i < 16; ++i)
{
int r = i / 4;
int c = i % 4;
for (int j = 0; j < 4; ++j)
{
set (operate[i], 4*r+j, 1);
set (operate[i], 4*j+c, 1);
}
}
}
int main()
{
int data;
int operate[16];
make_operate(operate);
char tmp[6];
data = 0;
for (int i = 0; i < 4; ++i)
{
scanf ("%s", tmp);
for (int j = 0; j < 4; ++j)
{
if (tmp[j] == '+')
set(data, i*4+j, 1);
else
set(data, i*4+j, 0);
}
}
int tmpdata = data;
int solution;
int min = 17;
int answer;
for (solution = 0; solution < 65536; ++solution)
{
for (int i = 0; i < 16; ++i)
{
if (getbit(solution, i))
tmpdata ^= operate[i];
}
if (tmpdata == 0)
{
int count = 0;
for (int i = 0; i < 16; ++i)
{
if (getbit(solution, i))
count += 1;
}
if (count < min)
{
min = count;
answer = solution;
}
}
tmpdata = data;
}
if (min == 17)
printf("Impossible\n");
else
{
printf ("%d\n", min);
for (int i = 0; i < 16; ++i)
{
if (getbit(answer, i))
{
printf ("%d %d\n", i/4 + 1, i%4 + 1);
}
}
}
return 0;
}
题目大意:一个4*4的矩阵,标号为'+'或'-',当标号全为'-'时就算达到目的。一次操作可以改变‘+’为‘-’,或是‘-’为‘+’。(即符号改变)但是对于坐标[i,j]的改变将会同样施加到i行和j列。要求得出最小操作次数,以及有哪些操作,给出最小操作的坐标,有多个答案输出一周即可。
解题思路: 直接暴力求解,用0--->65535代表中共的2^16种操作方式。
#include <cstdio>
#include <cstring>
using namespace std;
int data;
int operate[16];
//将data的第pos位设置为flag
void set(int &data,int pos,int flag)
{
if (flag == 1)
data|=(1<<pos);
else
data&=(~(1<<pos));
}
//获得data的pos位置的值0 or 1
bool getbit(const int &data,int pos)
{
return data&(1<<pos);
}
//生成16种操作
void make_operate(int operate[16])
{
memset (operate, 0, 16*sizeof(int));
for (int i = 0; i < 16; ++i)
{
int r = i / 4;
int c = i % 4;
for (int j = 0; j < 4; ++j)
{
set (operate[i], 4*r+j, 1);
set (operate[i], 4*j+c, 1);
}
}
}
int main()
{
int data;
int operate[16];
make_operate(operate);
char tmp[6];
data = 0;
for (int i = 0; i < 4; ++i)
{
scanf ("%s", tmp);
for (int j = 0; j < 4; ++j)
{
if (tmp[j] == '+')
set(data, i*4+j, 1);
else
set(data, i*4+j, 0);
}
}
int tmpdata = data;
int solution;
int min = 17;
int answer;
for (solution = 0; solution < 65536; ++solution)
{
for (int i = 0; i < 16; ++i)
{
if (getbit(solution, i))
tmpdata ^= operate[i];
}
if (tmpdata == 0)
{
int count = 0;
for (int i = 0; i < 16; ++i)
{
if (getbit(solution, i))
count += 1;
}
if (count < min)
{
min = count;
answer = solution;
}
}
tmpdata = data;
}
if (min == 17)
printf("Impossible\n");
else
{
printf ("%d\n", min);
for (int i = 0; i < 16; ++i)
{
if (getbit(answer, i))
{
printf ("%d %d\n", i/4 + 1, i%4 + 1);
}
}
}
return 0;
}
相关文章推荐
- poj 2965 The Pilots Brothers' refrigerator
- poj 2965 The Pilots Brothers' refrigerator
- poj_2965 The Pilots Brothers' refrigerator(bfs+位运算)
- 解题报告:POJ 2965 The Pilots Brothers' refrigerator 两种做法
- POJ2965-The Pilots Brothers' refrigerator
- POJ 2965:The Pilots Brothers' refrigerator
- POJ-2965 The Pilots Brothers' refrigerator【模拟法】
- POJ 2965 The Pilots Brothers' refrigerator(dfs+记录路径)
- poj 2965 The Pilots Brothers' refrigerator (迭代加深dfs)
- POJ 2965 The Pilots Brothers' refrigerator(枚举+dfs)
- POJ - 2965 - The Pilots Brothers' refrigerator (高效贪心!!)
- POJ 2965 The Pilots Brothers' refrigerator (想法题)
- POJ-----2965---The Pilots Brothers' refrigerator---枚举规律
- POJ 2965, The Pilots Brothers' refrigerator
- POJ 2965 The Pilots Brothers' refrigerator
- poj 2965 The Pilots Brothers' refrigerator
- 1.2 POJ 2965 The Pilots Brothers' refrigerator
- POJ 2965 The Pilots Brothers' refrigerator
- poj2965 The Pilots Brothers' refrigerator
- poj 2965 The Pilots Brothers' refrigerator