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

dfs1A过,注意scanf("\n%c",&c)的\n一定不能掉,否则会把结果输出两次!!!

Posted by KatrineYang at 2016-11-14 12:56:54 on Problem 1574
#include <stdio.h>

int tri[6][3];
int sts[6];
int used[6];
int sx[6];

int mxSc;

int zuo(int st){
	return st;
}

int you(int st){
	return (st+2)%3;
}

int wai(int st){
	return (st+1)%3;
}

void dfs(int jd){
	if(jd == 0){
		for(int i = 0; i < 3; i++){
			sts[0] = i;
			sx[0] = 0;
			dfs(1);
		}
	}
	else if(jd != 5){
		for(int i = 1; i < 6; i++){
			if(used[i]) continue;
			for(int j = 0; j < 3; j++){
				if(tri[i][zuo(j)] == tri[sx[jd-1]][you(sts[jd-1])]){
					sts[jd] = j;
					sx[jd] = i;
					used[i] = 1;
					dfs(jd+1);
					used[i] = 0;
				}
			}
		}
	}
	else{
		for(int i = 1; i < 6; i++){
			if(used[i]) continue;
			for(int j = 0; j < 3; j++){
				if(tri[i][zuo(j)] == tri[sx[4]][you(sts[4])] && tri[i][you(j)] == tri[0][zuo(sts[0])]){
					sts[jd] = j;
					sx[jd] = i;
					int sc = 0;
					for(int k = 0; k < 6; k++){
						sc += tri[sx[k]][wai(sts[k])];
					}
					if(sc > mxSc) mxSc = sc;
				}
			}
		}
	}
}

void solve(){
	//printf("Solving...\n");
	mxSc = -1;
	for(int i = 0; i < 6; i++) used[i] = 0;
	used[0] = 1;
	dfs(0);
	if(mxSc > 0) printf("%d\n",mxSc);
	else printf("none\n");
}


int main(int argc, char **argv){
	while(1){
		for(int i = 0; i < 6; i++){
			for(int j = 0; j < 3; j++){
				scanf("%d",&tri[i][j]);
			}
		}
		char c;
		scanf("\n%c", &c);
		//printf("c=%c\n", c);
		solve();
		if(c == '$') break;
	}
	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