POJ--1753-flip game (DFS]

Source: Internet
Author: User

Link: http://poj.org/problem? Id = 1753

Question: A 4*4 square, with a white or black game, each operation is a color flip of a position, that is, the white turns black and the black turns white, and the color of the four adjacent positions also changes, asking how many times it will change at least to make all the pawns white or black.


Train of Thought: it is not hard to see that the result of an even number of times is the same as that of a piece without turning. If some pieces are selected, the turning order will not affect the final result, therefore, you can use DFS to enumerate the status of each piece: up to 2 ^ 16 complexity can be obtained, 65536 times.


However, I ran wa, because every time I turned the cards at the last point of DFS, I quit the function without judging them, because the condition I wrote is to judge before the next DFS flop, And the last point won't enter the next DFS because of the boundary problem, so I will add a separate judgment and it will pass.


# Include <cstring> # include <string> # include <fstream> # include <iostream> # include <iomanip> # include <cstdio> # include <cctype> # include <algorithm> # include <queue> # include <map> # include <set> # include <vector> # include <stack> # include <ctime> # include <cstdlib> # include <functional> # include <cmath> using namespace STD; # define PI ACOs (-1.0) # define maxn 810 # define EPS 1e-7 # define INF 0x7ffffffftypedef long ll; # define ls On L, M, RT <1 # define rson m + 1, R, RT <1 | 1 char Mapp [10] [10]; int MP [10] [10]; int ans; bool check () {int I, j; for (I = 0; I <4; I ++) {for (j = 0; j <4; j ++) {If (MP [I] [J]! = MP [0] [0]) return false;} return true;} void flip (int x, int y) {If (x + 1 <4) MP [x + 1] [Y] =! MP [x + 1] [Y]; If (x-1> = 0) MP [x-1] [Y] =! MP [x-1] [Y]; If (Y + 1 <4) MP [x] [Y + 1] =! MP [x] [Y + 1]; If (Y-1> = 0) MP [x] [Y-1] =! MP [x] [Y-1]; MP [x] [Y] =! MP [x] [Y];} void DFS (int x, int y, int step) {If (Step> 16) return; int I, J; bool flag = check (); If (FLAG) {If (Step <ans) ans = step; return;} flip (x, y); If (Y + 1 <4) DFS (X, Y + 1, step + 1); else if (x + 1 <4) DFS (x + 1, 0, step + 1 ); else {If (check () {If (Step + 1 <ans) ans = Step + 1 ;}} flip (x, y); If (Y + 1 <4) DFS (X, Y + 1, step); else if (x + 1 <4) DFS (x + 1, 0); else {If (check ()) {If (Step <ans) ans = step; return ;}} int main () {int I, j; bool flag; ans = 20; for (I = 0; I <4; I ++) {scanf ("% s", Mapp [I]); For (j = 0; j <4; j ++) {If (MAPP [I] [J] = 'B') MP [I] [J] = 1 ;}} flag = check (); If (FLAG) puts ("0"); else {DFS (0, 0); If (ANS <20) cout <ans <Endl; else cout <"impossible" <Endl;} return 0 ;}


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.