Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

位运算

Posted by 396545313 at 2014-05-14 21:12:28 on Problem 1681
#include <stdio.h>
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
	int t;
	int min = -1;
	scanf("%d", &t);
	int tempwall[20];
	while (t--)
	{
		min = -1;
		int n;
		scanf("%d", &n);
		int wall[20] = { 0 };
		char c;
		for (int i = 1; i <= n; i++)
		{
			for (int j = 1; j <= n; j++)
			{
				cin >> c;
				wall[i] <<= 1;
				wall[i] += (c == 'y');
			}
		}
		int sum;
		int change[20];
		for (int i = 0; i < (1 << n); i++)
		{
			memcpy(tempwall, wall, sizeof(wall));
			change[1] = i;
			for (int j = 2; j <= n; j++)
			{
				change[j] = (~(change[j - 1] ^ tempwall[j - 1] ^ (change[j - 1] << 1) ^ (change[j - 1] >> 1))) &~(~0 << n);
				tempwall[j] = tempwall[j] ^ change[j - 1];
			}
			if (((change[n] ^ tempwall[n] ^ (change[n] << 1) ^ (change[n] >> 1))&~(~0 << n)) == ((1 << n) - 1))
			{
				sum = 0;
				for (int j = 1; j <= n; j++)
				for (int k = 0; k < n; k++)
					sum += (change[j] >> k) % 2;
				if (min == -1)
					min = sum;
				else
					min = min < sum ? min : sum;
			}
		}
		if (min != -1)
			cout << min << endl;
		else
			cout << "inf" << endl;
	}
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator