📄 samp_a_to_b.cpp
字号:
cout << "Error: num-try must be between 1 and 5000\n";
return 0;
}
m_should_log_tries = m_should_log_options = 0; // default
if (argc >= 5) {
if (argv[4][0] == '0' && argv[4][1] == '\0') {
m_should_log_tries = m_should_log_options = 0;
} else if (argv[4][0] == '1' && argv[4][1] == '\0') {
m_should_log_tries = 1;
m_should_log_options = 0;
} else if (argv[4][0] == '2' && argv[4][1] == '\0') {
m_should_log_tries = m_should_log_options = 1;
} else {
cout << "Error: logging must be 0 or 1 or 2\n";
return 0;
}
}
return 1;
}
//======================================================================
// Program main
int main (int argc, char *argv[]) {
a2bParam param;
if ( ! param.set(argc,argv) ) {
param.log_usage();
return 1;
}
a2bProblem *problem = assemble_new_problem();
if (!problem) {
cout << "ABORT - Could not create problem\n\n";
return 1;
}
problem->set_num_try(param.num_try());
if ( param.should_log_tries() ) {
problem->enable_log_tries();
} else {
problem->disable_log_tries();
}
if ( param.should_log_options() ) {
problem->enable_log_options();
} else {
problem->disable_log_options();
}
long distance = problem->solve_a_to_b (param.from_label(),
param.to_label());
if (distance == -1) {
cout << "\nSolution could not be found.\n\n";
} else if (distance == -2) {
cout << "\nStarting-node not found\n\n";
} else if (distance == -3) {
cout << "\nEnding-node not found\n\n";
}
int status = 0;
if (distance > 0) {
report_solution (distance, problem);
} else {
cout << "Solve-Failure: No solution was found.\n";
status = -1;
}
delete problem;
return status;
}
//----------------------------------------------------------------------
// Report the solution to the problem
void report_solution (long distance, a2bProblem *problem) {
if (!distance) return;
cout << "\n" << problem->num_try() << " tries done.\n";
cout << "\nBest solution found (in try "
<< problem->solve_stat()->num_tries_to_best()
<< ") was:\n";
a2bSolutionItr itr = problem->solution_iterator();
for ( a2bNode *n = itr.first(); n; n = itr.next() ) {
cout << n->label() << " ";
}
cout << " (distance = " << distance << ")\n\n";
cout << "The best path from A to B is:\n"
<< "A S Q D H J X W B (distance = 1850)\n\n";
}
//----------------------------------------------------------------------
// assemble_new_problem
a2bProblem * assemble_new_problem() {
a2bProblem *problem = new a2bProblem;
if (!problem) {
cout << "ABORT - no new problem\n\n"; return 0; }
const long num_node = 26;
char *node_label[num_node] = {
"A", "B", "C", "D", "E", "F", "G", "H", "I",
"J", "K", "L", "M", "N", "O", "P", "Q", "R",
"S", "T", "U", "V", "W", "X", "Y", "Z" };
int problem_ok = 1;
int ok1 = 1; // adding nodes starts ok
for (long inode = 0; inode<num_node; inode++) {
a2bNode *node = new a2bNode(node_label[inode]);
if (!node) {
cout << "ABORT - no new node " << inode << "\n\n";
ok1 = 0;
break;
}
node->set_owner(problem);
problem->add_node(node);
} // end of loop creating and attaching nodes
if (problem_ok && !ok1) problem_ok = ok1;
int ok2 = problem_ok;
// Links are added in both directions
if (ok2) ok2 = add_2_links (problem, "A", "R", 150);
if (ok2) ok2 = add_2_links (problem, "A", "S", 200);
if (ok2) ok2 = add_2_links (problem, "B", "W", 150);
if (ok2) ok2 = add_2_links (problem, "B", "Y", 400);
if (ok2) ok2 = add_2_links (problem, "B", "Z", 400);
if (ok2) ok2 = add_2_links (problem, "C", "E", 550);
if (ok2) ok2 = add_2_links (problem, "C", "I", 300);
if (ok2) ok2 = add_2_links (problem, "C", "Q", 300);
if (ok2) ok2 = add_2_links (problem, "C", "U", 300);
if (ok2) ok2 = add_2_links (problem, "C", "V", 300);
if (ok2) ok2 = add_2_links (problem, "D", "F", 250);
if (ok2) ok2 = add_2_links (problem, "D", "H", 400);
if (ok2) ok2 = add_2_links (problem, "D", "I", 150);
if (ok2) ok2 = add_2_links (problem, "D", "N", 150);
if (ok2) ok2 = add_2_links (problem, "D", "Q", 200);
if (ok2) ok2 = add_2_links (problem, "E", "I", 500);
if (ok2) ok2 = add_2_links (problem, "E", "J", 350);
if (ok2) ok2 = add_2_links (problem, "E", "X", 400);
if (ok2) ok2 = add_2_links (problem, "E", "Y", 450);
if (ok2) ok2 = add_2_links (problem, "F", "G", 250);
if (ok2) ok2 = add_2_links (problem, "F", "H", 300);
if (ok2) ok2 = add_2_links (problem, "F", "K", 100);
if (ok2) ok2 = add_2_links (problem, "G", "H", 150);
if (ok2) ok2 = add_2_links (problem, "G", "L", 100);
if (ok2) ok2 = add_2_links (problem, "G", "X", 300);
if (ok2) ok2 = add_2_links (problem, "G", "Z", 350);
if (ok2) ok2 = add_2_links (problem, "H", "J", 200);
if (ok2) ok2 = add_2_links (problem, "J", "X", 150);
if (ok2) ok2 = add_2_links (problem, "L", "M", 100);
if (ok2) ok2 = add_2_links (problem, "N", "O", 150);
if (ok2) ok2 = add_2_links (problem, "N", "P", 150);
if (ok2) ok2 = add_2_links (problem, "O", "P", 150);
if (ok2) ok2 = add_2_links (problem, "Q", "S", 300);
if (ok2) ok2 = add_2_links (problem, "Q", "T", 200);
if (ok2) ok2 = add_2_links (problem, "Q", "U", 250);
if (ok2) ok2 = add_2_links (problem, "R", "S", 300);
if (ok2) ok2 = add_2_links (problem, "R", "U", 150);
if (ok2) ok2 = add_2_links (problem, "R", "V", 250);
if (ok2) ok2 = add_2_links (problem, "S", "T", 200);
if (ok2) ok2 = add_2_links (problem, "U", "V", 150);
if (ok2) ok2 = add_2_links (problem, "W", "X", 250);
if (ok2) ok2 = add_2_links (problem, "W", "Z", 300);
if (ok2) ok2 = add_2_links (problem, "X", "Y", 100);
if (problem_ok && !ok2) {
cout << "ABORT - error adding link \n\n";
problem_ok = ok2;
}
if ( ! problem_ok ) {
delete problem;
return 0;
}
return problem;
}
//----------------------------------------------------------------------
// add_link - add option to a node, linking to another node.
// Return 1 on success, 0 on failure.
int add_link (a2bProblem *problem,
const char *from_label, const char *to_label,
long distance) {
int status = problem->add_link(from_label, to_label, distance);
if (status == -1) {
cout << "ERROR: add_link: no new a2bOption\n";
} else if (status == -2) {
cout << "ERROR: add_link could not find from-node "
<< from_label << aip_Endl;
} else if (status == -3) {
cout << "ERROR: add_link could not find to-node "
<< to_label << aip_Endl;
} else if (status != 0) {
cout << "ERROR: add_link unknown status code: "
<< status << aip_Endl;
}
return ( status ? 0 : 1 );
}
//----------------------------------------------------------------------
// add_2_links - add 2 links, from node1 to node2 and
// from node2 to node1,
// both with the same distance.
// Return 1 on success, 0 on failure.
int add_2_links (a2bProblem *problem,
const char *node1_label, const char *node2_label,
long distance) {
int ok = 1;
if (ok) ok = add_link (problem, node1_label, node2_label, distance);
if (ok) ok = add_link (problem, node2_label, node1_label, distance);
return ok;
}
//======================================================================
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -