#includeusing namespace std;
int N;int mat[100][100];
int countmax(int m, int n){if (m==N || n==N)return 0;elsereturn mat[m][n] + max(countmax(m+1, n), countmax(m, n+1));}
int main(){ios_base::sync_with_stdio(false);cin.tie(NULL);while (cin >> N){for (int i=0; i<N; i++){for (int j=0; j<N; j++){cin >> mat[i][j];}}cout << countmax(0, 0) ;}}
Python是SE
from sys import stdinfrom itertools import islicedef countmax(m, n):if m==N or n==N:return 0else:return li[m][n] + max(countmax(m+1, n), countmax(m, n+1))
for N in map(int, stdin):li = [list(map(int, line.split())) for line in islice(stdin, N)]print(countmax(0, 0))
你這樣當然TLE
你有想過當n=100時countmax(0,0)被call了多少次嗎