int main(){
char a[5][5]={'T','R','G','S','J',
'X','D','O','K','I',
'M',' ','V','L','N',
'W','P','A','B','E',
'U','Q','H','C','F'};
char c;
int row=2;
int col=1;
while((c=getchar())&&c!='0'){
switch(c){
case 'A':{
if(row==0){
printf("This pazzle has no final configuration");
return 0;
}
else{
a[row][col]=a[row-1][col];
a[row-1][col]=' ';
row=row-1;
break;
}
}
case 'B':{
if(row==4){
printf("This pazzle has no final configuration");
return 0;
}
else{
a[row][col]=a[row+1][col];
a[row+1][col]=' ';
row=row+1;
break;
}
}
case 'L':{
if(col==0){
printf("This pazzle has no final configuration");
return 0;
}
else{
a[row][col]=a[row][col-1];
a[row][col-1]=' ';
col=col-1;
break;
}
}
case 'R':{
if(row==4){
printf("This pazzle has no final configuration");
return 0;
}
else{
a[row][col]=a[row][col+1];
a[row][col+1]=' ';
col=col+1;
break;
}
}
}
}
for(int i=0;i<5;i++){
for(int j=0;j<5;j++){
printf("%c ",a[i][j]);
}
printf("\n");
}
}
应该就是一个模拟的过程吧。模拟空格的移动,感觉思路上应该没有难度吧。你是哪个地方不会呢。
已经搞定了,谢谢大神!
int main(){
char a[5][5]={'T','R','G','S','J',
'X','D','O','K','I',
'M',' ','V','L','N',
'W','P','A','B','E',
'U','Q','H','C','F'};
char c;
int row=2;
int col=1;
while((c=getchar())&&c!='0'){
switch(c){
case 'A':{
if(row==0){
printf("This pazzle has no final configuration");
return 0;
}
else{
a[row][col]=a[row-1][col];
a[row-1][col]=' ';
row=row-1;
break;
}
}
case 'B':{
if(row==4){
printf("This pazzle has no final configuration");
return 0;
}
else{
a[row][col]=a[row+1][col];
a[row+1][col]=' ';
row=row+1;
break;
}
}
case 'L':{
if(col==0){
printf("This pazzle has no final configuration");
return 0;
}
else{
a[row][col]=a[row][col-1];
a[row][col-1]=' ';
col=col-1;
break;
}
}
case 'R':{
if(row==4){
printf("This pazzle has no final configuration");
return 0;
}
else{
a[row][col]=a[row][col+1];
a[row][col+1]=' ';
col=col+1;
break;
}
}
}
}
for(int i=0;i<5;i++){
for(int j=0;j<5;j++){
printf("%c ",a[i][j]);
}
printf("\n");
}
}
// 数组中有10个0~100之间的随机整数,编程求这10个数的最大值、最小值、平均值,并输出小于平均数的数。
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
char matrix[5][5];
int row = -1, col = -1; // space posiiton
string cmd;
void input()
{
cout << "Please enter the 5x5 grid of marix:" << endl;
for (int i = 0; i < 5; ++i)
{
string s;
getline(cin, s);
while (s.length() < 5) {
cout << "Line too short. Needs 5 characters. Please enter again." << endl;
getline(cin, s);
}
for (int j = 0; j < 5; ++j) {
matrix[i][j] = s[j];
if (s[j] == ' ') {
row = i;
col = j;
}
}
}
cout << "Please enter the commands" << endl;
getline(cin, cmd);
std::transform(cmd.begin(), cmd.end(), cmd.begin(), ::toupper);
}
void move(int new_row, int new_col)
{
matrix[row][col] = matrix[new_row][new_col];
matrix[new_row][new_col] = ' ';
row = new_row;
col = new_col;
}
void step(char c)
{
switch(c)
{
case 'U':
if (row > 0)
move(row - 1, col);
break;
case 'D':
if (row < 5)
move(row + 1, col);
break;
case 'L':
if (col > 0)
move(row, col - 1);
break;
case 'R':
if (col < 5)
move(row, col + 1);
break;
default:
break;
}
}
void execute()
{
for (string::iterator it = cmd.begin(); it != cmd.end(); ++it)
step(*it);
}
void print()
{
for (int i = 0; i < 5; ++i)
{
for (int j = 0; j < 5; ++j)
cout << matrix[i][j] << ' ';
cout << endl;
}
}
int main()
{
input();
cout << "Matrix before executing commands" << endl;
print();
execute();
cout << "Matrix after executing commands" << endl;
print();
return 0;
}
// Output
Please enter the 5x5 grid of marix:
URGSJ
YDOKI
N VLN
XPABE
VQHCF
Please enter the commands
UDLRUDLRUUUDDDLLLRRRQWERTYUIOPASDFGHJKLZXCVBNM
Matrix before executing commands
U R G S J
Y D O K I
N V L N
X P A B E
V Q H C F
Matrix after executing commands
U R G S J
Y D O K I
N P V L N
X A B E
V Q H C F
希望对你有帮助:https://blog.csdn.net/it_xiangqiang/category_10768339.html
您好,我是有问必答小助手,您的问题已经有小伙伴解答了,您看下是否解决,可以追评进行沟通哦~
如果有您比较满意的答案 / 帮您提供解决思路的答案,可以点击【采纳】按钮,给回答的小伙伴一些鼓励哦~~
ps:问答VIP仅需29元,即可享受5次/月 有问必答服务,了解详情>>>https://vip.csdn.net/askvip?utm_source=1146287632
非常感谢您使用有问必答服务,为了后续更快速的帮您解决问题,现诚邀您参与有问必答体验反馈。您的建议将会运用到我们的产品优化中,希望能得到您的支持与协助!
速戳参与调研>>>https://t.csdnimg.cn/Kf0y