题目如图
作者嫌弃打字母太慢,改用数字,该程序完成后只需再添加一个字典(如1对T,2对R等等)
即可完成,核心问题是如何交换数值,一开始想直接用starti和startj记录每次变化后的空格的position(题中为0的位置),发现数组下标不能为变量,那用指针,用一个pos指针记录空格位置每次更新,perfect(dog)(--0--)!
#include <iostream>
#include <bits/stdc++.h>
#include<ctype.h>
using namespace std;
int main()
{
int a[5][5] = { 1,2,3,4,5,6,7,8,9,10,11,0,13,14,15,16,17,18,19,20,21,22,23,24,25};
for (int i = 0; i < 5; i++)
{
for (int j = 0; j <5; j++)
{
if (j < 4)
{
cout << a[i][j]<<" ";
}
else
cout << a[i][j] << endl;
}
}
cout << endl;
string s;
cin >> s;
int starti = 2, startj = 1;
int* pos = &a[2][1];
int len = s.length();
for (int i = 0; i < len; i++)
{
int tap;
if (s[i] == 'A')
{
int tap;
tap =*pos;
*pos = *(pos - 5);
*(pos - 5) = tap;
pos = pos - 5;
continue;
}
if (s[i] == 'B')
{
int tap;
tap = *pos;
*pos = *(pos + 5);
*(pos + 5) = tap;
pos += 5;
continue;
}
if (s[i] == 'L')
{
int tap;
tap = *pos;
*pos = *(pos-1);
*(pos-1) = tap;
pos--;
continue;
}
if (s[i] == 'R')
{
int tap;
tap = *pos;
*pos = *(pos + 1);
*(pos + 1) = tap;
pos++;
continue;
}
}
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 5; j++)
{
if (j < 4)
{
cout << a[i][j] << " ";
}
else
cout << a[i][j] << endl;
}
}
}