poj2965The Pilots Brothers' refrigerator DFS+枚举

The Pilots Brothers‘ refrigerator

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 20858   Accepted: 8048   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

#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>

using namespace std;

int num=0x3f3f3f3f;
int a[10][10],b[10][10],flag;
int fanzhuan(int x,int y)
{
    a[x][y]=!a[x][y];
    for(int i=0; i<4; i++)
        a[x][i]=!a[x][i];

    for(int j=0; j<4; j++)
        a[j][y]=!a[j][y];
}
int panduan()
{
    for(int i=0; i<4; i++)
        for(int j=0; j<4; j++)
            if(!a[i][j])
                return 0;
    return 1;
}
struct node
{
    int a,b;
} p[20];
void DFS(int x,int y,int ans)//将所有的num步的情况都跑一遍判断是否有符合的
{
    if(num==ans)
    {
        flag=panduan();
        return ;
    }

    if(flag||x>=4||y>=4)
        return ;

    int fy=(y+1)%4;   //按行移动的
    int fx=x+(y+1)/4;

    fanzhuan(x,y);
    DFS(fx,fy,ans+1);
    p[ans].a=x;
    p[ans].b=y;
    fanzhuan(x,y);//原路返回
    DFS(fx,fy,ans);

}
int main()
{
    string s[4];
    while(cin>>s[0])
    {
        for(int i=1; i<4; i++)
            cin>>s[i];
        for(int i=0; i<4; i++)
            for(int j=0; j<4; j++)//格式转换
                if(s[i][j]=='+')
                    a[i][j]=0;
                else
                    a[i][j]=1;
        flag=0;
        for(int i=0; i<=16; i++)//枚举
        {
            num=i;
            DFS(0,0,0);
            if(flag)
                break;
        }
        cout<<num<<endl;
        for(int i=0; i<num; i++)
            cout<<p[i].a+1<<" "<<p[i].b+1<<endl;
    }
}

//方式2状态压缩+bfs
//..

版权声明:本文为博主原创文章,未经博主允许不得转载。

poj2965The Pilots Brothers' refrigerator DFS+枚举

时间: 2024-12-24 18:49:34

poj2965The Pilots Brothers' refrigerator DFS+枚举的相关文章

The Pilots Brothers&#39; refrigerator DFS+枚举

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 refri

POJ 2965 The Pilots Brothers&#39; 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 refri

poj2965The Pilots Brothers&#39; refrigerator

背景:和poj1753一样,用dfs就可以做出来,只是和1753相比较得输出一些步骤,不过这也不麻烦,直接用两个数组就可以存储了.不过记住当递归回来的时候记住把数组里面对应位置的元素清零. 思路:同上一篇1753. #include <stdio.h> #include <string.h> int q[4][4],ok=0,r[16],c[16]; int iswin(void) { for(int i=0;i<4;i++) for(int j=0;j<4;j++)

POJ 2965-The Pilots Brothers&#39; refrigerator(贪心+枚举)

The Pilots Brothers' refrigerator Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 19464   Accepted: 7462   Special Judge Description The game "The Pilots Brothers: following the stripy elephant" has a quest where a player needs to o

【递归】POJ2965-The Pilots Brothers&#39; refrigerator

和POJ1753的方法一致,唯一不同的是需要记录路径,只需添加一个全集变量的path路径即可. 1 #include<iostream> 2 #include<cstdio> 3 using namespace std; 4 const int INF=100000; 5 int map[4][4]; 6 int path[4][4]; 7 int maxpath[4][4]; 8 int ans=INF; 9 10 void open(int step,int turn) 11

poj 2965 The Pilots Brothers&#39; refrigerator(dfs 枚举 +打印路径)

链接:poj 2965 题意:给定一个4*4矩阵状态,代表门的16个把手,'+'代表关,'-'代表开,当16个把手都为开(即'-')时,门才能打开,问至少要几步门才能打开 改变状态规则:选定16个把手中的任意一个,可以改变其本身以及同行同列的状态(即若为开,则变为关,若为关,则变为开),这一次操作为一步. 分析:这题与poj 1753思路差不多,每个把手最多改变一次状态, 所有整个矩阵最多改变16次状态 思路:直接dfs枚举所有状态,直到找到目标状态 但是要打印路径,所有应在dfs时记录路径 注

poj2965 The Pilots Brothers&#39; refrigerator(直接计算或枚举Enum+dfs)

转载请注明出处:http://blog.csdn.net/u012860063?viewmode=contents 题目链接:http://poj.org/problem?id=2965 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

poj2965 The Pilots Brothers&#39; refrigerator 枚举或DFS

http://poj.org/problem?id=2965 一.枚举每一个‘+’点,对该点和该点所在的同一行和同一列所有点进行操作,开关本身状态改变了7次,开关同一行.同一列的开关状态改变了4次,其他开关状态改变了2次. 然后,用一个数组存取每个操作,如果为奇数,则证明该点为必须点,否则为不必要的. 1 #include<cstdio> 2 #include<cstring> 3 using namespace std; 4 char map[4][4]; 5 int ans[4

poj 2965 The Pilots Brothers&#39; refrigerator 【dfs+枚举】【双十一大礼包】

The Pilots Brothers' refrigerator Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 27522   Accepted: 10625   Special Judge Description The game “The Pilots Brothers: following the stripy elephant” has a quest where a player needs to open