Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <cstdlib>
- #include <deque>
- using namespace std;
- static string frogway(int n, int k) {
- int* field = new int[n + 1];
- for (int i = 0; i < n + 1; i++)
- field[i] = 0;
- for (int i = 2; i < n; i++) {
- field[i] = rand() % 2;
- }
- for (int i = 1; i <= n; i++) {
- cout << field[i] << ' ';
- }
- cout << '\n';
- string ans;
- int count = 0;
- int i = 0;
- while(i<n+1) {
- for (int j = k; j >= 1; j--)
- {
- if (field[i + j] == 0) {
- ans += to_string(i)+' ';
- i += j;
- break;
- }
- if (j == 1) {
- ans += to_string(i) + ' ';
- i += k;
- }
- count++;
- }
- }
- cout << "count!11: " << count << "\n";
- return ans;
- }
- static string dpfrog(int n, int k) {
- int* dp = new int[n + 1];
- int* frog = new int[n + 1];
- for (int i = 0; i < n + 1; i++)
- dp[i] = frog[i] = 0;
- for (int i = 2; i < n; i++) {
- dp[i] = rand()%2;
- }
- for (int i = 1; i <= n; i++) {
- cout << dp[i] << ' ';
- }
- cout << '\n';
- int count=0;
- for (int i = k + 1; i <= n; i++) {
- int nmin = 1000;
- int mini = 1000;
- for (int j = 1; j <= k; j++) {
- if (nmin > dp[i - j]) {
- nmin = dp[i - j];
- mini = i - j;
- }
- count++;
- }
- frog[i] = mini;
- dp[i] += nmin;
- }
- cout << "count222: " << count << '\n';
- int cnt = 0;
- int num = n;
- deque<int> jumps;
- jumps.push_back(num);
- while (num > 1) {
- num = frog[num];
- jumps.push_front(num);
- cnt++;
- }
- string ans = dp[n] + "\n" + cnt + '\n';
- while (!jumps.empty()) {
- cout << jumps.back() << " ";
- jumps.pop_back();
- }
- return ans;
- }
- int main()
- {
- srand(time(NULL));
- cout << frogway(20, 3);
- cout << dpfrog(20, 3);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement