博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
POJ 2965, The Pilots Brothers' refrigerator
阅读量:5732 次
发布时间:2019-06-18

本文共 3395 字,大约阅读时间需要 11 分钟。

POJ 2965, The Pilots Brothers' refrigerator

POJ 1753, Flip Game
这两道题类似,可转化图论的最短路径问题:
顶点(Vertex)数:65536
边(Edge)数:65536 * 16 / 2
边的权值(Weight):1
对于给定两个点, 求最短路径:
由于边的权值均为1, 所以适宜采用BFS + Mark Table, 时间复杂度为O(N), N为顶点数。

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

 

Source

Northeastern Europe 2004, Western Subregion


//
 POJ1753.cpp : Defines the entry point for the console application.
//
#include 
<
algorithm
>
#include 
<
iostream
>
#include 
<
sstream
>
using
 
namespace
 std;
template
<
typename Target, typename Source
>
Target lexical_cast(Source arg){
    std::stringstream interpreter;
    Target result;
    interpreter
<<
arg;
    interpreter
>>
result;
    
return
 result;
}
void
 minSwitchs(
int
 cposition)
{
    
if
 (cposition 
==
 
0
)
    {
        cout 
<<
 
"
0\n
"
;
        
return
;
    }
    
const
 unsigned 
short
 pattern[
16
=
 {
0xf888
,
0xf444
,
0xf222
,
0xf111
,
0x8f88
,
0x4f44
,
0x2f22
,
0x1f11
,
                                        
0x88f8
,
0x44f4
,
0x22f2
,
0x11f1
,
0x888f
,
0x444f
,
0x222f
,
0x111f
 };
    
const
 
int
 MAX 
=
 
65536
;
    
char
 cnt[MAX];
    unsigned 
short
 door[MAX];
    unsigned 
short
 queuen[MAX];
    memset(cnt,
-
1
,
sizeof
(cnt));
    cnt[cposition] 
=
 
0
;
    queuen[
0
=
 cposition;
    
int
 front 
=
 
-
1
;
    
int
    rear 
=
 
0
;
    unsigned 
short
 cpos 
=
 
0
;
    unsigned 
short
 npos 
=
 
0
;
    
while
 (front 
!=
 rear)
    {
        
if
(
++
front 
>
 
65535
)front 
%=
 
65536
;
        cpos 
=
 queuen[front];
        
for
 (
int
 i 
=
 
0
; i 
<
 
16
++
i) 
        {
            npos 
=
 cpos 
^
 pattern[i];;
            
if
 (cnt[npos] 
==
 
-
1
)
            {
                
if
(
++
rear 
>
 
65535
)rear 
%=
 
65536
;
                queuen[rear] 
=
 npos;
                cnt[npos] 
=
 cnt[cpos] 
+
 
1
;
                door[npos] 
=
 i;
            }
            
if
 (npos 
==
 
0
)
            {
                
int
 ppos 
=
 npos;
                
string
 ret 
=
 
""
;
                
int
 steps 
=
 cnt[npos];
                
while
 (ppos 
!=
 cposition)
                {
                    ret 
+=
 
"
\n
"
;
                    ret 
+=
 ((door[ppos]
&
3
+
 
1
+
 
'
0
'
;
                    ret 
+=
 
"
 
"
;
                    ret 
+=
 ((door[ppos]
>>
2
+
 
1
+
 
'
0
'
;
                    ppos 
=
 ppos 
^
 pattern[door[ppos]];
                }
                std::reverse(ret.begin(), ret.end());
                ret 
=
 lexical_cast
<
string
int
>
(steps) 
+
 
"
\n
"
 
+
 ret;
                cout 
<<
 ret;
                
return
;
            }
        }
    }
    cout 
<<
 
"
-1
"
;
}
int
 main(
int
 argc, 
char
*
 argv[])
{
    
int
 cposition 
=
 
0
;
    
char
 s[
5
];
    s[
4
]
=
'
\0
'
;
    
for
(
int
 i
=
1
;i
<=
4
;i
++
)
    {
        scanf(
"
%s
"
,s);
        
for
(
int
 j
=
0
;j
<=
3
;j
++
)
        {
            cposition 
<<=
 
1
;
            
if
(s[j]
==
'
+
'
)
++
cposition;
        }
    }
    minSwitchs(cposition);
    
return
 
0
;
}
   

转载于:https://www.cnblogs.com/asuran/archive/2009/09/27/1574806.html

你可能感兴趣的文章
Linux下MongoDB安装与配置
查看>>
DSL配置(PPPOA)
查看>>
WEBRTC执行流程
查看>>
Spring Boot 入门系列
查看>>
Spring Cloud版——电影售票系统<六>使用 Spring Cloud Config 统一管理微服务配置
查看>>
Java not support java EE1.3
查看>>
iptables规则备份及恢复、firewalld九个zone,service的操作
查看>>
www.conf配置文件的参数详解
查看>>
如何实现邀请好友帮抢票功能?
查看>>
深圳联通特邀湖北籍企业参观公司总部大楼举行
查看>>
告警系统主脚本、告警系统配置文件、告警系统监控项目
查看>>
Python 和 PyCharm 在 windows10 环境的安装和设置
查看>>
B-树,B+树与B*树的优缺点比较
查看>>
C语言入门基础之数组——数学和编程的完美结合(图)
查看>>
《远见》的读后感作文1000字范文
查看>>
重置密码、单用户模式、救援模式
查看>>
LAMP环境搭建1-mysql5.5
查看>>
第三课 Linux目录及文件管理、用户组管理及bash重定向
查看>>
shell 脚本攻略--小试牛刀
查看>>
spring boot view override
查看>>