#include <iostream> #include <vector> using namespace std; void countof(int i, int hotel, int poi, int tnt, int& count, vector<string>& res) { if (hotel == 0 && poi == 0 && tnt == 0) { count++; return; } if (i < 2) { if (poi > 0) { res.push_back("poi"); countof(i + 1, hotel, poi - 1, tnt, count, res); res.pop_back(); } if (tnt > 0) { res.push_back("tnt"); countof(i + 1, hotel, poi, tnt - 1, count, res); res.pop_back(); } } if (i > 1 && i < 4) { if (res[i - 1] != "poi"||res[i - 2] != "poi") { if (poi > 0) { res.push_back("poi"); countof(i + 1, hotel, poi - 1, tnt, count, res); res.pop_back(); } } if (res[i - 1] != "tnt" || res[i - 2] != "tnt") { if (tnt > 0) { res.push_back("tnt"); countof(i + 1, hotel, poi, tnt - 1, count, res); res.pop_back(); } } } if (i > 3 ) { if (poi > 0 && (res[i - 1] != "poi" || res[i - 2] != "poi")) { res.push_back("poi"); countof(i + 1, hotel, poi - 1, tnt, count, res); res.pop_back(); } if (tnt > 0 && (res[i - 1] != "tnt" || res[i - 2] != "tnt")) { res.push_back("tnt"); countof(i + 1, hotel, poi, tnt - 1, count, res); res.pop_back(); } if (hotel > 0 && (res[i - 1] != "hotel" || res[i - 2] != "hotel")) { res.push_back("hotel"); countof(i + 1, hotel - 1, poi, tnt, count, res); res.pop_back(); } } } int main() { int hotel, poi, tnt; cin >> hotel >> poi >> tnt; if (poi+tnt<4) { cout << 0 << endl; return 0; } vector<string> vec; int count = 0; countof(0, hotel, poi, tnt, count, vec); cout << count << endl; }
a了,但是很笨重的代码,有人有更好的方法吗?
总觉得是道数学题。
全部评论
(0) 回帖