POJ1753 Turn coin (DFS)

Source: Internet
Author: User
Tags min rounds

In POJ's first blood, a DFS solution, which has a lot of ingenious solutions, is worth learning.

But its head file incredibly can't #include<bits/stdc++.h>

Topic

Flip Game

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 47846 Accepted: 20410

Description Flip Game is played on a rectangular 4×4 field with two-sided pieces placed on each of its 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 your 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 is chosen every round according to the following rules:
Choose any one of the 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 is 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 was shown at the picture) and 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 is 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" from each of the 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 G Ame from the given position. If The goal is initially achieved and then write 0. If it's impossible to achieve the goal and then write the word "impossible" (without quotes).

Sample Input

BWWB
bbwb
bwwb
bwww

Sample Output

4

There are a total number of 4*4 coin-flipping coins, which is a question worth thinking about.

Assuming that only one, there are 16 kinds of flipping, two, is a 16 choice 2 combination number problem. And so on, it can be concluded that a total of possible results have 2^16 results, with DFS will not explode ...

The bottom is a serious puzzle.

First write a judge function to determine if all the coins are the same as the above suit.

int judge ()
{
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 0;
return 1;
}

Then there is a flip function, which mainly performs a rollover operation.

void Flip (int x,int y)
{
A[x][y] =!a[x][y];
if (x-1>=0) a[x-1][y] =!a[x-1][y];
if (x+1<4) a[x+1][y] =!a[x+1][y];
if (y-1>=0) a[x][y-1] =!a[x][y-1];
if (y+1<4) a[x][y+1] =!a[x][y+1];
}

(a[x][y at first) =!a[x][y],wa for half a day)

Under the most important DFS,

void Dfs (int x,int y,int t)
{
if (judge () ==1)
{
ans = min (T,ans);
Return
}
if (x>=4| | y>=4) return;
int newx = (x+1)%4;
int newy = y+ (x+1)/4;
DFS (NEWX,NEWY,T);
Flip (x, y);
DFS (NEWX,NEWY,T+1);
Flip (x, y);
Return
}

The idea of stealing a big guy, his own data simulation can be learned that when the NEWX full 4 will return to 1, at this time Newy will automatically +1, the idea is very ingenious.

Each encounter a situation, there are two options, so Dfs two times, when the turn over, to flip back again, here is the thought of backtracking.


Below is the complete code.

#include <iostream>
#include <stdio.h>
using namespace Std;
const int INF = 999999;
int A[10][10],ans;
Char b[5][5];
int judge ()
{
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 0;
return 1;
}
void Flip (int x,int y)
{
A[x][y] =!a[x][y];
if (x-1>=0) a[x-1][y] =!a[x-1][y];
if (x+1<4) a[x+1][y] =!a[x+1][y];
if (y-1>=0) a[x][y-1] =!a[x][y-1];
if (y+1<4) a[x][y+1] =!a[x][y+1];
}
void Dfs (int x,int y,int t)
{
if (judge () ==1)
{
ans = min (T,ans);
Return
}
if (x>=4| | y>=4) return;
int newx = (x+1)%4;
int newy = y+ (x+1)/4;
DFS (NEWX,NEWY,T);
Flip (x, y);
DFS (NEWX,NEWY,T+1);
Flip (x, y);
Return
}
int main ()
{
Freopen ("D://test.txt", "R", stdin);
ans = INF;
for (int i=0;i<4;i++)
for (int j=0;j<4;j++)
{
cin>>b[i][j];
if (b[i][j]== ' B ') a[i][j] = 0;
else if (b[i][j]== ' W ') a[i][j] = 1;
}
DFS (0,0,0);
if (Ans==inf) cout<< "impossible";
else cout<<ans;
return 0;
}

This question is basically like this, hope oneself can persist down, ACM make a difference.



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.