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

贴个17重循环代妈,32ms

Posted by KatrineYang at 2016-08-28 12:51:00 on Problem 1224
#include <iostream>
#include <stdio.h>
using namespace std;

char piece[10][4][4];

bool con(int x1, int d1, int x2, int d2){
	return piece[x1][d1][0] == piece[x2][d2][0] && piece[x1][d1][1] != piece[x2][d2][1];
}

int main() {
	int num;
	while(1){
		scanf("%d", &num);
		if(num == 0) break;
		printf("%d:\n", num);
		bool used[10] = {0};
		for(int i = 1; i <= 9; i++){
			int fei;
			scanf("%d", &fei);
			for(int j = 0; j <= 3; j++) scanf("%s", piece[i][j]);
		}
		int bh[9];
		int dir[9];
		bool ky = 0;
		for(int i = 1; i <= 9; i++){
			used[i] = 1, bh[4] = i, dir[4] = 0;
			for(int j = 1; j <= 9; j++){

				if(used[j]) continue;
				for(int jdir = 0; jdir < 4; jdir++){
					if(con(j, (1+jdir)%4, bh[4], 3)){
						used[j] = 1, bh[3] = j, dir[3] = jdir;
						for(int k = 1; k <= 9; k++){
							if(used[k]) continue;
							for(int kdir = 0; kdir < 4; kdir++){
								if(con(k, (2+kdir)%4, bh[4], 0)){
									used[k] = 1, bh[1] = k, dir[1] = kdir;
									for(int l = 1; l <= 9; l++){
										if(used[l]) continue;
										for(int ldir = 0; ldir < 4; ldir++){
											if(con(l, (3+ldir)%4, bh[4], 1)){
												used[l] = 1, bh[5] = l, dir[5] = ldir;
												for(int m = 1; m <= 9; m++){
													if(used[m]) continue;
													for(int mdir = 0; mdir < 4; mdir++){
														if(con(m, mdir, bh[4], 2)){
															used[m] = 1, bh[7] = m, dir[7] = mdir;
															int unused[4];
															int cnt = 0;
															for(int z = 1; z <= 9; z++){
																if(!used[z]){
																	unused[cnt] = z;
																	cnt++;
																}
															}
															for(int P = 0; P < 4; P++){
																int p = unused[P];
																for(int pdir = 0; pdir < 4; pdir++){
																	if(!con(p, (1+pdir)%4, bh[1], (3+dir[1])%4)) continue;
																	if(!con(p, (2+pdir)%4, bh[3], dir[3])) continue;
																	used[p] = 1, bh[0] = p, dir[0] = pdir;
																	for(int Q = 0; Q < 4; Q++){
																		int q = unused[Q];
																		if(used[q]) continue;
																		for(int qdir = 0; qdir < 4; qdir++){
																			if(!con(q, qdir, bh[3], (2+dir[3])%4)) continue;
																			if(!con(q, (1+qdir)%4, bh[7], (3+dir[7])%4)) continue;
																			used[q] = 1, bh[6] = q, dir[6] = qdir;
																			for(int R = 0; R < 4; R++){
																				int r = unused[R];
																				if(used[r]) continue;
																				for(int rdir = 0; rdir < 4; rdir++){
																					if(!con(r, (3+rdir)%4, bh[7], (1+dir[7])%4)) continue;
																					if(!con(r, rdir, bh[5], (2+dir[5])%4)) continue;
																					used[r] = 1, bh[8] = r, dir[8] = rdir;
																					int s;
																					for(int y = 0; y < 4; y++){
																						if(!used[unused[y]]){
																							s = unused[y];
																							break;
																						}
																					}
																					for(int sdir = 0; sdir < 4; sdir++){
																						if(!con(s, (2+sdir)%4, bh[5], dir[5])) continue;
																						if(!con(s, (3+sdir)%4, bh[1], (1+dir[1])%4)) continue;
																						used[s] = 1, bh[2] = s, dir[2] = sdir;
																						ky = 1;
																						goto done;
																					}
																					used[r] = 0;
																				}
																			}
																			used[q] = 0;
																		}
																	}
																	used[p] = 0;
																}
															}
															used[m] = 0;
														}
													}
												}
												used[l] = 0;
											}
										}
									}
									used[k] = 0;
								}
							}
						}
						used[j] = 0;
					}
				}
			}

			used[i] = 0;
		}
		done:
		if(!ky){
			printf("No Solution\n\n");
			continue;
		}
		//cout << 1 << endl;
		for(int i = 0; i < 3; i++){
			for(int j = 0; j < 3; j++){
				int tar = 3*i+j;
				printf("   %c%c   ", piece[bh[tar]][(0+dir[tar])%4][0], piece[bh[tar]][(0+dir[tar])%4][1]);
			}
			printf("\n");
			for(int j = 0; j < 3; j++){
				int tar = 3*i+j;
				printf("%c%c %d %c%c ", piece[bh[tar]][(3+dir[tar])%4][0], piece[bh[tar]][(3+dir[tar])%4][1], bh[tar], piece[bh[tar]][(1+dir[tar])%4][0], piece[bh[tar]][(1+dir[tar])%4][1]);
			}
			printf("\n");
			for(int j = 0; j < 3; j++){
				int tar = 3*i+j;
				printf("   %c%c   ", piece[bh[tar]][(2+dir[tar])%4][0], piece[bh[tar]][(2+dir[tar])%4][1]);
			}
			printf("\n\n");
		}
	}
	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