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

H2O

Posted by KatrineYang at 2016-08-23 00:42:06 on Problem 1681
#include <iostream>
#include <stdio.h>
using namespace std;

int N;
int X[15][15];

int A[15][15];

int getAEntry(int x, int y){
	if(x < 0 || x >= N || y < 0 || y >= N) return 0;
	return A[x][y];
}

int main() {
	int T;
	scanf("%d", &T);
	for(int ii = 0; ii < T; ii++){
		scanf("%d", &N);
		for(int i = 0; i < N; i++){
			char s[16];
			scanf("%s", s);
			for(int j = 0; j < N; j++){
				if(s[j] == 'y') X[i][j] = 0;
				else X[i][j] = 1;
			}
		}
		if(N == 1){
			if(X[0][0] == 0) printf("0\n");
			else printf("1\n");
			continue;
		}
		int N2 = (1 << N);
		int H2O = 2147483647;
		for(int st = 0; st < N2; st++){
			int cs = 0;
			for(int i = 0; i < N; i++){
				A[0][i] = ((st & (1 << i)) > 0) ? 1 : 0;
				if(A[0][i]) cs ++;
			}
			for(int i = 1; i < N; i++){
				for(int j = 0; j < N; j++){
					A[i][j] = (X[i-1][j] + getAEntry(i-1, j-1) + getAEntry(i-1, j) + getAEntry(i-1, j+1) + getAEntry(i-2, j))%2;
					if(A[i][j]) cs++;
				}
			}
			bool keyi = 1;
			for(int i = 0; i < N; i++){
				if((X[N-1][i] + A[N-1][i] + getAEntry(N-1, i-1) + getAEntry(N-2, i) + getAEntry(N-1, i+1))%2 == 1){
					keyi = 0;
					break;
				}
			}
			if(keyi && cs < H2O) H2O = cs;
		}
		if(H2O == 2147483647) printf("inf\n");
		else printf("%d\n", H2O);
	}
	return 0;
}

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