Feeling this problem is really a big water problem (although it is the world finals real problem), but it gives the input and output data, marked the Lao Zi World Finals Real Title Although the topic is very watery but the data will be able to kill you ... has been PE PE until this morning AC ... The incomparable feeling ... This is because the last line cannot be empty. The topic of the topic is that there is such a 5
5 square Inside there are 24 small squares and an empty, then this game originally meant to move all the squares (each printed a letter) in alphabetical order, but the problem it water, did not let you find how to move, But just to give you some instructions to see the instructions. This 5What the 5 version looks like ... Emmm This is very simple, if you think so it is wrong first of all its input data will be the card holder. Analysis of the card person's place ① input data is a series of consecutive characters, and the middle of the empty no @, no #, use a space. Space Ah, I went to let me enter the first time I did not find and was a pit. What does a space mean? means the scaanf and cin of the flapping street. ② then it entered the instruction is infinite and then to 0 end, well this is still relatively easy but actually the command in the middle there will be empty line ah I go ... There may be more than one line of instructions, what does a blank line mean? It means that getline also jumped on the street. ③ Fortunately we also have C=getchar () while loop, but the output data also has the first regular pit is the letter finally can not have the extra space, this very good understanding ah, the general question may also have. ④ to the end is the most pit, that is, the last line of output can not have a blank line. Key output end look at z Ah, how do I know when the output z? Solve the last output z when the previous result can not have the extra blank line problem I didn't want to understand this until this morning, just add an if (num>0) to the back of Z and then enter the line, and then delete each of the following results in the blank line. Problem solving there is nothing to say the problem of writing a swap and then the meaning of swap on the line, pay attention to add a decision if out of bounds then there is no result. I have this decision written in a function inside my code (VJ 20ms)
#include <iostream> #include <cstring>using namespace Std;int f;char Order[10000];char orderplus[10000]; String map[5];void swap (char &a,char &b) {char C; C=a; A=b; B=c;} BOOL Exc (int a,int b) {if (a<0| | b<0| | a>=5| | b>=5) {f=1; return true; } else return false;} Main () {int num=0; while (1) {memset (order,0,sizeof (order)); f=0; int x, y; for (int i=0;i<5;i++) {getline (cin,map[i]); if (map[0][0]== ' Z ') return 0; } if (num>0) cout<<endl; for (int i=0;i<5;i++) for (int j=0;j<5;j++) if (map[i][j]== ") {x=i;y=j; } int p=0; Char now; while ((Now=getchar ()) = ' 0 ') Order[p++]=now; Cin.get (); for (int i=0;i<p;i++) {if (order[i]== ' A ') {if (exc (x-1,y)) BR Eak Swap(Map[x-1][y],map[x][y]); X=x-1;y=y; } else if (order[i]== ' R ') {if (exc (x,y+1)) break; Swap (map[x][y+1],map[x][y]); x=x;y=y+1; } else if (order[i]== ' B ') {if (exc (x+1,y)) break; Swap (map[x+1][y],map[x][y]); X=x+1;y=y; } else if (order[i]== ' L ') {if (exc (x,y-1)) break; Swap (map[x][y-1],map[x][y]); X=x;y=y-1; }} printf ("Puzzle #%d:\n", ++num); if (f) cout<< "This puzzle have no final configuration.\n"; else {for (int. i=0;i<5;i++) {for (int j=0;j<5;j++) { cout<<map[i][j]; if (j<4) cout<< ""; } Cout<<endl; } } }}
UVa 227 Puzzle (UVA-227)