标签:return cin col bit strong end out clu space
思路:
dp,使用了bitset优化。
实现:
1 #include <bits/stdc++.h> 2 using namespace std; 3 const int N = 81; 4 const int INF = 0x3f3f3f3f; 5 int a[N][N], b[N][N]; 6 bitset<N * N * 4> dp[N][N]; 7 8 int main() 9 { 10 int h, w; 11 while (cin >> h >> w) 12 { 13 for (int i = 0; i < h; i++) 14 { 15 for (int j = 0; j < w; j++) 16 { 17 cin >> a[i][j]; 18 } 19 } 20 for (int i = 0; i < h; i++) 21 { 22 for (int j = 0; j < w; j++) 23 { 24 cin >> b[i][j]; 25 } 26 } 27 for (int i = 0; i < h; i++) 28 { 29 for (int j = 0; j < w; j++) 30 { 31 dp[i][j].reset(); 32 } 33 } 34 dp[0][0][a[0][0] - b[0][0] + N * N * 2] = 1; 35 dp[0][0][b[0][0] - a[0][0] + N * N * 2] = 1; 36 for (int i = 0; i < h; i++) 37 { 38 for (int j = 0; j < w; j++) 39 { 40 int tmp = abs(a[i][j] - b[i][j]); 41 if (j) 42 { 43 dp[i][j] |= dp[i][j - 1] << tmp; 44 dp[i][j] |= dp[i][j - 1] >> tmp; 45 } 46 if (i) 47 { 48 dp[i][j] |= dp[i - 1][j] << tmp; 49 dp[i][j] |= dp[i - 1][j] >> tmp; 50 } 51 } 52 } 53 int res = INF; 54 for (int i = 0; i < N * N * 4; i++) 55 { 56 if (dp[h - 1][w - 1][i]) res = min(res, abs(i - N * N * 2)); 57 } 58 cout << res << endl; 59 } 60 return 0; 61 }
AtCoder Beginner Contest 147 E. Balanced Path
标签:return cin col bit strong end out clu space
原文地址:https://www.cnblogs.com/wangyiming/p/12055814.html