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

状態压缩0ms

Posted by KatrineYang at 2016-09-11 13:17:59 on Problem 1315
#include <iostream>
#include <stdio.h>
using namespace std;

int main() {
	while(1){
		int N;
		scanf("%d", &N);
		if(N == 0) break;
		bool qiang[8][8];
		int qstate[8] = {0};
		for(int i = 1; i <= N; i++){
			char s[10];
			scanf("%s", s);
			for(int j = 0; j < N; j++){
				if(s[j] == 'X') qiang[i][j] = 1;
				else qiang[i][j] = 0;
			}
		}
		for(int i = 1; i <= N; i++){
			for(int j = 0; j < N; j++){
				if(qiang[i][j]) qstate[i] += (1<<j);
			}
		}
		int dp[8][100] = {0};
		int p2 = (1<<N);
		for(int i = 1; i <= N; i++){

			for(int st = 0; st < p2; st++){
				if((st & qstate[i]) != 0) continue;
				bool occ[8] = {0};
				int gs = 0;
				for(int j = 0; j < N; j++){
					if((st & (1<<j)) != 0) {
						occ[j] = 1;
						gs++;
					}
				}
				bool kf = 1;
				bool ky = 1;
				for(int j = 0; j < N; j++){
					if(occ[j]){
						if(!kf){
							ky = 0;
							break;
						}
						kf = 0;
					}
					else if(qiang[i][j]){
						kf = 1;
					}
				}
				if(!ky) continue;
				for(int st1 = 0; st1 < p2; st1++){
					if((st & st1) != 0) continue;
					int tmp = gs + dp[i-1][st1];
					int newSt = (st | st1) & (~qstate[i]);
					if(tmp > dp[i][newSt]) dp[i][newSt] = tmp;
				}
			}
		}
		int mx = 0;
		for(int j = 0; j < p2; j++){
			if(dp[N][j] > mx) mx = dp[N][j];
		}
		printf("%d\n", mx);
	}
	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