POJ 1753 (violent search) __ACM

Source: Internet
Author: User
Flip Game
Time Limit: 1000MS Memory Limit: 65536K
Total submissions: 44180 accepted: 18986

Description Flip Game is played on a rectangular 4x4 field with two-sided pieces placed on each of it squares. One side of each piece are white and the other one are black and each piece is lying either it's black or white side up. Each round for you on the flip 3 to 5 pieces, thus changing the color of their upper-side to white and vice versa. The pieces to is flipped are chosen every round according to the following rules:
Choose any one of the pieces.
The Flip of the chosen piece and also all adjacent pieces to the "right" to "the top" and "to" bottom 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 the pieces lying their white side. If we choose to flip the 1st piece from the 3rd row (this choice are shown at the picture), then the field would 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 are to write a program that would 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 which denote game field position.

Output Write to-output file a single integer number-the minimum number of rounds needed to achieve the goal of the G Ame 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

[Submit] [Go back] [Status] [discuss]

Home Page Go back to top





Brute Force search



/********************************************************** * Author:skymiange * email:wbx747635916@g mail.com * Last modified:2017-03-29 22:37 * Filename:POJ1753.cpp * Description: * ***********************
/#include <iostream> using namespace std;
int a[20][20];
int ans;
    BOOL Panduan () {int x=a[0][0];
        for (int i=0;i<4;i++) {for (int j=0;j<4;j++) {if (X!=a[i][j]) return false;
} return true;
    } void Fan (int x,int y) {if (x-1>=0) a[x-1][y]=!a[x-1][y];
    if (y-1>=0) a[x][y-1]=!a[x][y-1];
    if (x+1<4) a[x+1][y]=!a[x+1][y];
    if (y+1<4) a[x][y+1]=!a[x][y+1];
A[x][y]=!a[x][y];
        } void Dfs (int x,int y,int t) {if (Panduan ()) {if (ans > t) ans = t;
    Return
    } if (x >= 4 | | | y >= 4) return;
    int nx,ny;
    NX = (x + 1)%4; NY = y + (x+ 1)/4;
    DFS (NX,NY,T);

    Fan (x,y);
    DFS (NX,NY,T+1);

Fan (x,y);
    int main () {char s[20][20];
    for (int i=0;i<4;i++) {cin>>s[i];
            for (int i=0;i<4;i++) {for (int j=0;j<4;j++) {if (s[i][j]== ' B ') a[i][j]=0;
        else a[i][j]=1;
    }} ans=0x7f7f7f;
    DFS (0,0,0);
    if (ans!=0x7f7f7f) {cout<<ans<<endl;
    } else{cout<< "Impossible" <<endl; }
}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.