POJ-1753解题报告

本文讨论了如何使用深度优先遍历来解决FlipGame问题,包括输入输出规范、问题描述、代码实现和优化策略。重点在于理解问题本质并采用递归方法进行搜索,最终求得最小步骤数以达成目标状态。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

Flip Game
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 21178 Accepted: 9172

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

///////////////////////////////////////////////////////////////////////////////////////////////////////////////
这题的翻牌只有两种可能,翻或不翻,所以相当于我们在遍历一棵16层的二叉树,通过深度优先找出符合条件的所有方案,找出最理想的结果。
 
这题我花了很久,看了网上的解题报告才搞定,汗~~~~~我主要卡在深度优先遍历这棵树上,本来也是用递归的想法去遍历,但是递归没有处理好,弄了很久,后来看了网上的解题报告后,才把深度优先遍历的递归实现出来,平时对递归还是要加强学习,练习。
//////////////////////////////////////////////////////////////////////////////////////
代码:
 
/***********************
*   程序名:Flip Game.cpp
*   功能:ACM
************************/

#include <iostream>
using namespace std;

//void flip(int [][4], int, int);
//int success_fail(int [][4]);
void dfs(int, int, int );
int minimal = 99;
int pices[4][4];

int main()
{
    char string[4][4];
    int i, j, t;
    for(i = 0; i < 4; i++) {
        cin>>string[i];
        for(j = 0; j < 4; j++) {
            if(string[i][j] == 'w') {
                pices[i][j] = 0;
            }
            else {
                pices[i][j] = 1;
            }
        }
    }
    dfs(0, 0, 0);
    if(minimal != 99) {
        cout<<minimal<<endl;
    }
    else {
        cout<<"Impossible"<<endl;
    }
    return 0;
}

int success_fail()
{
    int SUM = 0, i, j;
    for(i = 0; i < 4; i++) {
        for(j = 0; j < 4; j++) {
            SUM += pices[i][j];
        }
    }
    if(SUM == 0 || SUM == 16) {
        return 1;
    }
    else {
        return 0;
    }
}

void flip(int i, int j)
{
    pices[i][j] = (pices[i][j]+1)%2;
    if(i > 0) {
        pices[i-1][j] = (pices[i-1][j]+1)%2;
    }
    if(i < 3) {
        pices[i+1][j] = (pices[i+1][j]+1)%2;
    }
    if(j > 0) {
        pices[i][j-1] = (pices[i][j-1]+1)%2;
    }
    if(j < 3) {
        pices[i][j+1] = (pices[i][j+1]+1)%2;
    }
}

void dfs(int x, int y, int step)
{
    int temp_x, temp_y;
    //cout<<"dfs"<<endl;
    //cout<<"x="<<x<<"y="<<y<<endl;
    if(success_fail()) {
        //cout<<"success"<<endl;
        if(minimal > step) {
            minimal = step;
        }
    }
    else {
        if(x != 4) { //判断是否越界,由于需要访问最后一个点,所以临界点在最后一个点后面
            temp_x = x;
            temp_y = y;
            if((y+1) > 3) {
                x += 1;
                y = 0;
            }
            else {
                y += 1;
            }
            flip(temp_x, temp_y);//实现深度优先遍历,递归实现
            dfs(x, y, step+1);
            flip(temp_x, temp_y);
            dfs(x, y, step);
        }
    }
}


 
 
      

 

 



 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值