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

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\n",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\n",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\n",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;
}
复制代码

 

posted @   北岛知寒  阅读(188)  评论(0编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
点击右上角即可分享
微信分享提示
主题色彩