UVa 227 Simulation

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.