poj 2965 -- The Pilots Brothers' refrigerator

这篇博客介绍了一个利用位运算解决特定游戏任务的方法,具体是关于如何通过最小的操作次数打开一个冰箱门。详细解释了游戏背景、输入输出规则,并提供了一个C++代码示例来实现解决方案。
The Pilots Brothers' refrigerator
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 17620 Accepted: 6676 Special Judge

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

这个题用位运算搜索(枚举)即可,只不过多了一个打印路径。递归打印就行。

 1 /*======================================================================
 2  *           Author :   kevin
 3  *         Filename :   ThePilotsBrothersRefrigerator.cpp
 4  *       Creat time :   2014-05-13 19:58
 5  *      Description :
 6 ========================================================================*/
 7 #include <iostream>
 8 #include <algorithm>
 9 #include <cstdio>
10 #include <cstring>
11 #include <queue>
12 #include <cmath>
13 #define clr(a,b) memset(a,b,sizeof(a))
14 #define M (1<<17)
15 using namespace std;
16 int cnt[M],number;
17 struct node
18 {
19     int x,y,n;
20 }node[M];
21 int dir[4][4]={
22     0xf888,0xf444,0xf222,0xf111,
23     0x8f88,0x4f44,0x2f22,0x1f11,
24     0x88f8,0x44f4,0x22f2,0x11f1,
25     0x888f,0x444f,0x222f,0x111f
26 };
27 void Print(int s)
28 {
29     if(number == node[s].n)
30         return ;
31     int t = node[s].n;
32     Print(t);
33     printf("%d %d\n",node[t].x+1,node[t].y+1);
34 }
35 int BFS(int num)
36 {
37     queue<int>pq;
38     cnt[num] = 1;
39     pq.push(num);
40     while(!pq.empty()){
41         int v = pq.front();
42         pq.pop();
43         for(int i = 0; i < 4; i++){
44             for(int j = 0; j < 4; j++){
45                 int t = v^dir[i][j];
46                 if(t == 0x0000){
47                     cnt[t] = cnt[v];
48                     node[t] = {i,j,v};
49                     return cnt[t];
50                 }
51                 if(!cnt[t]){
52                     cnt[t] = cnt[v]+1;
53                     node[t] = {i,j,v};
54                     pq.push(t);
55                 }
56             }
57         }
58     }
59     return -1;
60 }
61 int main(int argc,char *argv[])
62 {
63     char str[6];
64     while(cin>>str){
65         clr(cnt,0);
66         clr(node,0);
67         number = 0;
68         for(int i = 0; str[i]; i++){
69             number = number << 1;
70             if(str[i] == '+'){
71                 number |= 1;
72             }
73         }
74         for(int i = 0; i < 3; i++){
75             cin>>str;
76             for(int j = 0; str[j]; j++){
77                 number = number << 1;
78                 if(str[j] == '+'){
79                     number |= 1;
80                 }
81             }
82         }
83         int steps = BFS(number);
84         printf("%d\n",steps);
85         Print(0);
86         printf("%d %d\n",node[0].x+1,node[0].y+1);
87     }
88     return 0;
89 }
View Code

 

转载于:https://www.cnblogs.com/ubuntu-kevin/p/3727128.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值