您的位置:首页 > 其它

Uva 227 - Puzzle (数组和字符串)

2015-10-20 16:51 323 查看
题不难,就是小坑涉及到基础需注意。。。

比如:

字符串后面的'\0'和'0'不同。

getchar函数读数据直到用户按回车为止(回车字符也放在缓冲区中)。

还有题目pdf中样例二 不能直接复制到控制台调试,因为没空格,要手动测试这组。

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<cmath>
#include<cctype>
#include<vector>
#include<queue>
#include<map>
#include<set>
#define eps 10e-6

using namespace std;

typedef long long ll;
char mp[8][8];
char op[205],op1[105];

int main()
{
int ex,ey;
int cas = 1;
while(gets(mp[0])!=NULL)
{
if(mp[0][0]=='Z')
break;
if(cas!=1) printf("\n");
for(int i=1;i<5;i++)
gets(mp[i]);
for(int i=0;i<5;i++)
{
// printf("ok:  %s\n",mp[i]);
for(int j=0;j<5;j++)
{
if(mp[i][j]==' ')
{
ex = i;
ey = j;
}
}
}
printf("Puzzle #%d:\n",cas++);

memset(op,0,sizeof(op));
memset(op1,0,sizeof(op1));
int flag = 0;
char c;
gets(op);
int l = strlen(op);
while(op[l-1]!='0')
{
gets(op1);
strcat(op,op1);
l = strlen(op);
}
for(int i=0;i<l-1;i++)
{
// printf("c=%c ex=%d ey=%d\n",c,ex,ey);
c = op[i];
if(c=='A')
{
if(ex == 0)
{
flag = 1;
break;
}
mp[ex][ey] = mp[ex-1][ey];
mp[ex-1][ey] = ' ';
ex--;
}
else if(c=='B')
{
if(ex == 4)
{
flag = 1;
break;
}
mp[ex][ey] = mp[ex+1][ey];
mp[ex+1][ey] = ' ';
ex++;
}
else if(c=='L')
{
if(ey == 0)
{
flag = 1;
break;
}
mp[ex][ey] = mp[ex][ey-1];
mp[ex][ey-1] = ' ';
ey--;
}
else
{
if(ey == 4)
{
flag = 1;
break;
}
mp[ex][ey] = mp[ex][ey+1];
mp[ex][ey+1] = ' ';
ey++;
}
}
if(flag){
printf("This puzzle has no final configuration.\n");
}
else
{
for(int i=0;i<5;i++)
{
for(int j=0;j<4;j++)
{
printf("%c ",mp[i][j]);
}
printf("%c\n",mp[i][4]);
}
//printf("\n");
}
// getchar();
}
return 0;
}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: