227 Puzzle
A children’s puzzle that was popular 30 years ago consisted of a 5×5 frame which contained 24 small squares of equal size. A unique letter of the alphabet was printed on each small square. Since there were only 24 squares within the frame, the frame also contained an empty position which was the same size as a small square. A square could be moved into that empty position if it were immediately to the right, to the left, above, or below the empty position. The object of the puzzle was to slide squares into the empty position so that the frame displayed the letters in alphabetical order.
The illustration below represents a puzzle in its original configuration and in its configuration after the following sequence of 6 moves:
1) The square above the empty position moves.
2) The square to the right of the empty position moves.
3) The square to the right of the empty position moves.
4) The square below the empty position moves.
5) The square below the empty position moves.
6) The square to the left of the empty position moves.
这道题的看着是比较简单的一道,但是对于代码来说还是有很多坑
首先是对数据的存储问题,怎么存?最先考虑到的是用二维字符数组,然后用cin进行输入
这就有问题了,cin输入它不接收空格,如立即对接收到的字符矩阵进行输出,就没有空格输出出来
所以需要换一种输出方式,改成用scanf输入,这样就可以读取空格字符了。为什么?还不知道
还需要注意的是字符数组的每一行末尾是一个回车,需要用getchar吸收回车,不然的话会导致后面的混乱
除了这个地方需要用getchar(),还有一个用的地方是读入操作的时候,读入操作这个就很皮了
它可以不写在一行,也就是说不能整行地读入,我的操作是定义一个字符,然后一个一个将字符读入,读入后放入字符数组中,连成一个字符串
之后整体进行交换的操作
进行调换之前还得判断一下可不可以调换,如果发生数组越界了就不能进行调换
其实一开始我写的是在整个外面进行判断,但是这样写有些不好,就是明明已经发生越界了却还要进行调换
其他的倒没什么可以注意的了,但是这道题还是一直过不了。。。。
明天再看吧
以下代码
#include<iostream>
#include<string>
#include<stdio.h>
using namespace std;
int main(){
int count=0;
while(1){
count++;//计数
int I,J;
char A[6][6];
for(int i=1;i<=5;i++){
for(int j=1;j<=5;j++){
scanf("%c",&A[i][j]);
if(A[1][1]=='Z'){
return 0;
}
}
getchar();
}//输入数组数据
for(int i=1;i<=5;i++){
for(int j=1;j<=5;j++){
if(A[i][j]==' '){
I=i;
J=j;
}
}
}
char c;
char s[100];
int cnt=0;
while(scanf("%c",&c)&&c!='0'){
if (c=='\n') continue;
s[cnt++]=c;
}
getchar();
//for()
//cout<<s<<endl;
//cout<<"&&&&&&&&&&&&&& 输入操作 &&&&&&&&&&&&&&&&&&&&"<<endl;
//getchar();
bool flag=1;//用于标记操作是否合法
//cout<<"flag1="<<flag<<endl;
for(int i=0;i<=cnt;i++){
if(s[i]!='0'&&flag==1){
//cout<<"I="<<I<<' '<<"J="<<J<<endl;
char t;
if(s[i]=='A'){
if(I-1<1){
flag=0;
break;
}
t=A[I][J];
A[I][J]=A[I-1][J];
A[I-1][J]=t;
I-=1;
}else if(s[i]=='B'){
if(I+1>5){
flag=0;
break;
}
t=A[I][J];
A[I][J]=A[I+1][J];
A[I+1][J]=t;
I+=1;
}else if(s[i]=='R'){
if(J+1>5){
flag=0;
break;
}
t=A[I][J];
A[I][J]=A[I][J+1];
A[I][J+1]=t;
J+=1;
}else if(s[i]=='L'){
if(J-1<1){
flag=0;
break;
}
t=A[I][J];
A[I][J]=A[I][J-1];
A[I][J-1]=t;
J-=1;
}//对数组中的元素进行调换
}
}
if(count!=1){
cout<<endl;
}
cout<<"Puzzle #"<<count<<':'<<endl;
if(!flag){
cout<<"This puzzle has no final configuration."<<endl;
}else{
for(int i=1;i<=5;i++){
for(int j=1;j<=5;j++){
cout<<A[i][j]<<' ';
}
cout<<endl;
}
}//输出调换后的数组元素
//cout<<endl;
}
return 0;
}
/*
TRGSJ
XDOKI
M VLN
WPABE
UQHCF
ARRBBL0
ABCDE
FGHIJ
KLMNO
PQRS
TUVWX
AAA
LLLL0
ABCDE
FGHIJ
KLMNO
PQRS
TUVWX
AAAAABBRRRLL0
Z
*/
需要真正弄明白每个函数都有什么作用