POJ 2965 The Pilots Brothers' refrigerator(dfs+枚举 || 规律)

The Pilots Brothers' refrigerator
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 20778 Accepted: 7996 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
 
跟1753几乎一样的一个题 ,只不过这次翻一个点 ,其所在的行 和 列都要翻一次,问全变成'-',所需要的最小次数,并输出翻的点的坐标
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <stdlib.h>
#define N 5
#define inf 0x3f3f3f3f
using namespace std;
bool a[N][N];
bool flag;
int xx[20];
int yy[20];
int ans;
bool judge()
{
    for(int i=1;i<=4;i++)
    {
        for(int j=1;j<=4;j++)
          {
              if(a[i][j])
              return false;
          }
    }
    return true;
}
void change(int x,int y)
{
     a[x][y] = !a[x][y];
    for(int i=1;i<=4;i++)
    {
        a[x][i]=!a[x][i];
        a[i][y]=!a[i][y];

    }
}
void dfs(int x,int y,int step)
{
    if(step == ans)
    {
        if(judge())
          flag=true;
        return ;
    }
 if(x==5 || flag) return ;

    change(x,y);
    xx[step]=x;
    yy[step]=y;
    if(y<4)
        dfs(x,y+1,step+1);
    else
      dfs(x+1,1,step+1);

     change(x,y);
     if(y<4)
         dfs(x,y+1,step);
     else
        dfs(x+1,1,step);
}
int main()
{
    memset(a,false,sizeof(a));
    for(int i=1;i<=4;i++)
      {
          for(int j=1;j<=4;j++)
             {
                 char s;
                 cin>>s;
                 if(s=='+') a[i][j]=true;
             }
      }
      flag=false;
      for(ans=0;ans<=16;ans++)
      {
           dfs(1,1,0);
           if(flag)
              break;
      }
      printf("%d\n",ans);
      for(int i=0;i<ans;i++)
        printf("%d %d\n",xx[i],yy[i]);

    return 0;
}

一年前一次比赛做过的一道题,只不过英语狠渣,题意读了个半懂,知识发现把样例中'+'的行列全部变化后,输出的点都是变化次数为奇数的点,抱着试试的想法,结果一遍就A了。看的别人的解释:如果没遇到一个‘+’即关闭的开关,我们就把这个开关所在行和列包括它本身(本身只操作一次)都操作一次的话。那么可以计算它本身的状态变化次数为7,其同在一行和一列的元素则状态变化为4,余下的状态转化都是2.可以得到这个方法可以在不影响其它元素的状态下将关闭的开关打开(因为偶数次对状态实际上是没有影响的)(以前的代码写的真。。。。。)
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include<math.h>
using namespace std;
char a[5][5];
int b[5][5];
int main()
{
    int k,i,j,p;
    memset(b,0,sizeof(b));
    for(i=0; i<4; i++)
        scanf("%s",a[i]);
    for(i=0; i<4; i++)
        for(j=0; j<4; j++)     //先找到'+',每个变化的点+1
        {
            if(a[i][j]=='+')
            {
                b[i][j]++;
                for(k=1; k<=3; k++)
                {
                    p=i+k;
                    if(p>3)
                        p=p-4;
                    b[p][j]++;
                }
                for(k=1; k<=3; k++)
                {
                    p=j+k;
                    if(p>3)
                        p=p-4;
                    b[i][p]++;
                }
            }
        }
    int y=0;
    for(i=0; i<4; i++)
        for(j=0; j<4; j++)
            {
                if(b[i][j]%2==1)    //统计,输出奇数点
                y++;
            }
    printf("%d\n",y);
    for(i=0; i<4; i++)
        for(j=0; j<4; j++)
            {
                if(b[i][j]%2==1)
                printf("%d %d\n",i+1,j+1);
            }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值