[P1736]創意吃魚法[DP]
阿新 • • 發佈:2018-03-03
spa space nbsp cout 連續 ble ems dig clas
開始沒看到要求對角線以外的地方不能是0,以為是個xx題。。。照著題解思路寫的,很妙啊
題意:給定01矩陣
求矩陣中最長的只有對角線是1的正方形的對角線長度
x[i][j]從(i,j)向左/右(不包括(i,j))的連續的0的數量
y[i][j]從(i,j)向上(不包括(i,j))的連續的0的數量
1 #include <bits/stdc++.h> 2 using namespace std; 3 typedef long long LL; 4 const int eps = 1e-8; 5 typedef long double LD; 6 typedef pair<intView Code, int> pii; 7 #define lop(i,a,b) for(register int i = (a); i <= (b); ++i) 8 #define dlop(i,a,b) for(register int i = (a); i >= (b); --i) 9 #define bl(i) for(int i = head[u]; i; i = G[i].next) 10 //char buf[50<<20], *p1=buf; 11 //#define getchar() (*p1++) 12 #define make_pair MP 13 inline intread(){ 14 register int c = getchar(), x = 0, f = 1; 15 while(!isdigit(c)){if (c == ‘-‘) f = -1; c = getchar();} 16 while(isdigit(c)) x = x*10+(c&15), c = getchar(); 17 return x * f; 18 } 19 int n, m, s, dp[2503][2505], x[2503][2505], y[2503][2505]; 20 bool matrix[2503][2505]; 21 int main(void){ 22 //freopen("data.in", "r", stdin); 23 //fread(buf,1,50<<20,stdin); 24 int ans = 0; 25 n = read(), m = read(); 26 lop(i,1,n) lop(j,1,m) { 27 matrix[i][j] = read(); 28 if (matrix[i][j]) dp[i][j] = min(dp[i-1][j-1], min(x[i][j-1], y[i-1][j]))+1, ans = max(ans, dp[i][j]); 29 else x[i][j] = x[i][j-1] + 1, y[i][j] = y[i-1][j] + 1; 30 } 31 memset(x,0,sizeof(x)); 32 memset(dp,0,sizeof(dp)); 33 lop(i,1,n) dlop(j,m,1) { 34 if (matrix[i][j]) dp[i][j] = min(dp[i-1][j+1], min(x[i][j+1], y[i-1][j]))+1, ans = max(ans, dp[i][j]); 35 else x[i][j] = x[i][j+1] + 1; 36 } 37 cout << ans; 38 return 0; 39 }
[P1736]創意吃魚法[DP]