problemscpp
A collection of my answers to algorithm problems in c++.
静态 Public 成员函数 | 所有成员列表
acwing::acwing2005类 参考

AcWing 2005. 马蹄铁 更多...

#include <acwing.h>

静态 Public 成员函数

static int dfs (bool stage, char horseshoes[5][5], const bool picked[5][5], int count, int level, int x, int y, int n)
 
static int main (istream &, ostream &)
 

详细描述

AcWing 2005. 马蹄铁

在文件 acwing.h223 行定义.

成员函数说明

◆ dfs()

int acwing::acwing2005::dfs ( bool  stage,
char  horseshoes[5][5],
const bool  picked[5][5],
int  count,
int  level,
int  x,
int  y,
int  n 
)
static
参数
stage当前阶段 ture为寻找左括号 false为寻找右括号
horseshoes马蹄铁方格
picked已经走过的区域
count当前括号字符串长度
level括号平衡情况(当前左括号的数量-右括号的数量)
x当前横座标
y当前纵座标
n座标上限
返回
当前状态下最长完全平衡括号字符串的长度

在文件 acwing.cpp583 行定义.

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)
Definition: acwing.cpp:583

引用了 dfs().

被这些函数引用 dfs() , 以及 main().

◆ main()

int acwing::acwing2005::main ( istream &  cin,
ostream &  cout 
)
static

在文件 acwing.cpp564 行定义.

564 {
565 char horseshoes[5][5];
566 bool picked[5][5];
567 int n;
568 cin >> n;
569 for(int i = 0; i < n; i++) {
570 for(int j = 0; j < n; j++) {
571 cin >> horseshoes[i][j];
572 }
573 }
574 memset(picked, 0, sizeof picked);
575 if(horseshoes[0][0] == ')') {
576 cout << 0;
577 return 0;
578 }
579 cout << dfs(true, horseshoes, picked, 1, 1, 0, 0, n);
580 return 0;
581 }

引用了 dfs().

被这些函数引用 acwing::TEST().


该类的文档由以下文件生成: