POJ 1753 Flip Game && POJ 2965 The Pilots Brothers‘ refrigerator

POJ 1753 Flip Game

Description

Flip game is played on a rectangular 4x4 field with two-sided pieces placed on each of its 16 squares. One side of each piece is white and the other one is black and each piece is lying either it's black or white side up. Each round you flip 3 to 5 pieces, thus changing the color of their upper side from black to white and vice versa. The pieces to be flipped are chosen every round according to the following rules:

  1. Choose any one of the 16 pieces.
  2. Flip the chosen piece and also all adjacent pieces to the left, to the right, to the top, and to the bottom of the chosen piece (if there are any).

Consider the following position as an example:

bwbw
wwww
bbwb
bwwb
Here "b" denotes pieces lying their black side up and "w" denotes pieces lying their white side up. If we choose to flip the 1st piece from the 3rd row (this choice is shown at the picture), then the field will become:

bwbw
bwww
wwwb
wwwb
The goal of the game is to flip either all pieces white side up or all pieces black side up. You are to write a program that will search for the minimum number of rounds needed to achieve this goal.

Input

The input consists of 4 lines with 4 characters "w" or "b" each that denote game field position.

Output

Write to the output file a single integer number - the minimum number of rounds needed to achieve the goal of the game from the given position. If the goal is initially achieved, then write 0. If it's impossible to achieve the goal, then write the word "Impossible" (without quotes).

Sample Input

bwwb
bbwb
bwwb
bwww

Sample Output

4

Source

Northeastern Europe 2000

 

题目大意:

给定4x4的棋盘,每个位置都有棋子,棋子有2种状态“黑”或“白”。每次操作可以改变某一位置及其上下左右最多5个棋子的状态,即黑->白,白->黑。给定一个棋盘的状态,问最少需要多少步能达到全黑或者全白的状态(或者永远实现不了)

思路:

对于每个棋子,被改变奇数次等价于被改变1次,被改变偶数次等价于没变,而对于某一个位置,先变和后变并没有区别,因此无须考虑操作顺序。使用位运算压缩状态(共16位)或者直接dfs搜索哪些位置需要被操作即可,注意最后保留最小结果

代码:

#include<stdio.h>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<climits>
#include<ctype.h>
#include<algorithm>
using namespace std;
#pragma comment(linker,"/STACK:102400000,102400000")
#define pi acos(-1.0)
#define eps 1e-8
#define MOD 1000000007
#define INF 0x3f3f3f3f
#define lb(x) (x&(-x))
#define clr(a,x) memset(a,x,sizeof(a))
#define mp(a,b) make_pair(a,b)
typedef long long LL;
const int maxn=10005;
char str[5][5];
int map[5][5];
int ans;
void operate(int n)
{
    int x=n/4;
    int y=n%4;
    map[x][y]=!map[x][y];
    if(x-1>=0&&x-1<4)
        map[x-1][y]=!map[x-1][y];
    if(x+1>=0&&x+1<4)
        map[x+1][y]=!map[x+1][y];
    if(y-1>=0&&y-1<4)
        map[x][y-1]=!map[x][y-1];
    if(y+1>=0&&y+1<4)
        map[x][y+1]=!map[x][y+1];
}
bool judge()
{
    for(int i=0;i<4;i++)
        for(int j=0;j<4;j++)
            if(map[i][j]!=map[0][0])
                return 0;
    return 1;
}
void dfs(int cur,int cnt)
{
    for(int i=cur+1;i<16;i++)
    {
        operate(i);
        if(judge())
            ans=min(ans,cnt);
        dfs(i,cnt+1);
        operate(i);
    }
}
int main(void)
{
    while(scanf("%s",str[0])!=EOF)
    {
        for(int i=1;i<4;i++)
            scanf("%s",str[i]);
        for(int i=0;i<4;i++)
            for(int j=0;j<4;j++)
                if(str[i][j]=='w')
                    map[i][j]=0;
                else
                    map[i][j]=1;
        if(judge())
        {
            printf("0\n");
            continue;
        }
        ans=INT_MAX;
        dfs(-1,1);
        if(ans==INT_MAX)
            printf("Impossible\n");
        else
            printf("%d\n",ans);
    }
    return 0;
}

与其类似的是POJ2965

POJ 2965 The Pilots Brothers' refrigerator

这个题是每次操作会改变所在行列所有7块的状态,思路类似,注意此题需要记录路径,不再赘述

代码:

#include<stdio.h>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<climits>
#include<ctype.h>
#include<algorithm>
#include<vector>
using namespace std;
#pragma comment(linker,"/STACK:102400000,102400000")
#define pi acos(-1.0)
#define eps 1e-8
#define MOD 1000000007
#define INF 0x3f3f3f3f
#define lb(x) (x&(-x))
#define clr(a,x) memset(a,x,sizeof(a))
#define mp(a,b) make_pair(a,b)
typedef long long LL;
const int maxn=10005;
char str[5][5];
int map[5][5];
int ans;
vector<pair<int,int> >v,va;
void operate(int x,int y)
{
    map[x][y]=!map[x][y];
    for(int i=0;i<4;i++)
        map[i][y]=!map[i][y];
    for(int i=0;i<4;i++)
        map[x][i]=!map[x][i];
}
bool judge()
{
    for(int i=0;i<4;i++)
        for(int j=0;j<4;j++)
            if(map[i][j]!=1)
                return 0;
    return 1;
}
void dfs(int cur)
{
    for(int i=cur+1;i<16;i++)
    {
        int x=i/4,y=i%4;
        operate(x,y);
        v.push_back(mp(x,y));
        if(judge()&&v.size()<ans)
        {
            va=v;
            ans=(int)va.size();
        }
        dfs(i);
        operate(x,y);
        v.pop_back();
    }
}
int main(void)
{
    while(scanf("%s",str[0])!=EOF)
    {
        v.clear();
        for(int i=1;i<4;i++)
            scanf("%s",str[i]);
        for(int i=0;i<4;i++)
            for(int j=0;j<4;j++)
                if(str[i][j]=='-')
                    map[i][j]=1;
                else
                    map[i][j]=0;
        if(judge())
        {
            printf("0\n");
            continue;
        }
        ans=INT_MAX;
        dfs(-1);
        printf("%d\n",ans);
        for(int i=0;i<va.size();i++)
            printf("%d %d\n",va[i].first+1,va[i].second+1);
    }
    return 0;
}

碎碎念:

有时候会问自己,如果当年再努力一点,拿了银牌能继续跟队友一起进步,而不是变得懒散不思进取,会不会人生轨迹变得不一样了呢?上周也试了单人去参加acm校赛,感慨万分,自己怀念的并不是打acm的日子,而是那个努力的自己啊

不管怎么样,好好生活

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了安卓应用、小程序、Python应用和Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值