c++面向对象狼吃羊
时间: 2023-11-20 07:58:58 浏览: 53
以下是C++面向对象实现红太狼吃羊的代码:
```cpp
#include <iostream>
#include <vector>
using namespace std;
class Wolf {
public:
Wolf(int n, int m) : n(n), m(m) {}
void eatSheep() {
vector<int> path;
dfs(path, 1, 0);
}
private:
int n, m;
void dfs(vector<int>& path, int cur, int cnt) {
if (cnt == m) {
for (int i = 0; i < path.size(); i++) {
cout << path[i] << " ";
}
cout << endl;
return;
}
if (cur > n) {
return;
}
path.push_back(cur);
dfs(path, cur + 1, cnt + 1);
path.pop_back();
dfs(path, cur + 1, cnt);
}
};
int main() {
int n, m;
cin >> n >> m;
Wolf wolf(n, m);
wolf.eatSheep();
return 0;
}
```