俄罗斯套娃

记忆化搜索,类似于POJ 1088#include <stdlib.h> #include <stdio.h> #include <string.h> #include <time.h> #include <vector> #include <algorithm> using namespace std; int row, col; int cnt; int **matrix; int **assist; vector<int> ***path; int getSum (const vector<int> &array) { int i, ret = 0; for (i = 0; i < array.size(); ++i) { ret += array[i]; } return ret; } vector<int> DFS (int i, int j, int shell) { vector<int> maxPath, temp; if (!(*path[i][j]).empty()) { return *(path[i][j]); } assist[i][j] = 1; // up if (i-1 >= 0) { if (matrix[i][j] > 0 && (matrix[i-1][j] > matrix[i][j] || matrix[i-1][j] == 0)) { temp = DFS(i-1, j, matrix[i][j]); if (getSum(maxPath) < getSum(temp)) { maxPath = temp; } } if (matrix[i][j] == 0 && matrix[i-1][j] >= shell && assist[i-1][j] != 1) { temp = DFS(i-1, j, shell); if (getSum(maxPath) < getSum(temp)) { maxPath = temp; } } } // right if (j+1 < col) { if (matrix[i][j] > 0 && (matrix[i][j+1] > matrix[i][j] || matrix[i][j+1] == 0)) { temp = DFS(i, j+1, matrix[i][j]); if (getSum(maxPath) < getSum(temp)) { maxPath = temp; } } if (matrix[i][j] == 0 && matrix[i][j+1] >= shell && assist[i][j+1] != 1) { temp = DFS(i, j+1, shell); if (getSum(maxPath) < getSum(temp)) { maxPath = temp; } } } // down if (i+1 < row) { if (matrix[i][j] > 0 && (matrix[i+1][j] > matrix[i][j] || matrix[i+1][j] == 0)) { temp = DFS(i+1, j, matrix[i][j]); if (getSum(maxPath) < getSum(temp)) { maxPath = temp; } } if (matrix[i][j] == 0 && matrix[i+1][j] >= shell && assist[i+1][j] != 1) { temp = DFS(i+1, j, shell); if (getSum(maxPath) < getSum(temp)) { maxPath = temp; } } } // left if (j-1 >= 0) { if (matrix[i][j] > 0 && (matrix[i][j-1] > matrix[i][j] || matrix[i][j-1] == 0)) { temp = DFS(i, j-1, matrix[i][j]); if (getSum(maxPath) < getSum(temp)) { maxPath = temp; } } if (matrix[i][j] == 0 && matrix[i][j-1] >= shell && assist[i][j-1] != 1) { temp = DFS(i, j-1, shell); if (getSum(maxPath) < getSum(temp)) { maxPath = temp; } } } assist[i][j] = 0; maxPath.insert(maxPath.begin(), matrix[i][j]); return *(path[i][j]) = maxPath; } int main () { int i, j; FILE *pFile = NULL; pFile = fopen("cross.txt", "r"); if (pFile != NULL) { rewind (pFile); fscanf (pFile, "%d %d", &row, &col); matrix = (int**) malloc(sizeof(int*) * row); assist = (int**) malloc(sizeof(int*) * row); path = (vector<int>***) malloc(sizeof(vector<int>**) * row); // alloc memory for (i = 0; i < row; ++i) { matrix[i] = (int*) malloc(sizeof(int) * col); memset(matrix[i], 0, sizeof(int) * col); assist[i] = (int*) malloc(sizeof(int) * col); memset(assist[i], 0, sizeof(int) * col); path[i] = (vector<int>**) malloc(sizeof(vector<int>*) * col); for (j = 0; j < col; ++j) { path[i][j] = new vector<int>(); } } // read matrix for (i = 0; i < row; ++i) { for (j = 0; j < col; ++j) { fscanf(pFile, "%d", matrix[i] + j); } } //printf("/n"); } long time1 = clock(); DFS(0, 0, 0); long time2 = clock() - time1; for (i = 0; i < (*(path[0][0])).size(); ++i) { printf("%d ", (*(path[0][0]))[i]); } printf("/n"); for (i = 0; i < row; ++i) { free(matrix[i]); free(assist[i]); for (j = 0; j < col; ++j) { delete path[i][j]; } } printf("%d/n", time2); return 0; }

你可能感兴趣的:(俄罗斯套娃)