POJ 1753 Flip Game(BFS)

链接:https://ac.nowcoder.com/acm/problem/106350
来源:牛客网

题目描述
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:
Choose any one of the 16 pieces.
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.
输入描述:
The input consists of 4 lines with 4 characters “w” or “b” each that denote game field position.
输出描述:
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).
示例1
输入

bwwb
bbwb
bwwb
bwww
输出

4

题意:一个4*4的棋盘,每次可以选择一个棋子,使得这个棋子和其上下左右的棋子都翻转。求最少多少次翻转可以得到全白或者全黑的棋盘。

题记:首先一个棋子最多选一次,因为选两次和不选是一样的。那么每个棋子都有一个状态1或者0(黑或白),用二进制来表示就是1111111111111111和0000000000000000,一共有216个状态。

那么我们可以用二进制去存状态,然后bfs找出答案。选择一个数使改变状态也可以用位运算去做。

例如题目给出的例子:
1010
0000
1101
1001

选择第三行第一个数

将原来的状态去异或

0000
1000
1100
1000

得到:

1010
1000
0001
0001

最后记得用vis记录已经遍历过的状态即可。

#include<iostream>
#include<queue>
#include<cstring>

using namespace std;
char str[5][5];
int vis[1<<16];//最多有2的16次方个状态

struct Node{
    int state;//状态
    int step;//步数
};

int dis[16]={
     51200,58368,29184,12544,
     35968,20032,10016,4880,
	 2248,1252,626,305,
	 140,78,39,19
};

int bfs(int state){
    memset(vis,0,sizeof(vis));
    queue<Node>q;
    Node st,ne;
    st.state=state;
    st.step=0;
    q.push(st);
    vis[state]=1;
    while(!q.empty()){
        st=q.front();
        q.pop();
        if(st.state==0||st.state==65535)
            return st.step;
        for(int i=0;i<16;i++){
            ne.state=st.state^dis[i];
            ne.step=st.step+1;
            if(vis[ne.state])
                continue;
            if(ne.state==0||ne.state==65535)
                return ne.step;
            vis[ne.state]=1;
            q.push(ne);
        }
    }
    return -1;
}

int main(){
    for(int i=0;i<4;i++)
        cin>>str[i];
    int state=0;
    for(int i=0;i<4;i++){
        for(int j=0;j<4;j++){
            state<<=1;
            if(str[i][j]=='b')
                state+=1;
        }
    }
    int ans=bfs(state);
    if(ans==-1)
        cout<<"Impossible"<<endl;
    else
        cout<<ans<<endl;
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值