/* Problem 6--Puzzle This one wasn't too bad. Once you correctly read in the data, processing it was pretty easy. */ #include #include using namespace std; ifstream in ("prob6.in"); ofstream out ("prob6.out"); int main (int argc, char **argv); int main (int argc, char **argv) { char Game[7][7]={}; //By making it 7x7, we can surround the board with //a ring of nulls. int blankrow=0, blankcol=0; //Location of the space int cs=0; while (true) { in.getline (&Game[1][1],6); //Read in the first line if (Game[1][1]=='0') return 0; for (int i = 2; i < 6; i++) in.getline (&Game[i][1],6);//Get rest for (int i = 1; i < 6; i++) { char *p = strchr (&Game[i][1],' ');//Find the space if (p==NULL) continue; blankrow = i; blankcol = p-Game[i]; break; } bool valid = true; while (true) { //Get the next direction char ch; in >> ch; if (ch=='0') break; //If it's a 0, we're done with this game if (!valid) continue; //If we already have an error, don't process. int h=0, v=0; switch (ch) { case 'A' : v = -1; break; //Compute the direction the blank moves. case 'B' : v = 1; break; case 'L' : h = -1; break; case 'R' : h = 1; break; } if (Game[blankrow+v][blankcol+h]==0) { //If we hit a null, we're out valid = false; continue;} //of bounds. Game[blankrow][blankcol] = Game[blankrow+v][blankcol+h]; Game[blankrow+v][blankcol+h] = ' '; //Make the move. blankrow += v; blankcol += h; } in.getline (&Game[0][0],6); //Get that last . out << "Puzzle #"<< ++cs << ":" << endl; if (!valid) {out << "This puzzle has no final configuration." << endl << endl; continue;} for (int i = 1; i < 6; i++) { //Print board. for (int j = 1; j < 6; j++) out << Game[i][j] << " "; out << endl; } out << endl; } return 0; }