Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstdlib>
- #include <string>
- using namespace std;
- const int MAXN = 10;
- //row 0 & n+1, column 0 & n+1 are kept empty to provide safe boundary for the data
- char matrix[MAXN+2][MAXN+2];
- int n = 0;
- int maxMatchedLength = 0;
- void findMatchingClose(int count, int r, int c, int maxOpenCount) {
- if (count == 0) {
- if (maxMatchedLength < maxOpenCount)
- maxMatchedLength = maxOpenCount;
- return;
- }
- //more digging; count > 0
- if (matrix[r][c] != ')')
- return;
- //digging for matching )
- findMatchingClose(count-5, r-1, c-1, maxOpenCount);
- //...
- }
- //start from (r,c), return the longest sequence of "(" found
- void findMaxOpen(int count, int r, int c) {
- if (matrix[r][c] != '(') {
- findMatchingClose(count, r, c, count);
- return;
- }
- //confirmed as '('
- matrix[r][c] = ' ';
- findMaxOpen(count+1, r+1, c);
- findMaxOpen(count+1, r-1, c);
- findMaxOpen(count+1, r, c+1);
- findMaxOpen(count+1, r, c-1);
- matrix[r][c] = '(';
- }
- int main()
- {
- cin >> n;
- //row 0 & n+1, column 0 & n+1 are kept empty to provide safe boundary for the data
- for(int i=1; i<=n; i++)
- for(int j=1; j<=n; j++)
- cin >> matrix[i][j];
- findMaxOpen(0, 1,1);
- cout << maxMatchedLength*2;
- //testing i/o
- // for(int i=1; i<=n; i++)
- // cout << &matrix[i][1] << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement