poj 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 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 rowi and all handles in columnj.

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>
using namespace std;
bool mark[4][4];
char s[4][4];
int main()
{
    int i,j,k;
    int cx[16],cy[16];
    int time=0;
    memset(mark,0,sizeof(mark));
    for(i=0;i<4;++i)
        cin>>s[i];
    for(int i=0;i<4;++i)
        for(int j=0;j<4;++j)
    {
        char c=s[i][j];
        if(c=='+')
        {
            mark[i][j]=!mark[i][j];
            for(k=0;k<4;k++)
            {
                mark[i][k]=!mark[i][k];
                mark[k][j]=!mark[k][j];
            }
        }
    }
    for(i=0;i<4;++i)
       for(j=0;j<4;++j)
        if(mark[i][j]==true)
       {
           cx[time]=i+1;
           cy[time]=j+1;
           time++;
       }
       printf("%d\n",time);
       for(i=0;i<time;i++)
       {
           printf("%d %d\n",cx[i],cy[i]);
       }
       return 0;
}
方法二:将所有位置的两种情况:翻转或者不翻转进行枚举,dfs搜索 每次搜索到更小的步数就进行记录
#include<cstring>
#include<cstdio>
#include<iostream>
using namespace std;
int mapp[5][5];
int jilux[20];
int jiluy[20];
int x[20];
int y[20];
int step=20;
int judge()//判断是否为全部开启状态
{
    int i,j;
    for(i=0;i<4;++i)
        for(j=0;j<4;++j)
        if(mapp[i][j]==0)return 0;
    return 1;
}
void flip(int x,int y)//转换开关的动作
{
    int i,j;
    for(j=0;j<4;++j)
        mapp[x][j]=!mapp[x][j];
    for(i=0;i<4;++i)
        mapp[i][y]=!mapp[i][y];
    mapp[x][y]=!mapp[x][y];
}
void dfs(int s,int b)//搜索,加入用map[x][y]记录翻过的点坐标
{
    if(judge())
    {
        if(step>b)
        {
            step=b;
            for(int i=1;i<=step;++i)
            {
                jilux[i]=x[i];
                jiluy[i]=y[i];
            }
        }
        return;
    }
    if(s>=16)return;
    dfs(s+1,b);
    int a1=s/4;
    int b1=s%4;
    flip(a1,b1);
    x[b+1]=a1+1;
    y[b+1]=b1+1;
    dfs(s+1,b+1);
    flip(a1,b1);
}
int main()
{
    int i,j;
    char c;
    memset(mapp,0,sizeof(mapp));
    for(i=0;i<4;i++)
        for(j=0;j<=4;j++)
    {
        c=getchar();
        if(c=='-')mapp[i][j]=1;
    }
    dfs(0,0);
    printf("%d\n",step);
    for(i=1;i<=step;++i)
        printf("%d %d\n",jilux[i],jiluy[i]);
    return 0;
}


 
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值