Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
树状dp 5548K 157ms 这效率还可以啊!#include <iostream> #include <stdio.h> #include <algorithm> using namespace std; int m,n; bool used[1022][1022]; bool kong[1022][1022]; int rootX, rootY; int mx; inline int MX(int a, int b){ return (a>b) ? a : b; } int getDepthMax(int x, int y){ used[x][y] = 1; int depth[4]; int cnt = 0; int retVal = 0; if(kong[x-1][y] && !used[x-1][y]){ depth[cnt] = getDepthMax(x-1,y); retVal = MX(retVal, depth[cnt]+1); cnt++; } if(kong[x+1][y] && !used[x+1][y]){ depth[cnt] = getDepthMax(x+1,y); retVal = MX(retVal, depth[cnt]+1); cnt++; } if(kong[x][y-1] && !used[x][y-1]){ depth[cnt] = getDepthMax(x,y-1); retVal = MX(retVal, depth[cnt]+1); cnt++; } if(kong[x][y+1] && !used[x][y+1]){ depth[cnt] = getDepthMax(x,y+1); retVal = MX(retVal, depth[cnt]+1); cnt++; } if(cnt == 0){;} else if(cnt == 1){ mx = MX(mx, retVal); } else if(cnt == 2){ mx = MX(mx, depth[0]+depth[1]+2); } else{ sort(depth, depth+cnt); mx = MX(mx, depth[cnt-1] + depth[cnt-2] + 2); } return retVal; } int main(int argc, char **argv) { int t; scanf("%d", &t); for(int ii = 0; ii < t; ii++){ bool getRoot = 0; mx = 0; scanf("%d%d", &n, &m); char str[1022]; for(int i = 1; i <= m; i++){ scanf("%s", str); for(int j = 1; j <= n; j++){ if(str[j-1]=='.') { kong[i][j] = 1; if(!getRoot){ rootX = i, rootY = j; getRoot = 1; } } else kong[i][j] = 0; } } for(int j = 0; j <= n+1; j++) { kong[0][j] = 0, kong[m+1][j] = 0; } for(int i = 0; i <= m+1; i++){ kong[i][0] = 0, kong[i][n+1] = 0; } for(int i = 0; i < m; i++){ for(int j = 0; j < n; j++){ used[i][j] = 0; } } getDepthMax(rootX, rootY); printf("Maximum rope length is %d.\n", mx); } return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator