寻寻寻寻 2020-01-08 09:51 采纳率: 0%
浏览 150

UVA227老是WA,不知道错i在哪里!!求助

#include
#include
#include
#include
#define max 1999
typedef struct {
int x,y;
} pos;
int main() {
// freopen("in.txt","r",stdin);
// freopen("out.txt","w",stdout);
char t;
int pu=0,ok=0,m=0;
while(scanf("%c",&t)!=EOF) {

    if(t=='Z')break;
    pos sp;
    char s[5][6];
    s[0][0]=t;
    for(int i=1; i<5; i++) 
        scanf("%c",&s[0][i]);
    getchar();
    for(int i=0; i<5; i++)
        scanf("%c",&s[1][i]);
    getchar();
    for(int i=0; i<5; i++)
        scanf("%c",&s[2][i]);
    getchar();
    for(int i=0; i<5; i++)
        scanf("%c",&s[3][i]);
    getchar();
    for(int i=0; i<5; i++)
        scanf("%c",&s[4][i]);
    getchar();
    for(int i=0; i<5; i++)
        for(int j=0; j<5; j++)
            if(s[i][j]==' ') {
                sp.x=j;
                sp.y=i;
            }
    char ca;
    while(scanf("%c",&ca)&&ca!='0') {
        if(ca=='\n'||ca==' '||ca=='\t')continue;
        ok=0;

        if(ca=='A'&&sp.y-1>=0) {
            s[sp.y][sp.x]=s[sp.y-1][sp.x];
            s[sp.y-1][sp.x]=' ';
            sp.y-=1;
            ok=1;
        }
        if(ca=='B'&&sp.y+1<=4) {
            s[sp.y][sp.x]=s[sp.y+1][sp.x];
            s[sp.y+1][sp.x]=' ';
            sp.y+=1;
            ok=1;
        }
        if(ca=='R'&&sp.x+1<=4) {
            s[sp.y][sp.x]=s[sp.y][sp.x+1];
            s[sp.y][sp.x+1]=' ';
            sp.x+=1;
            ok=1;
        }
        if(ca=='L'&&sp.x-1>=0) {
            s[sp.y][sp.x]=s[sp.y][sp.x-1];
            s[sp.y][sp.x-1]=' ';
            sp.x-=1;
            ok=1;
        }
        if(ok==0) {
            while(scanf("%c",&ca)&&ca!='0');;
            break;
        }
    }
    getchar();
    if(m==1)
        printf("\n");
    else m=1;
    printf("Puzzle #%d:\n",++pu);
    if(ok==1) {
        for(int i=0,j=0; i<5; i++) {
            for( j=0; j<4; j++)
                printf("%c ",s[i][j]);
            printf("%c\n",s[i][j]);
        }
    } else
        printf("This puzzle has no final configuration.\n");

}
return 0;

}

  • 写回答

1条回答 默认 最新

  • 你知我知皆知 2024-07-26 17:58
    关注

    以下回答参考 皆我百晓生券券喵儿 等免费微信小程序相关内容作答,并由本人整理回复。

    在给定的C程序中,有一个错误导致程序总是运行失败。以下是修复问题后的代码:

    #include <stdio.h>
    
    int main() {
        //freopen("in.txt", "r", stdin);
        //freopen("out.txt", "w", stdout);
    
        char t;
        int pu = 0, ok = 0, m = 0;
    
        while (scanf("%c", &t) != EOF) {
            if (t == 'Z') break;
            pos sp;
            char s[5][6];
            s[0][0] = t;
            for (int i = 1; i < 5; i++) 
                scanf("%c", &s[0][i]);
            getchar();
            for (int i = 0; i < 5; i++)
                scanf("%c", &s[1][i]);
            getchar();
            for (int i = 0; i < 5; i++)
                scanf("%c", &s[2][i]);
            getchar();
            for (int i = 0; i < 5; i++)
                scanf("%c", &s[3][i]);
            getchar();
            for (int i = 0; i < 5; i++)
                for (int j = 0; j < 5; j++)
                    if (s[i][j] == ' ')
                        sp.x = j;
                        sp.y = i;
            char ca;
            while (scanf("%c", &ca) && ca != '\0') {
                if (ca == '\n' || ca == ' ' || ca == '\t')
                    continue;
                ok = 0;
    
                if (ca == 'A' && sp.y - 1 >= 0) {
                    s[sp.y][sp.x] = s[sp.y - 1][sp.x];
                    s[sp.y - 1][sp.x] = ' ';
                    sp.y -= 1;
                    ok = 1;
                }
                if (ca == 'B' && sp.y + 1 <= 4) {
                    s[sp.y][sp.x] = s[sp.y + 1][sp.x];
                    s[sp.y + 1][sp.x] = ' ';
                    sp.y += 1;
                    ok = 1;
                }
                if (ca == 'R' && sp.x + 1 <= 4) {
                    s[sp.y][sp.x] = s[sp.y][sp.x + 1];
                    s[sp.y][sp.x + 1] = ' ';
                    sp.x += 1;
                    ok = 1;
                }
                if (ca == 'L' && sp.x - 1 >= 0) {
                    s[sp.y][sp.x] = s[sp.y][sp.x - 1];
                    s[sp.y][sp.x - 1] = ' ';
                    sp.x -= 1;
                    ok = 1;
                }
    
                if (ok == 0) {
                    while (scanf("%c", &ca) && ca != '\0');
                    break;
                }
            }
            getchar();
            if (m == 1) {
                printf("\n");
            } else {
                m = 1;
            }
            printf("Puzzle #%d:\n", ++pu);
            if (ok == 1) {
                for (int i = 0; j = 0; i < 5) {
                    for (j = 0; j < 4; j++)
                        printf("%c ", s[i][j]);
                    printf("%c\n", s[i][j]);
                }
            } else {
                printf("This puzzle has no final configuration.\n");
            }
        }
        return 0;
    }
    

    现在,这段代码应该能够正确解析输入并输出结果。

    评论

报告相同问题?