所以基本上我需要帮助来获取一个现有的数组并在该数组中移动一个特定的数字,即 3X3 并将其向上移动,等等,在它移动它周围的值之后需要相应地移动。示例 123 321 123
我想将点 (3, 2) 向左移动 123 213 123
上下元素也一样,程序也会跟上你所处的位置并相应地改变它们
我的程序也需要跟踪坐标,我还没有弄清楚如何实现。
#include <iostream>
#include <iomanip>
#include <array>
using namespace std;
void printarray(int num[3][3])
{
for(int i = 0;i < 3; i++)
{
for(int j = 0; j< 3; j++)
{
cout << num[i][j] << " ";
}
cout << "\n";
}
}
void swapValues(int (&array)[3][3], int i1,int j1,int i2,int j2)
{
int temp = array[i1][j1];
array[i1][j1] = array[i2][j2];
array[i2][j2] = temp;
}
void moveleft(int (&array)[3][3], int i, int j)
{
if (i>0 && i<3 && j>=0 && j<3)
{
swapValues(array, i, j, i-1, j);
swapValues(array, i, j, i+1, j);
}
else
{
cout << "wrong move!" << endl;
}
//swapValues(array, i, j, i, j-1);
//swapValues(array, i, j, i, j+1);
}
void movedown(int (&array)[3][3], int i, int j)
{
swapValues(array, i+1, j, i, j);
swapValues(array, i-1, j, i, j);
}
void moveright(int (&array)[3][3], int i, int j)
{
swapValues(array, i, j+1, i, j);
swapValues(array, i, j-1, i, j);
}
void moveup(int (&array)[3][3], int i, int j)
{
//here you MUST check the indexes to make sure you CAN move left
//int lim = (3*3)-1;
//for(int x = lim; x >= 1; x--)
//{
swapValues(array, i, j, i-1, j);
swapValues(array, i, j, i+1, j);
int coord1d = j * 3 + i;
}
class Puzzle
{
public:
Puzzle();
void swapValues(int num[3][3], int i1, int j1, int i2, int j2);
//void moveleft(int[3][3], int start1, int start2);
void moveup(int (&array)[3][3], int i, int j);
void moveright(int (&array)[3][3], int i, int j);
void moveleft(int (&array)[3][3], int i, int j);
void movedown(int (&array)[3][3], int i, int j);
void printarray(int[3][3]);
};
int main()
{
int state1[3][3] = {{2, 8, 3}, {1, 6, 4}, {7, 0, 5}};
int state2[3][3] = {{2, 8, 1}, {4, 6, 3}, {0, 7, 5}};
int gstate[3][3] = {{1, 2, 3}, {8, 0, 4}, {7, 6, 5}};
cout << "this is state 1" << endl;
printarray(state1);
cout << "\n\n";
cout << "now this is state 2" << endl;
printarray(state2);
cout << "\n\n";
cout << "now this is the goal state" << endl;;
printarray(gstate);
int start1, start2;
cout << endl << endl << "please enter your starting point in the states listed above on the x axis(left to right)" << endl;
cin >> start1;
cout << "now enter the point on the y axis(up or down)" << endl;
cin >> start2;
//moveleft(state1, start1, start2);
cout << "this is moving coordinate (1, 1) upwards" << endl;
moveup(state1, start1, start2);
printarray(state1);
cout << endl;
cout << "this is moving coordinate (1, 1) to the left" << endl;
moveleft(state1, start1, start2);
printarray(state1);
cout << endl;
cout << "this is moving coordinate (1, 1) downwards" << endl;
movedown(state1, start1, start2);
printarray(state1);
cout << endl;
cout << "this is moving coordinate (1, 1) to the right" << endl;
moveright(state1, start1, start2);
printarray(state1);
cout << endl;
cin.get();
cin.get();
return 0;
}
请给我代码示例,解释很好,但对我来说还不够清楚。不要求你给出答案,只是帮助我看到正确的答案
- 所以我更新了我的代码,它有点工作,唯一的问题是,当我使用除 (1, 1) 之外的任何其他坐标时,我会得到内存地址而不是数组编号。对此的任何解释将不胜感激,我不知道如何解决这样的问题,因为我不确定问题是如何存在的?
此外,该代码是完全可编译的并且可以使用,所以如果我解释得不够好,请运行看看我的意思。