1287. Mars Canals(DP)
水DP
1 #include <iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 #include<stdlib.h> 6 #include<vector> 7 using namespace std; 8 #define N 1402 9 int dp[N][N][4]; 10 char s[N][N]; 11 int main() 12 { 13 int i,j,n; 14 scanf("%d",&n); 15 for(i = 0 ; i < n ; i++) 16 cin>>s[i]; 17 dp[0][0][0] = 1; 18 dp[0][n-1][3] = 1; 19 for(i = 0 ; i < n ; i++) 20 { 21 dp[0][i][2] = 1; 22 dp[i][0][1] = 1; 23 } 24 for(i = 0 ; i < n ;i++) 25 for(j = 0 ; j < n ; j++) 26 { 27 if(i!=0&&j!=0&&s[i][j]==s[i-1][j-1]) 28 { 29 dp[i][j][0] = dp[i-1][j-1][0]+1; 30 } 31 else 32 dp[i][j][0] = 1; 33 if(i!=0&&j!=n-1&&s[i][j]==s[i-1][j+1]) 34 { 35 dp[i][j][3] = dp[i-1][j+1][3]+1; 36 } 37 else 38 dp[i][j][3] = 1; 39 if(j!=0&&s[i][j]==s[i][j-1]) 40 { 41 dp[i][j][1] = dp[i][j-1][1]+1; 42 } 43 else 44 dp[i][j][1] = 1; 45 if(i!=0&&s[i][j]==s[i-1][j]) 46 { 47 dp[i][j][2] = dp[i-1][j][2]+1; 48 } 49 else 50 dp[i][j][2] = 1; 51 } 52 int maxz1=0,maxz2=0; 53 for(i = 0 ; i < n ; i++) 54 for(j = 0 ; j < n ; j++) 55 { 56 int kk = max(max(dp[i][j][3],dp[i][j][0]),max(dp[i][j][1],dp[i][j][2])); 57 if(s[i][j]=='S') 58 { 59 if(kk>maxz1) 60 { 61 maxz1 = kk; 62 } 63 } 64 else if(kk>maxz2) 65 maxz2 = kk; 66 } 67 if(maxz1>maxz2) 68 { 69 printf("S\n"); 70 printf("%d\n",maxz1); 71 } 72 else if(maxz1<maxz2) 73 { 74 printf("s\n"); 75 printf("%d\n",maxz2); 76 } 77 else 78 { 79 printf("?\n"); 80 printf("%d\n",maxz1); 81 } 82 return 0; 83 }