hdoj 1015 暴力dfs

 
 
//我自己sb 在dfs回去的时候少加了几个return 导致tle 就有点气 
// 题目就不列了 就是暴力dfs 很easy 作为一个新手也可以自己码出来

#include<iostream>
#include<string>
#include<cmath>
#include<cstring>
#include<algorithm>
using namespace std;
double n;
int sz;
double sum;
int word[13];
bool mark[13];
int ans[6];
bool flag;
bool cmp(int a, int b) {
	return a > b;
}
void dfs(int k) {
	if (!flag) return;
	if (k > 5) {
		//cout << sum << " ";
		sum = ans[1] - ans[2] * ans[2] + ans[3] * ans[3] * ans[3] - ans[4] * ans[4] * ans[4] * ans[4] + ans[5] * ans[5] * ans[5] * ans[5] * ans[5];
		if (sum == n) {
			for (int i = 1; i <= 5; i++) {
			//	cout << ans[i] << endl;
				char x = ans[i] + 'A' -1;
				cout << x;
			}
			cout << endl;
			flag = 0;
		}
		return;
	}
	for (int i = 1; i <= sz; i++) {
		if (!mark[i]) {
			mark[i] = 1;
			ans[k] = word[i];
			dfs(k + 1);
			mark[i] = 0;
		}
	}
}
int main()
{
	string s;
	while (cin >> n) {
		flag = 1;
		cin >> s;
		memset(mark, 0, sizeof(mark));
		memset(ans, 0, sizeof(ans));
		memset(word, 0, sizeof(word));
		if (s == "END") break;
		sz = s.size();
		for (int i = 0; i < sz; i++) {
			word[i + 1] = s[i] - 'A' + 1;
		}
		sort(word+1, word+sz+1,cmp);
	//	for (int i = 1; i <= sz; i++)
	//		cout << word[i] << " ";
		dfs(1);
		if (flag) cout << "no solution" << endl;
	}
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值