POJ3279 Flip coin (state compression Dfs)

Source: Internet
Author: User
Tags first row

Daily Punch-In (1/1)

Portal: Click to open link

The main idea: a n*m matrix flip coin, turn a coin will involve around four, ask all the coins face up to the minimum need to turn over which few coins.


Ideas:

It is not the same as how many times the previous question asked, this time to record the status.

First, we first think of enumerating the first row, and then to the other rows, as long as the top of a coin is upside down, and finally check.

The first place to learn is how to enumerate the first row of the flipping method:


for (int i=0;i< (1<<m); i++)
	 dfs (i);
for (int j=0;j<m;j++)
	{
		if ((i>>j) &1) {
			flip (0,j);
			cnt++;
		}
	}

It was marvelous, and all at once a 2^m of the whole arrangement.

As for the status record, you can copy the array A to the array temp each time, and then open an array vis, if you turn over a piece, vis[x][y] becomes 1, and when the final check is satisfied, the VIS value is copied to the other array res, because the VIS array is cleared 0 each time, So we have two sets of solutions to compare the size of the solution can be used in the RES array to record the best answer.

At the end of the output, if ANS does not change, it shows no solution, output impossible;

Otherwise, the output res array.

The following code is attached:

#include <iostream> #include <cstdio> #include <algorithm> #include <cstring> #include <
String> #include <cmath> using namespace std;
const int MAXN = 18;
int m,n,ans=1e6,dir[4][2]={1,0,-1,0,0,1,0,-1};

BOOL A[MAXN][MAXN],VIS[MAXN][MAXN],TEMP[MAXN][MAXN],RES[MAXN][MAXN];
	void Flip (int x,int y) {vis[x][y] = 1;
	Temp[x][y] =!temp[x][y];
	if (x-1>=0) temp[x-1][y] =!temp[x-1][y];
	if (x+1<n) temp[x+1][y] =!temp[x+1][y];
	if (y-1>=0) temp[x][y-1] =!temp[x][y-1];
if (y+1<m) temp[x][y+1] =!temp[x][y+1];
	} bool Check () {for (Int. i=0;i<n;i++) for (int j=0;j<m;j++) if (Temp[i][j]) return false;
return true;
	} void Dfs (int k) {memcpy (temp,a,sizeof (a));
	memset (vis,0,sizeof (VIS));
	int cnt = 0;
			for (int j=0;j<m;j++) {if ((k>>j) &1) {Flip (0,j);
		cnt++;
	  	}} for (int i=1;i<n;i++) for (int j=0;j<m;j++) if (temp[i-1][j]==1) {flip (i,j);
	  cnt++;
		} if (check () &&cnt<ans) {ans = cnt;memcpy (res,vis,sizeof (VIS));
	}} int main () {scanf ("%d%d", &n,&m);
	for (int i=0;i<n;i++) for (int j=0;j<m;j++) scanf ("%d", &a[i][j]);
	for (int i=0;i< (1<<m); i++) Dfs (i);
	if (ans==1e6) cout<< "Impossible" <<endl;
		else for (int i=0;i<n;i++) {for (int j=0;j<m-1;j++) {cout<<res[i][j]<< "";
		} cout<<res[i][m-1];
	cout<<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.