您的位置:首页 > 编程语言 > C语言/C++

UVa 227 Puzzles

2016-02-05 01:58 281 查看
3-5

本题调试了许久,虽然最终仍然是WA, 但是一直未能查出纰漏之处,先将代码贴上,如日后查出bug,再前来编辑:

(注释掉的是为了审查输出格式而输出到一个data.out文件)

#include <stdio.h>
#include <string.h>

int main(void)
{
// FILE *fout;
// fout = fopen("data.out", "wb");
int i, j, m, c, x, y, flag = 1, count = 0;
char store[5][10], temp[10];
fgets(temp, 8, stdin);//得到第一行输入,来判断是否是Z\n,用来结束程序
while (temp[1] != '\n')
{
if (count > 0)
// fprintf(fout, "\r\n");//如果不是第一组那么在头行输出一个换行符
printf("\n");
for (j = 0; j < 8; j++)
store[0][j] = temp[j];//将temp数组中的元素给到二维数组的第一行中
for (i = 1; i < 5; i++)
fgets(store[i], 8, stdin);//得到所有的输入
for (i = 0; i < 5; i++)
for (j = 0; j < 5; j++)
if (store[i][j] == ' ') {
x = i;
y = j;
}
while ((c = getchar()) != '0')//用来换的移动指令,用模拟的方式改变元素位置
{
if (c == '\n')
continue;
else
switch (c) {
case 'A':
if (x > 0)
{
store[x][y] = store[x-1][y];
store[x-1][y] = ' ';
x--;
}
else
{
flag = 0;
break;
}
break;
case 'B':
if (x < 4)
{
store[x][y] = store[x+1][y];
store[x+1][y] = ' ';
x++;
}
else
{
flag = 0;
break;
}
break;
case 'L':
if (y > 0)
{
store[x][y] = store[x][y-1];
store[x][y-1] = ' ';
y--;
}
else
{
flag = 0;
break;
}
break;
case 'R':
if (y < 4)
{
store[x][y] = store[x][y+1];
store[x][y+1] = ' ';
y++;
}
else
{
flag = 0;
break;
}
break;
default:
flag = 0;
break;
}
if (flag == 0)
break;
}
while ((c = getchar() != '\n'));
count++;
printf("Puzzle #%d:\n", count);
// fprintf(fout, "Puzzle #%d:\r\n", count);
if (flag == 1)
for (i = 0; i < 5; i++)
for (j = 0, m = 0; j < 5; j++, m++)//m的作用是用来输出空格和换行符的
if (m < 4)
printf("%c ", store[i][j]);
// fprintf(fout, "%c ", store[i][j]);
else
printf("%c\n", store[i][j]);
// fprintf(fout, "%c\r\n", store[i][j]);
else
printf("This puzzle has no final configuration.\n");
// fprintf(fout, "This puzzle has no final configuration.\r\n");
fgets(temp, 8, stdin);
flag = 1;
}
// fclose(fout);
return 0;
}

如有人可以看出bug所在,欢迎前来探讨
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  c语言 uva