584 {
585 if(level == 0 && !stage) {
586 if(count == 13) {
587 printf("13");
588 }
589 return count;
590 }
591
592 pair<int, int> nexts[4] = {pair(x - 1, y), pair(x + 1, y), pair(x, y - 1),
593 pair(x, y + 1)};
594
595 bool picked_cpy[5][5];
596 memcpy(picked_cpy, picked, sizeof picked_cpy);
597
598 int max = 0;
599 picked_cpy[x][y] = true;
600
601 for(const auto next: nexts) {
602 if(0 <= next.first && next.first < n && 0 <= next.second && next.second < n &&
603 !picked_cpy[next.first][next.second]) {
604 int res = 0;
605 if(stage && horseshoes[next.first][next.second] == '(') {
606 res =
dfs(
true, horseshoes, picked_cpy, count + 1, level + 1, next.first, next.second, n);
607 } else if(stage && horseshoes[next.first][next.second] == ')') {
608 res =
dfs(
false, horseshoes, picked_cpy, count + 1, level - 1, next.first, next.second, n);
609 } else if(!stage && horseshoes[next.first][next.second] == ')') {
610 res =
dfs(
false, horseshoes, picked_cpy, count + 1, level - 1, next.first, next.second, n);
611 }
612 if(max < res) {
613 max = res;
614 }
615 }
616 }
617 return max;
618 }
static int dfs(bool stage, char horseshoes[5][5], const bool picked[5][5], int count, int level, int x, int y, int n)