题目链接:https://www.luogu.org/problem/P1141
这题目就尼玛的傻逼题 (如果出现a-b-c的路可以走,那么从b出发可达到的最多的地点和从a出发是一样的!)
思路:
根据题目的要求,我把此时出发的点一直到最后走过最多点时的终点全部记录下来,那么无论如何从这些点出发走过的最多点点个数都是一样的,那么就可以搞成记忆化搜索了
1 #include <math.h> 2 #include <stdio.h> 3 #include <stdlib.h> 4 #include <iostream> 5 #include <algorithm> 6 #include <string> 7 #include <string.h> 8 #include <vector> 9 #include <map> 10 #include <stack> 11 #include <set> 12 #include <queue> 13 14 15 #define LL long long 16 #define INF 0x3f3f3f3f 17 #define ls nod<<1 18 #define rs (nod<<1)+1 19 const int maxn = 4e5+10; 20 const double eps = 1e-9; 21 22 int n,m,cnt; 23 int a[1010][1010],res[1010][1010]; 24 int dik[1000010][2]; 25 int dir[4][2] = {{-1,0},{1,0},{0,-1},{0,1}}; 26 27 void dfs(int row,int col) { 28 cnt++; 29 dik[cnt][0] = row; 30 dik[cnt][1] = col; 31 res[row][col] = 1; 32 for (int i=0;i<4;i++) { 33 int new_row = row + dir[i][0]; 34 int new_col = col + dir[i][1]; 35 if (new_row<1 || new_col<1 || new_row>n || new_col>n || res[new_row][new_col]) { 36 continue; 37 } 38 if (a[new_row][new_col] == a[row][col]) { 39 continue; 40 } 41 dfs(new_row,new_col); 42 } 43 } 44 45 int main() { 46 scanf("%d%d",&n,&m); 47 for (int i=1;i<=n;i++) { 48 for (int j=1;j<=n;j++) { 49 scanf("%1d",&a[i][j]); 50 } 51 } 52 for (int i=1;i<=m;i++) { 53 cnt = 0; 54 int row,col; 55 scanf("%d%d",&row,&col); 56 if (res[row][col] > 0) { 57 printf("%d\n",res[row][col]); 58 continue; 59 } 60 dfs(row,col); 61 for (int j=1;j<=cnt;j++) { 62 res[dik[j][0]][dik[j][1]] = cnt; 63 } 64 printf("%d\n",cnt); 65 } 66 return 0; 67 }