版本比较
4.24
正文
增加 8 行删除 8 行
33 行未改动34353637383940414298 行未改动1411421431441451461471481497 行未改动15715815916016116216316416516616716816917017117217317417517617717817933 行未改动{cin >> t >> x ;while(t --){cin >> n ;printf("%.2lf\n" , f(n)) ;}return 0 ;}98 行未改动}int main(){cin >> t >> p ;while(t--){cin >> w >> h ;int ans = dfs(w , h) ;if(ans == 3) cout << "error\n" ;7 行未改动1.我们用三维状态来表示:当前时间,当前位置,当前移动次数,然后我们有两种可能:移动(能够的情况下),不移动,移动的情况下我们就$f(i,j,k)\to f(i+1,3-j,k+1)+是否到达$,不移动就是$f(i,j,k)\to f(i+1,j,k)+是否到达$```cpp line-numbers lines=9#include <bits/stdc++.h>using namespace std ;int t , w , a[1005] , f[1005][10][35] ;int dfs(int ti , int pos , int cnt){if(ti > t) return 0 ; //已经接完了if(f[ti][pos][cnt] != -1) return f[ti][pos][cnt] ; //走过int tmp1 = 0 , tmp2 = 0 ;if(cnt < w) tmp1 = dfs(ti + 1 , 3 - pos , cnt + 1) + (3 - pos == a[ti]); //注意,不一定要接到苹果!tmp2 = dfs(ti + 1 , pos , cnt) + (pos == a[ti]);return f[ti][pos][cnt] = max(tmp1 , tmp2) ;}int main(){cin >> t >> w;for(int i = 1 ; i <= t ; i ++) cin >> a[i] ;memset(f , -1 , sizeof(f)) ;cout << dfs(1 , 1 , 0) ;return 0 ;}```33 行未改动34353637383940414298 行未改动1411421431441451461471481497 行未改动15715815916016116216316416516616716816917017117217317417517617717817933 行未改动{cin >> t >> x ;while(t --){cin >> n ;printf("%.2lf\n" , f(n)) ;}return 0 ;}98 行未改动}int main(){cin >> t >> p ;while(t--){cin >> w >> h ;int ans = dfs(w , h) ;if(ans == 3) cout << "error\n" ;7 行未改动1.我们用三维状态来表示:当前时间,当前位置,当前移动次数,然后我们有两种可能:移动(能够的情况下),不移动,移动的情况下我们就$f(i,j,k)\to f(i+1,3-j,k+1)+是否到达$,不移动就是$f(i,j,k)\to f(i+1,j,k)+是否到达$```cpp line-numbers lines=9#include <bits/stdc++.h>using namespace std ;int t , w , a[1005] , f[1005][10][35] ;int dfs(int ti , int pos , int cnt){if(ti > t) return 0 ; //已经接完了if(f[ti][pos][cnt] != -1) return f[ti][pos][cnt] ; //走过int tmp1 = 0 , tmp2 = 0 ;if(cnt < w) tmp1 = dfs(ti + 1 , 3 - pos , cnt + 1) + (3 - pos == a[ti]); //注意,不一定要接到苹果!tmp2 = dfs(ti + 1 , pos , cnt) + (pos == a[ti]);return f[ti][pos][cnt] = max(tmp1 , tmp2) ;}int main(){cin >> t >> w;for(int i = 1 ; i <= t ; i ++) cin >> a[i] ;memset(f , -1 , sizeof(f)) ;cout << dfs(1 , 1 , 0) ;return 0 ;}```