题意:还是寻找子数组和最大,只是上下左右可以联通了,NxN,变成2Nx2N就行了,循环了四层,D_Double用了三层,有个大佬用dp是俩层
#include <string> #include<iostream> #include<map> #include<memory.h> #include<vector> #include<algorithm> #include<queue> #include<vector> #include<stack> #include<math.h> #include<iomanip> #include<bitset> #include"math.h" namespace cc { using std::cout; using std::endl; using std::cin; using std::map; using std::vector; using std::string; using std::sort; using std::priority_queue; using std::greater; using std::vector; using std::swap; using std::stack; using std::queue; using std::bitset; constexpr int N = 80; int a[N * 2][N * 2]; void solve() { int n; cin >> n; while (n--) { int R; cin >> R; memset(a, 0, sizeof(a)); for (int i = 1;i <= R;i++) { for (int j = 1;j <= R;j++) { cin >> a[i][j]; a[i][R + j] = a[i][j]; a[R + i][j] = a[i][j]; a[R + i][R + j] = a[i][R + j]; } } for (int i = 1;i <= 2*R;i++) { for (int j = 1;j <= 2*R;j++) { a[i][j] = a[i][j] + a[i - 1][j]; } } int max = INT32_MIN; for (int i = 0;i <= R;i++) { for (int j = i + 1;j <= i + R;j++) { for (int k = 1;k <= R;k++) { int sum = 0; for (int c = k;c < k + R;c++) { if (sum < 0) sum = 0; else sum = sum + a[j][c] - a[i][c]; if (sum > max) { max = sum; } } } } } cout << max << endl; } } }; int main() { #ifndef ONLINE_JUDGE freopen("d://1.text", "r", stdin); #endif // !ONLINE_JUDGE cc::solve(); return 0; }