Question: Find the longest path from any walking point. If there is a turn,
It must be 90 degrees or no turns!
Train of Thought: first, use DFS to obtain the longest length of all the lines in the eight directions starting from the point!
Step [I] [J] [k] indicates the maximum number of steps (I, j) that walk along the K direction to the head or turn!
Finally, enumerate each path that can take a certain turn to 90 degrees, and find the longest length!
Step [I] [J] [k1] + step [I] [J] [k2] is (I, j) The point K1 and K2 constitute 90 degrees!
1 # include <iostream> 2 # include <cstdio> 3 # include <cstring> 4 # include <algorithm> 5 # define N 105 6 using namespace STD; 7 8 int step [N] [N] [8]; 9 int dir [8] [2] = {0, 1}, {1, 0 }, {-1, 0}, {0,-1}, {-1,-1}, {1, 1}, {-1, 1}, {1, -1 }}; 10 int index [8] [2] = {0, 1}, {1, 3}, {2, 3}, {0, 2 }, {5, 6}, {5, 7}, {4, 7}, {4, 6 }}; // enumeration of turns corresponding to each node 11 char MP [N] [N], vis [N] [N]; 12 int n; 13 14 bool judge (int x, I NT y) {15 if (x <1 | Y <1 | x> N | Y> N) 16 return false; 17 if (MP [x] [Y] = '#') return false; 18 19 Return true; 20} 21 22 void DFS (int x, int y) {23 for (INT I = 0; I <8; ++ I) {24 int xx = x + dir [I] [1]; 25 int YY = Y + dir [I] [0]; 26 if (! Judge (XX, YY) 27 step [x] [y] [I] = 1; 28 else {29 If (! Step [XX] [YY] [I]) // catch up with the memory 30 DFS (XX, YY ); 31 step [x] [y] [I] = 1 + step [XX] [YY] [I]; 32} 33} 34} 35 36 int main () {37 while (scanf ("% d", & N) {38 memset (step, 0, sizeof (STEP); 39 memset (VIS, 0, sizeof (VIS); 40 for (INT I = 1; I <= N; ++ I) 41 scanf ("% s", MP [I] + 1 ); 42 43 for (INT I = 1; I <= N; ++ I) 44 for (Int J = 1; j <= N; ++ J) 45 if (MP [I] [J] = '. ') 46 DFS (I, j); 47 48 int maxn =-1; 49 for (INT I = 1; I <= N; ++ I) 50 for (Int J = 1; j <= N; ++ J) {51 if (MP [I] [J] = '. ') 52 for (int K = 0; k <8; ++ K) 53 maxn = max (maxn, step [I] [J] [index [k] [0] + step [I] [J] [index [k] [1]); 54} 55 printf ("% d \ n", maxn-1); // an inflection point is added! 56} 57 Return 0; 58}View code
2014 select 5024 Wang Xifeng's little plot