Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

2. (60 points) puzzle.cxx A children\'s puzzle that was popular 30 years ago con

ID: 3882706 • Letter: 2

Question

2. (60 points) puzzle.cxx A children's puzzle that was popular 30 years ago consisted of a 5x5 frame which contains 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: (a) The square above the empty position moves. (b) The square to the right of the empty position moves. (c) The square to the right of the empty position moves. (d) The square below the empty position moves. (e) The square below the empty position moves. (f) The square to the left of the empty position moves. T TRT GT STJ T RTG STJ | XD TO K I XTO KIL I MV LN MD TV TBN W P |A | B | E W PAE UT QHC F U | QH | CF Configuration after the 6 moves above Original configuration Fall 201 A DCA 19

Explanation / Answer

#include<stdio.h>
#include<string.h>
const int LEN=5;
const int MAX=100;
const int y[]={0,0,1,-1};
const int x[]={-1,1,0,0};
char map[LEN][LEN];
int tra[110];
bool legal(int pos){
    return 0<=pos&&pos<LEN;
}
void Pmap(){
    for(int cow=0;cow<LEN;cow++){
        printf("%c",map[cow][0]);
        for(int col=1;col<LEN;col++)
            printf(" %c",map[cow][col]);
        printf(" ");
    }
}
int main(){
    tra['A']=0;
    tra['B']=1;
    tra['R']=2;
    tra['L']=3;

    bool first=true;
    int Case=0;
    freopen("123.in","r",stdin);
    freopen("123.out","w",stdout);
    int bx,by;
    while(gets(map[0])){
        if(map[0][0]=='Z')break;
        for(int col=1;col<LEN;col++)
            gets(map[col]);
        for(int i=0;i<LEN;i++)
            for(int j=0;j<LEN;j++)
                if(map[i][j]==' '){
                    bx=i;by=j;
                }
        bool ok=true;
        char c;
        while(scanf(" %c",&c),c!='0'){
            if(!ok)continue;
            int nx=bx+x[tra[c]],ny=by+y[tra[c]];
            if(!legal(nx)||!legal(ny)){
                ok=false;
                continue;
            }
            map[bx][by]=map[nx][ny];
            map[nx][ny]=' ';
            bx=nx;by=ny;
        }
        getchar();
        if(first)
            first=false;
        else
            printf(" ");
        printf("Puzzle #%d: ",++Case);
        if(ok)
            Pmap();
        else
            printf("This puzzle has no final configuration. ");
    }
    return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote