void findSol(TreeNode *root, int sum){ if((root->left==NULL) && (root->right==NULL) && (sum-root->val ==0)){ sol.push_back(root->val); allsol.push_back(sol); return; } sol.push_back(root->val); if(root->left != NULL){ findSol(root->left, sum-root->val); sol.pop_back(); } if(root->right != NULL){ findSol(root->right, sum-root->val); sol.pop_back(); } }
vector<vector<string>> solveNQueens(int n) { vector<vector<string>> result; vector<string> sol; vector<int> colLocation(n, -1); //colLocation[i] is the column location for queen at row i findSol(n, 0, colLocation, sol, result); return result; }
vector<vector<int> > pathSum(TreeNode *root, int sum) { // Note: The Solution object is instantiated only once and is reused by each test case. sol.clear(); allsol.clear(); if(root == NULL) return allsol; findSol(root, sum); return allsol; }
void findSol(int n, int currentRow, vector<int>& colLocation, vector<string>& sol, vector<vector<string>>& result) { if (currentRow == n) { result.push_back(sol); return; } for (int col = 0; col < n; col++) { if (isValid(col, currentRow, colLocation)) { string s(n, '.'); s[col] = 'Q'; sol.push_back(s); colLocation[currentRow] = col; findSol(n, currentRow + 1, colLocation, sol, result); sol.pop_back(); colLocation[currentRow] = -1; } } }
int main() { int k,i,j,ii,jj,di,dj,max,maxi,maxj,maxii,maxjj; // input scanf("%d",&k); for (; k>0; k--) { scanf("%d",&N); for (i=0; i<N; i++) { scanf("%s",a[i]); } // s[i][j][ii][jj] = max length of a palindrome that starts at i,j and ends at ii,jj // init s to undef. for (i=0; i<N; i++) { for (j=0; j<N; j++) { for (ii=i; ii<N; ii++) { for (jj=j; jj<N; jj++) { s[i][j][ii][jj] = 0; contA[i][j][ii][jj] = '.'; contB[i][j][ii][jj] = '.'; } } } } // init palindromes of length 1 and 2 for (i=0; i<N; i++) { for (j=0; j<N; j++) { s[i][j][i][j] = 1; if (i+1<N && a[i][j]==a[i+1][j]) { s[i][j][i+1][j] = 2; contA[i][j][i+1][j] = 'D'; contB[i][j][i+1][j] = 'D'; } if (j+1<N && a[i][j]==a[i][j+1]) { s[i][j][i][j+1] = 2; contA[i][j][i][j+1] = 'R'; contB[i][j][i][j+1] = 'R'; } } } // for general i,j,ii,jj for (di=0; di<N; di++) { for (dj=0; dj<N; dj++) { for (i=0; i<N; i++) { for (j=0; j<N; j++) { ii = i+di; jj = j+dj; if (ii<N && jj<N && s[i][j][ii][jj]==0 && a[i][j]==a[ii][jj]) { // s>0 only for cases initialized above, we do not need to deal with them anymore // palindrome can be formed only if the start and end letter agree // try i+1,j -> ii-1,jj if (i+1<N && ii-1>=0 && s[i+1][j][ii-1][jj]>0) { if (s[i][j][ii][jj] < 2+s[i+1][j][ii-1][jj]) { s[i][j][ii][jj] = 2+s[i+1][j][ii-1][jj]; // printf("A updating %d %d %d %d to %d\n",i,j,ii,jj,s[i][j][ii][jj]); contA[i][j][ii][jj] = 'D'; contB[i][j][ii][jj] = 'D'; } } // try i+1,j -> ii,jj-1 if (i+1<N && jj-1>=0 && s[i+1][j][ii][jj-1]>0) { if (s[i][j][ii][jj] < 2+s[i+1][j][ii][jj-1]) { s[i][j][ii][jj] = 2+s[i+1][j][ii][jj-1]; // printf("B updating %d %d %d %d to %d\n",i,j,ii,jj,s[i][j][ii][jj]); contA[i][j][ii][jj] = 'D'; contB[i][j][ii][jj] = 'R'; } } // try i,j+1 -> ii-1,jj if (j+1<N && ii-1>=0 && s[i][j+1][ii-1][jj]>0) { if (s[i][j][ii][jj] < 2+s[i][j+1][ii-1][jj]) { s[i][j][ii][jj] = 2+s[i][j+1][ii-1][jj]; // printf("C updating %d %d %d %d to %d\n",i,j,ii,jj,s[i][j][ii][jj]); contA[i][j][ii][jj] = 'R'; contB[i][j][ii][jj] = 'D'; } } // try i,j+1 -> ii,jj-1 if (j+1<N && jj-1>=0 && s[i][j+1][ii][jj-1]>0) { if (s[i][j][ii][jj] < 2+s[i][j+1][ii][jj-1]) { s[i][j][ii][jj] = 2+s[i][j+1][ii][jj-1]; // printf("D updating %d %d %d %d to %d; %c %c\n",i,j,ii,jj,s[i][j][ii][jj],a[i][j],a[ii][jj]); contA[i][j][ii][jj] = 'R'; contB[i][j][ii][jj] = 'R'; // printf("updating cont: %c %c\n",contA[i][j][ii][jj],contB[i][j][ii][jj]); } } } } } } } // find the max max = 0; maxi = -1; maxj = -1; maxii = -1; maxjj = -1; for (i=0; i<N; i++) { for (j=0; j<N; j++) { for (ii=i; ii<N; ii++) { for (jj=j; jj<N; jj++) { if (max < s[i][j][ii][jj]) { max = s[i][j][ii][jj]; maxi = i; maxj = j; maxii = ii; maxjj = jj; } } } } } // printf("max = %d %d %d %d %d\n",max,maxi,maxj,maxii,maxjj); // reconstruct the solution sss[max] = 0; path[max-1] = 'S'; path[max] = 0; findSol(sss,path,maxi,maxj,maxii,maxjj,max); printf("%s %d %d %s\n",sss,maxi+1,maxj+1,path); } if (DEBUG) scanf("%d",&i); return 0; }