If you are interested in the introduction of the algorithm and other information, you can join the learning and communication group to communicate with each other. 948097478
1. use STL library to solve the coefficient of the trigonometric triangle.
Design ideas:
As shown in the following figure, the values of the entry elements can be obtained by the sum of the queue elements and the head elements, and the corresponding elements in line n can be obtained with the increasing number of operations.
Source code:
#include<queue> #include<iostream> #include<iomanip> using namespace std; void tri(int n){ queue<int>q; q.push(0); q.push(1); int s, e; for (int i = 0; i<n; i++) { q.push(0); for (int j = n; j>i; j--) cout <<setw(3)<< " "; do { s = q.front(); q.pop(); e = q.front(); e != 0 ? cout<< setw(5)<<e << " " : cout << " "; q.push((s + e)); } while (e != 0); cout << endl; } } int main(){ cout << "Please enter the number of rows in the delta triangle." << endl; int n; cin >> n; tri(n); return 0; }
Operation results:
2. Using STL library to solve maze.
Design ideas:
The labyrinth is designed as a two-dimensional array, which is not accessible by 1 and accessible by 0. In addition, the path is stored by stack. If the location is not accessible, the location is out of the stack. At the current location, judge whether there are accessible segments around (up and down or left), if there are, move, if not, return to the location of the previous step.
#include<stdio.h> #include<iostream> #include<stack> #include<iterator> using namespace std; bool map[11][11] = { { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, { 1, 0, 0, 1, 0, 0, 0, 1, 0, 1 }, { 1, 0, 0, 1, 0, 0, 0, 1, 0, 1 }, { 1, 0, 0, 0, 0, 1, 1, 0, 0, 1 }, { 1, 0, 1, 1, 1, 0, 0, 0, 0, 1 }, { 1, 0, 0, 0, 1, 0, 0, 0, 0, 1 }, { 1, 0, 1, 0, 0, 0, 1, 0, 0, 1 }, { 1, 0, 1, 1, 1, 0, 1, 1, 0, 1 }, { 1, 1, 0, 0, 0, 0, 0, 0, 0, 1 }, { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, };//A map is set, 1 indicates an obstacle, and 0 indicates a barrier free, assuming that the initial position is at (1, 1), and the end point is (8,8). int flag[11][11] = { { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, };//Deposit whether the location has been saved in the path, 1 is saved, 0 is not saved, and - 1 means traveled but not in the path. class migong{ public: int positionx = 1; int positiony = 1; int IsRoundAccess();//Judging whether there are obstacles around bool IsEnd();//Determine whether to arrive at the finish line. void Print();//Print the current position }; struct node{ int x=1, y=1; }pre; stack<node>s; int migong::IsRoundAccess(){ if (map[positionx - 1][positiony] == 0 && flag[positionx - 1][positiony] == 0) return 1;//Above is a passageway and not traveled. else if (map[positionx + 1][positiony] == 0 && flag[positionx + 1][positiony] == 0) return 2;//Underneath is the passage, which has not been passed. else if (map[positionx][positiony - 1] == 0 && flag[positionx][positiony - 1] == 0) return 3;//On the left is the path and has not been passed. else if (map[positionx][positiony + 1] == 0 && flag[positionx][positiony + 1] == 0) return 4;//On the right is the path and has not been passed. else return 0; } bool migong::IsEnd(){ if (positionx == 8 && positiony == 8) return 1; else return 0; } void migong::Print(){ printf("( %d ,%d )->", positionx, positiony); /*while (!s.empty()){ pre = s.top(); cout << "(" << pre.x << "," << pre.y << ")" << "->"; s.pop(); } */ } void start(int x,int y){ int p; migong m; m.positionx = x; m.positiony = y; if (map[x][y] == 1){ cout << "This location can not be used as an entry." << endl; return; } flag[x][y] = 1; pre.x = m.positionx; pre.y = m.positiony; //s.push(pre); m.Print(); while (!(m.IsEnd())){//Determine whether to arrive at the finish line. p = m.IsRoundAccess(); if (p == 1) {//If there is a path above and it has not been passed, go to the upper grid and put the coordinates above into the stack. pre.x = m.positionx--; s.push(pre); m.Print(); flag[m.positionx][m.positiony] = 1; } else if (p == 2) {//If there's a path below and you haven't gone through it, go to the upper grid and put the coordinates below in the stack. pre.x = m.positionx++; s.push(pre); m.Print(); flag[m.positionx][m.positiony] = 1; } else if (p == 3) {//If the left side is a path and has not passed, go to the upper grid and store the coordinates of the left side in the stack. pre.y = m.positiony--; s.push(pre); m.Print(); flag[m.positionx][m.positiony] = 1; } else if (p == 4) {//If the right side is a path and has not passed, go to the upper grid and store the coordinates of the right side in the stack. pre.y = m.positiony++; s.push(pre); m.Print(); flag[m.positionx][m.positiony] = 1; } else{//If all four directions go up and down/are obstacles, go back to the previous step. flag[m.positionx][m.positiony] = -1;//Mark the current position as - 1 to avoid repetition s.pop();//Move the current location out of the stack pre=s.top();//Get the coordinates of the previous step m.positionx = pre.x;//Back to the previous step m.positiony = pre.y;//Back to the previous step } } //m.Print(); } int main(){ int x, y; printf("Input entry coordinates:"); cin >> x; cin >> y; printf("Maze Walking Game:\n"); start(x,y); printf("\n"); return 0; }
Operation result
Interested can join the group to discuss: 948097478