icpc2021 昆明 K (dfs爆搜)

#include<bits/stdc++.h>
using namespace std;

const string ma[500] = {
	"1w","2w","3w","4w","5w","6w","7w","8w","9w",
	"1b","2b","3b","4b","5b","6b","7b","8b","9b",
	"1s","2s","3s","4s","5s","6s","7s","8s","9s",
	"1z","2z","3z","4z","5z","6z","7z"
};

int maj[100][300];

int T;

void get_index(char num, char suit){
	int c, d;
	if(suit == 'w'){
		c = 0;
	}
	if(suit == 'b'){
		c = 1;
	}
	if(suit == 's'){
		c = 2;
	}
	if(suit == 'z'){
		c = 3;
	}
	d = num - '0' - 1;
	++maj[c][d];
}

bool hu;

vector<string> ans;
int cnt = 0;

void dfs(int d, int rem){
	if(hu) return;
	if(rem < 0) return;
	if(rem == 0){
		hu = true;
		return;
	}
	
	if(maj[d/9][d%9] == 0) {
		dfs(d + 1, rem);
	}
	
	if(maj[d/9][d%9] >= 3){
		maj[d/9][d%9] -= 3;
		dfs(d, rem - 3);
		maj[d/9][d%9] += 3;
	}
	
	if(rem % 3 == 2 && maj[d/9][d%9] >= 2){
		maj[d/9][d%9] -= 2;
		dfs(d, rem - 2);
		maj[d/9][d%9] += 2;
	}
	
	if(d <= 26 && d%9 <= 6 && maj[d/9][d%9] >= 1 && maj[d/9][d%9+1] >= 1 && maj[d/9][d%9+2] >= 1){
		maj[d/9][d%9]--, maj[d/9][d%9+1]--, maj[d/9][d%9+2]--;
		dfs(d, rem - 3);
		maj[d/9][d%9]++, maj[d/9][d%9+1]++, maj[d/9][d%9+2]++;
	}
	return;
}

//void dfs(int left){
//	if(hu) return;
////	printf("%d
", left);
//	
//	if(left == 3){
//		
//		for(int i = 0 ; i < 34 ; ++i){
//			int x = i / 9, y = i % 9;
//			// kezi
//			if(maj[x][y] >= 3){
//				hu = true;
//				return;
//			}
//			// shunzi
//			if(x <= 2 && y <= 6 && maj[x][y] >= 1 && maj[x][y+1] >= 1 && maj[x][y+2] >= 1){
//				hu = true;
//				return;
//			}			
//		}
//		
//		return;
//	}
//	
//	for(int i = 0 ; i < 34 ; ++i){
//		int x = i / 9, y = i % 9;
////		if(left == 14){ // quetou
////			if(maj[x][y] >= 2){
////				maj[x][y] -= 2;
////				dfs(left - 2);
////				maj[x][y] += 2;
////			}
////		} else{
//			// kezi
//			if(maj[x][y] >= 3){
//				maj[x][y] -= 3;
//				dfs(left - 3);
//				maj[x][y] += 3;
//				if(hu) return;
//			}
//			// shunzi
//			if(x <= 2 && y <= 6 && maj[x][y] >= 1 && maj[x][y+1] >= 1 && maj[x][y+2] >= 1){
//				maj[x][y]--;
//				maj[x][y+1]--;
//				maj[x][y+2]--;
//				dfs(left - 3);
//				maj[x][y]++;
//				maj[x][y+1]++;
//				maj[x][y+2]++;
//				if(hu) return; 
//			}
//		}
//		return;
////	}
//}
//
//void check(){
//	for(int i = 0 ; i < 34 ; ++i){
//		if(maj[i/9][i%9] >= 2){
//			maj[i/9][i%9] -= 2;
//			dfs(0, 12);
//			maj[i/9][i%9] += 2;
//			if(hu) return;
//		}
//	}
//}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    
	cin >> T;
	while(T--){
		memset(maj, 0, sizeof(maj));
		hu = false;
		ans.clear();
		
//		printf("zhxing
");
		string s;
		cin >> s;
			
		for(int i = 0 ; i < 28 ; i += 2){
			char suit, num;
			num = s[i], suit = s[i + 1];
			get_index(num, suit);			
		}

//		for(int i = 0 ; i < 34 ; ++i){
//			printf("%d ", maj[i/9][i%9]);
//		} printf("
");
		
		//check();
		dfs(0, 14);
		if(hu){
			cout << "Tsumo!" << endl;
			continue;
		}
		
		for(int i = 0 ; i < 34 ; ++i){ // discard which 
			string cur = "";
			
			if(!maj[i/9][i%9]) continue;
			
			--maj[i/9][i%9];
			cur += ma[i];
			cur += " ";
			
			bool flag = false;
			for(int j = 0 ; j < 34 ; ++j){
				hu = false;
				if(i == j) continue;
				++maj[j/9][j%9];
				
				dfs(0, 14);
				//check();
				if(hu){
					cur += ma[j];
					flag = true;
				}
				--maj[j/9][j%9];
			}
			++maj[i/9][i%9];
			
			if(flag) ans.push_back(cur);
		} 
		
//		sort(ans.begin(), ans.end());
		cout << ans.size() << endl;
//		printf("%d
", ans.size());
		for(int i = 0 ; i < ans.size() ; ++i){
			cout << ans[i] << endl;
		}
	}
	return 0;
}
原文地址:https://www.cnblogs.com/tuchen/p/14837840.html