Background: Did two hours, all kinds of mistakes, various debugging, see the final question is 1993.
Learning 1. Definition of struct data type:
typedef struct{ int x; int y; } place;//uses this type to define a variable. Palce A, B;
2. Once the input sequence is judged to be invalid, it should not be exited immediately, and the remaining elements in the input sequence should be processed to avoid being treated as the next input sequence.
3. Once a variable value can go beyond the array line, do not let the variable be subscript as an array by first judging whether it is out of bounds:
if (blank.x>4| | blank.x<0| | blank.y<0| | blank.y>4) {casse=0; continue;} /<span style= "Font-size:18px;color: #FF0000;" The upper and lower bounds of the >/transverse ordinate are judged as good. Column such as: Do not because here is x--only Judge X>0, there may have been x=6, even if x--still have x>4 not satisfied. </span>
#include <stdio.h>typedef struct{int x; int y; }place;int Main (void) {char puzzle[5][5]; int count=1; while (1) {if (count-1) GetChar (); Puzzle[0][0]=getchar (); if (puzzle[0][0]== ' Z ') break; for (int i=1;i<5;i++) Puzzle[0][i]=getchar (); for (int i=1;i<5;i++) {getchar (); for (int ii=0;ii<5;ii++) Puzzle[i][ii]=getchar (); }place blank;for (int i=0;i<5;i++) for (int j=0;j<5;j++) if (puzzle[i][j]== ") {blank.x=i; Blank.y=j; }int k=0; int casse=1; while (1) {if (!k) GetChar (); K++;char move;scanf ("%c", &move), if (move== ' \ n ') continue;if (move== ' 0 ') Break;else if ( move== ' A ') {blank.x--;if (blank.x>4| | blank.x<0| | blank.y<0| | blank.y>4) {casse=0; continue;} Puzzle[blank.x+1][blank.y]=puzzle[blank.x][blank.y];p uzzle[blank.x][blank.y]= ' ';} else if (move== ' B ') {blank.x++;if (blank.x>4| | blank.x<0| | blank.y<0| | blank.y>4) {casse=0; continue;} Puzzle[blank.x-1][blank.y]=puzzle[blank.x][blank.y];p uzzle[blank.x][blank.y]= ' ';} else if(move== ' L ') {Blank.y--;if (blank.x>4| | blank.x<0| | blank.y<0| | blank.y>4) {casse=0; continue;} Puzzle[blank.x][blank.y+1]=puzzle[blank.x][blank.y];p uzzle[blank.x][blank.y]= ' ';} else if (move== ' R ') {blank.y++;if (blank.x>4| | blank.x<0| | blank.y<0| | blank.y>4) {casse=0; continue;} Puzzle[blank.x][blank.y-1]=puzzle[blank.x][blank.y];p uzzle[blank.x][blank.y]= ' ';} else {casse=0; continue;}} if (Casse) {if (count-1) printf ("\ n"); printf ("Puzzle #%d:\n", count++); for (int. i=0;i<5;i++) {for (int j=0;j<5;j++) {if (j) printf (""); printf ("%c", Puzzle[i][j]); } printf ("\ n"); }} else {if (count-1) printf ("\ n"); printf ("Puzzle #%d:\n", count++); printf ("This puzzle have no final configuration.\n"); }} return 0;}
UVa 227 Simulation