暴力枚举 --- 多方法求解

The Pilots Brothers' refrigerator
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 17598   Accepted: 6660   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

【题目来源】
http://poj.org/problem?id=2965
【题目大意】
有一个冰箱,冰箱上有16道门(MD,这是哪个奇葩制造的=_=||),按4*4排列,加号表示状态为open,减号表示状态为close,每次你可以改变一道门的状态,同时这一行
和这一列的所有门的状态将会改变,让你通过最小的步骤,将所有门打开,并输出路径。
【题目分析】
典型的暴搜,这题有多种方法,其中我自己觉得最简单的方法就是一次遍历,根本不用DFS、BFS什么的。这种方法很巧妙,想不通的话自己模拟去。
一次遍历AC代码:
//Accepted    
//232K    
//16MS    
//C++    
//1051B    
//2014-05-12 21:40:25
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#include<string>
using namespace std;
struct Node
{
    int x;
    int y;
};
Node path[16];
bool vis[4][4];
char Map[4][4];

int main()
{
    int i,j,k;
    for(i=0;i<4;i++)
        scanf("%s",&Map[i]);
    for(i=0;i<4;i++)
    {
        for(j=0;j<4;j++)
        {
            if(Map[i][j]=='+')
            {
                vis[i][j]=!vis[i][j];
                for(k=0;k<4;k++)
                {
                    vis[i][k]=!vis[i][k];
                    vis[k][j]=!vis[k][j];
                }
            }
        }
    }
    int cnt=0;
    for(i=0;i<4;i++)
    {
        for(j=0;j<4;j++)
        {
            if(vis[i][j])
            {
                path[cnt].x=i+1;
                path[cnt].y=j+1;
                cnt++;
            }
        }
    }
    cout<<cnt<<endl;
    for(i=0;i<cnt;i++)
    {
        printf("%d %d
",path[i].x,path[i].y);
    }
    return 0;
}

一次AC,时间很优,仅需16毫秒。

 方法二:BFS

AC代码:

#include<stdio.h>
#include<string.h>
#include<algorithm>
#define maxn 100000

struct node
{
    int r,c,pre,ans,mm;
}p[maxn],st,st1;

int id;
int flag;
bool vis[maxn];
char ss[maxn];

void bfs(int m)
{
    int be=0,ed=0;
    p[ed].c=p[ed].r=p[ed].pre=-1;
    p[ed].ans=0;
    p[ed++].mm=m;
    vis[m]=true;
    while(be!=ed)
    {
        st=p[be++];
        int site1= st.mm;
        if(site1==65535)
        {
            printf("%d
",st.ans);
            flag=be-1;
            return;
        }
        for(int i=0;i<=3;i++)
        {
            for(int j=0;j<=3;j++)
            {
                int site;
                site=site1;
                for(int t=i*4;t<=i*4+3;t++)
                  site^=(1<<(t));
                for(int t=j%4;t<=15;t+=4)
                  site^=(1<<(t));
                site^=(1<<(i*4+j));
                if(!vis[site])
                {
                    p[ed]=st;
                    p[ed].r=3-i;
                    p[ed].c=3-j;
                    p[ed].ans++;
                    p[ed].mm=site;
                    p[ed++].pre=be-1;
                    vis[site]=true;
                }
            }
        }
    }
}

void print(int n)
{
    if(p[n].pre==-1)
        return;
    printf("%d %d
",p[n].r+1,p[n].c+1);
    print(p[n].pre);
}

int main()
{
    int id=0;
    for(int i=0;i<4;i++)
    {
        scanf("%s",ss);
        for(int j=0;j<4;j++)
        {
            id=id*2;
            if(ss[j]=='-') id+=1;
        }
    }
    bfs(id);
    print(flag);
    return 0;
}
原文地址:https://www.cnblogs.com/crazyacking/p/3724297.html