web123456

Oriental Boyi OJ [Basic] Pawn Traversal

Title Description

On an n*m board (e.g., 6 rows and 7 columns) there is a pawn in the upper leftmost corner at the position (1,1). The pawn can only move down or to the right, and the pawn adopts the strategy of moving down first and then to the right when the next move reaches the head. Ask what moves can be made from the point (1,1) to the point (n,m), and output these moves.

importation

Two integers n, m represent the size of the board (3≤n≤8,3≤m≤8)

exports

Pawn Walking Route

example

importation

3 3

 

exports

  1. 1:1,1->2,1->3,1->3,2->3,3
  2. 2:1,1->2,1->2,2->3,2->3,3
  3. 3:1,1->2,1->2,2->2,3->3,3
  4. 4:1,1->1,2->2,2->3,2->3,3
  5. 5:1,1->1,2->2,2->2,3->3,3
  6. 6:1,1->1,2->1,3->2,3->3,3

 

  1. //Solution 1: Refer to the first path of the maze and search deep to find all paths of the maze
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4. //Can only go down or to the right: first down, then right.
  5. int n,m;
  6. int r[20][3];//Storing travel paths
  7. //Changes in direction
  8. int fx[3] = {0,1,0};
  9. int fy[3] = {0,0,1};
  10. int c;//register
  11. void print(int k){
  12. c++;
  13. cout<<c<<":";
  14. //Except for the last point.
  15. for(int i = 1;i < k;i++){
  16. cout<<r[i][1]<<","<<r[i][2]<<"->";
  17. }
  18. cout<<n<<","<<m<<endl;
  19. }
  20. //To the row of the r array with subscript k, record the x,y points
  21. void dfs(int x,int y,int k){
  22. //Record coordinates
  23. r[k][1] = x;
  24. r[k][2] = y;
  25. //If it goes to the end, print the path
  26. if(x == n && y == m){
  27. print(k);
  28. //Stop the recursive function, when it reaches the end of the printout, there is no need to continue the recursion
  29. return;
  30. }
  31. int tx,ty;
  32. for(int i = 1;i <= 2;i++){
  33. tx = x + fx[i];
  34. ty = y + fy[i];
  35. //Determine the validity of tx,ty
  36. if(tx>=1&&tx<=n&&ty>=1&&ty<=m){
  37. dfs(tx,ty,k+1);
  38. }
  39. }
  40. }
  41. int main(){
  42. cin>>n>>m;
  43. //The subscript to the r array is1The line that records the1,1point (in space or time)
  44. dfs(1,1,1);
  45. }
  1. //Solution 1: Refer to the first path of the maze and search deep to find all paths of the maze
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4. //Can only go down or to the right: first down, then right.
  5. int n,m;
  6. int r[20][3];//Storing travel paths
  7. //Changes in direction
  8. int fx[3] = {0,1,0};
  9. int fy[3] = {0,0,1};
  10. int c;//register
  11. void print(int k){
  12. c++;
  13. cout<<c<<":";
  14. //Except for the last point.
  15. for(int i = 1;i < k;i++){
  16. cout<<r[i][1]<<","<<r[i][2]<<"->";
  17. }
  18. cout<<n<<","<<m<<endl;
  19. }
  20. //To the row of the r array with subscript k, record the x,y points
  21. void dfs(int x,int y,int k){
  22. //Record coordinates
  23. r[k][1] = x;
  24. r[k][2] = y;
  25. //If it goes to the end, print the path
  26. if(x == n && y == m){
  27. print(k);
  28. //Stop the recursive function, when it reaches the end of the printout, there is no need to continue the recursion
  29. return;
  30. }
  31. int tx,ty;
  32. for(int i = 1;i <= 2;i++){
  33. tx = x + fx[i];
  34. ty = y + fy[i];
  35. //Determine the validity of tx,ty
  36. if(tx>=1&&tx<=n&&ty>=1&&ty<=m){
  37. dfs(tx,ty,k+1);
  38. }
  39. }
  40. }
  41. int main(){
  42. cin>>n>>m;
  43. //The subscript to the r array is1The line that records the1,1point (in space or time)
  44. dfs(1,1,1);
  45. }

source (of information etc)

deep search recursive (calculation)