POJ2965-The Pilots Brothers' refrigerator
2017-05-29 22:10
274 查看
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
题意:电冰箱有16个把手,每个把手两种状态(开‘-’或关‘+’),只有在所有把手都打开时,门才开,给一个4*4的矩阵,可以改变任意一个把手的位置,但同时改变其所在的行和列,求最小步骤。
解题思路:暴力枚举每个把手变或不变
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <vector>
#include <set>
using namespace std;
#define LL long long
const int INF = 0x3f3f3f3f;
int a[20][20], r[20], c[20], visit[20][20];
char ch[10][10];
int mi;
struct node
{
int x, y;
} x[20], ans[20];
void dfs(int pos, int sum)
{
if (sum >= mi) return;
if (pos == 16)
{
int flag = 1;
for (int i = 0; i < 4; i++)
for (int j = 0; j < 4; j++)
if ((a[i][j] + r[i] + c[j] - visit[i][j]) % 2 == 1) flag = 0;
if (flag&&sum < mi)
{
mi = sum;
for (int i = 0; i < sum; i++)
ans[i] = x[i];
}
return;
}
visit[pos / 4][pos % 4] = 1;
x[sum].x = pos / 4, x[sum].y = pos % 4;
r[pos / 4]++, c[pos % 4]++;
dfs(pos + 1, sum + 1);
visit[pos / 4][pos % 4] = 0;
r[pos / 4]--, c[pos % 4]--;
dfs(pos + 1, sum);
}
int main()
{
while (~scanf("%s", ch[0]))
{
for (int i = 1; i < 4; i++)
scanf("%s", ch[i]);
for (int i = 0; i < 4; i++)
for (int j = 0; j < 4; j++)
a[i][j] = ch[i][j] == '+' ? 1 : 0;
mi = INF;
memset(visit, 0, sizeof visit);
memset(r, 0, sizeof r);
memset(c, 0, sizeof c);
dfs(0, 0);
printf("%d\n", mi);
for (int i = 0; i < mi; i++)
printf("%d %d\n", ans[i].x + 1, ans[i].y + 1);
}
return 0;
}
Time Limit: 1000MS | Memory Limit: 65536K | |||
Total Submissions: 26478 | Accepted: 10198 | 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
题意:电冰箱有16个把手,每个把手两种状态(开‘-’或关‘+’),只有在所有把手都打开时,门才开,给一个4*4的矩阵,可以改变任意一个把手的位置,但同时改变其所在的行和列,求最小步骤。
解题思路:暴力枚举每个把手变或不变
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <vector>
#include <set>
using namespace std;
#define LL long long
const int INF = 0x3f3f3f3f;
int a[20][20], r[20], c[20], visit[20][20];
char ch[10][10];
int mi;
struct node
{
int x, y;
} x[20], ans[20];
void dfs(int pos, int sum)
{
if (sum >= mi) return;
if (pos == 16)
{
int flag = 1;
for (int i = 0; i < 4; i++)
for (int j = 0; j < 4; j++)
if ((a[i][j] + r[i] + c[j] - visit[i][j]) % 2 == 1) flag = 0;
if (flag&&sum < mi)
{
mi = sum;
for (int i = 0; i < sum; i++)
ans[i] = x[i];
}
return;
}
visit[pos / 4][pos % 4] = 1;
x[sum].x = pos / 4, x[sum].y = pos % 4;
r[pos / 4]++, c[pos % 4]++;
dfs(pos + 1, sum + 1);
visit[pos / 4][pos % 4] = 0;
r[pos / 4]--, c[pos % 4]--;
dfs(pos + 1, sum);
}
int main()
{
while (~scanf("%s", ch[0]))
{
for (int i = 1; i < 4; i++)
scanf("%s", ch[i]);
for (int i = 0; i < 4; i++)
for (int j = 0; j < 4; j++)
a[i][j] = ch[i][j] == '+' ? 1 : 0;
mi = INF;
memset(visit, 0, sizeof visit);
memset(r, 0, sizeof r);
memset(c, 0, sizeof c);
dfs(0, 0);
printf("%d\n", mi);
for (int i = 0; i < mi; i++)
printf("%d %d\n", ans[i].x + 1, ans[i].y + 1);
}
return 0;
}
相关文章推荐
- poj2965 The Pilots Brothers' refrigerator
- poj2965 The Pilots Brothers' refrigerator(位运算)
- poj2965 The Pilots Brothers' refrigerator(直接计算或枚举Enum+dfs)
- poj2965-The Pilots Brothers' refrigerator
- poj2965 The Pilots Brothers' refrigerator
- [POJ2965]The Pilots Brothers' refrigerator【枚举】
- POJ2965-The Pilots Brothers' refrigerator
- POJ2965 The Pilots Brothers' refrigerator (精妙方法秒杀DFS BFS)
- POJ2965-The Pilots Brothers' refrigerator
- 枚举 poj2965 The Pilots Brothers' refrigerator
- poj2965 The Pilots Brothers' refrigerator
- POJ2965-The Pilots Brothers' refrigerator
- POJ2965《The Pilots Brothers' refrigerator》方法:DFS+Bit
- POJ2965----The Pilots Brothers' refrigerator
- 暴力枚举-POJ2965 The Pilots Brothers' refrigerator
- DFS.枚舉::poj1753 flip game && poj2965 the Pilots Brothers'refrigerator
- POJ2965 The Pilots Brothers' refrigerator dfs
- 搜索- poj2965 The Pilots Brothers' refrigerator
- POJ2965 The Pilots Brothers' refrigerator 枚舉題目
- xor高斯消元 poj2965 The Pilots Brothers' refrigerator