poj2965 (枚举+高效)

The Pilots Brothers' 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 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

题意:如果翻其中一个格子,改变的是那个格子,它所在的行和列的格子都要翻转,求最少翻几次能使此图全为减号。

思路:这道题需要记录路径,所以要用深搜,我们知道一个格子可能翻转,也可能不翻转,所以深搜的复杂度是2的16次方,但细节上也需要注意,否则也会超时。


#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
char str[10][10];
int ans;
int cx[20],cy[20],mx[20],my[20];
int Find()
{
    int s = 0;
    for(int i = 0; i<4; i++)
        for(int j = 0; j<4; j++)
        {
           if(str[i][j] != '-')
            return 1;
        }
    return 0;
}
void turn(int x,int y)//对图形进行翻转
{
    for(int i = 0; i<4; i++)
    {
        if(y!=i)
        {
            if(str[x][i] == '-')
                str[x][i] = '+';
            else str[x][i] = '-';
        }
        if(str[i][y] == '-')
            str[i][y] = '+';
        else str[i][y] = '-';
    }
}
void dfs(int x,int y,int step)
{

    int sum = Find();//如果sum换成二进制数进行标记也会超时
    if(sum == 0)
    {
        if( step < ans)
        {
            ans = step;
            for(int i = 0; i<step; i++)
            {
                mx[i] = cx[i];
                my[i] = cy[i];
            }
        }
        return;
    }
    if(x>3 || step > 16) return;
    //必须先进性翻转,否则回溯时才进行翻转,结果不一样。
    turn(x,y);
    cx[step] = x;//记录路径
    cy[step] = y;
    if(y+1<=3)
        dfs(x,y+1,step+1);
    else dfs(x+1,0,step+1);

    turn(x,y);
    if(y+1<=3)
        dfs(x,y+1,step);
    else dfs(x+1,0,step);
}
int main()
{
    for(int i = 0; i<4; i++)
        scanf("%s",str[i]);
    ans = 99999;
    dfs(0,0,0);
    printf("%d\n",ans);
    for(int i = 0; i<ans; i++)
        printf("%d %d\n",mx[i]+1,my[i]+1);
    return 0;
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值