其实题意没有多大难度(前提是翻译正确),就是小时候我们所玩的转迷宫的小游戏
但是题目在后面要求了输入输出的要求,就是不同两个结果之中要有一个空行,所以
要记得加换行
具体如下
Case++;
if(Case>1) printf("\n");
//然后由于我还是一个萌新,还希望大佬们可以指出其中的不足之处,以及如果有更简易的办法的话也可交流//
然后这是我之前一直WA的代码
可能就是因为我写了一个数组
WA代码
#include <stdio.h>
#include <string.h>
int main()
{
char s[5][10],ch[1005];
int Case=0;
while(gets(s[0])!=NULL)
{
int i,j,y,x;
if(s[0][0]=='Z') return 0;
for(i=1;i<5;i++)
gets(s[i]);
int m=0;
while(scanf("%c",&ch[m])!=EOF)
{
if(ch[m]!='0') m++;
else break;
}
ch[m]='0';getchar();
for(i=0;i<5;i++)
{
for(j=0;j<5;j++){
if(s[i][j]==' ')
{
x=i;
y=j;
break;
}
}
}
int flag=1;
int xx=x,yy=y;
for(i=0;ch[i]!='\0';i++)
{
if(ch[i]=='A')
xx--;
else if(ch[i]=='B')
xx++;
else if(ch[i]=='L')
yy--;
else if(ch[i]=='R')
yy++;
if(xx<0||xx>4||yy<0||yy>4)
flag=0;
}
Case++;
if(Case>1) printf("\n");
printf("Puzzle #%d:\n",Case);
if(flag==0)
printf("This puzzle has no final configuration.\n");
else {
for(i=0;ch[i]!='\0';i++)
{
if(ch[i]=='A')
{
s[x][y]=s[x-1][y];
s[x-1][y]=' ';
x=x-1;
}
else if(ch[i]=='B')
{
s[x][y]=s[x+1][y];
s[x+1][y]=' ';
x=x+1;
}
else if(ch[i]=='L')
{
s[x][y]=s[x][y-1];
s[x][y-1]=' ';
y=y-1;
}
else if(ch[i]=='R')
{
s[x][y]=s[x][y+1];
s[x][y+1]=' ';
y=y+1;
}
}
for(i=0;i<5;i++)
{
for(j=0;j<4;j++)
printf("%c ",s[i][j]);
printf("%c\n",s[i][j]);
}
}
}
return 0;
}
上面可能就是因为ch[1005]的问题,我是报有侥幸心理以为可以通过数据测试(因为题目没有说这个第二个数组的大小
换了之后,AC了(记得加getchar())
AC代码
#include <stdio.h>
#include <string.h>
int main()
{
char s[5][10];
int Case=0;
while(gets(s[0])!=NULL)
{
int i,j,y,x;
if(s[0][0]=='Z') return 0;
for(i=1;i<5;i++)
gets(s[i]);
for(i=0;i<5;i++)
{
for(j=0;j<5;j++){
if(s[i][j]==' ')
{
x=i;
y=j;
break;
}
}
}
int flag=1;
int xx=x,yy=y;
char c;
while((c=getchar())!='0')
{
if(c=='A')
xx--;
else if(c=='B')
xx++;
else if(c=='L')
yy--;
else if(c=='R')
yy++;
if(xx<0||xx>4||yy<0||yy>4)
{flag=0;}
if(c=='A'&&x!=0)
{
s[x][y]=s[x-1][y];
s[x-1][y]=' ';
x=x-1;
}
else if(c=='B'&&x!=4)
{
s[x][y]=s[x+1][y];
s[x+1][y]=' ';
x=x+1;
}
else if(c=='L'&&y!=0)
{
s[x][y]=s[x][y-1];
s[x][y-1]=' ';
y=y-1;
}
else if(c=='R'&&y!=4)
{
s[x][y]=s[x][y+1];
s[x][y+1]=' ';
y=y+1;
}
}
getchar();
Case++;
if(Case>1) printf("\n");
printf("Puzzle #%d:\n",Case);
if(flag==0)
printf("This puzzle has no final configuration.\n");
else{
for(i=0;i<5;i++)
{
for(j=0;j<4;j++)
printf("%c ",s[i][j]);
printf("%c\n",s[i][j]);
}
}
}
return 0;
}
在此说一下,由于还没学c++,所以目前只会用c写一些代码,如果有幸大佬们看到其中的问题所在,微臣洗耳恭听!
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。