滴滴
选择 30,编程 2
Index
排列小球
思路
DFS(会超时)
多维 DP
Ways to arrange Balls such that adjacent balls are of different types - GeeksforGeeks
C++(67%,TLE)
#include <iostream>
#include <vector>
using namespace std;
int bs[3];
int n;
int ans;
vector<int> tmp;
void dfs(int step) {
if (tmp.size() == n) {
ans += 1;
return;
}
for (int i = 0; i < 3; i++) {
if (bs[i] > 0 && i != tmp.back()) {
tmp.push_back(i);
bs[i] -= 1;
dfs(step + 1);
bs[i] += 1;
tmp.pop_back();
}
}
}
void solve() {
cin >> bs[0] >> bs[1] >> bs[2];
n = bs[0] + bs[1] + bs[2];
ans = 0;
for (int i = 0; i < 3; i++) {
if (bs[i] > 0) {
tmp.push_back(i);
bs[i] -= 1;
dfs(1);
bs[i] += 1;
tmp.pop_back();
}
}
cout << ans;
}
int main() {
solve();
//cout << endl;
//system("PAUSE");
return 0;
}多维 DP(未测试)
Ways to arrange Balls such that adjacent balls are of different types - GeeksforGeeks
交通轨迹分析
思路
并查集/暴力枚举
判断相交
```python
class Point(object):
暴力(未测试)
最后更新于
这有帮助吗?