題目難度不大,主要(基本上是完全的)是輸入輸出的格式控制了……
兩大坑:
輸入里面如果空格在行尾,會直接換行!(是PQRS/n而不是PQRS空格/n)
輸入的指令可能跨行!
能避開這兩個坑應該就還好吧……
#define NDEBUG//這個宏的意思是不要debug,交上去的時候要有這一句。//第0坑,終于做完,興高采烈,忘了刪這句話//#define LOCAL//定義了這個宏,就是輸出到本地,否則是輸出到屏幕#include <stdio.h>#include <iomanip>#include <iostream>#include <cmath>#include <ctime>#include <cassert>#include <string>#include <vector>const double pi = acos(-1.0);using namespace std;int row = 0, col = 0;char Puzzle[5][5];int main(int argc, char* argv[]){#ifdef LOCAL FILE* newFile; freopen_s(&newFile, "out.txt", "w", stdout); FILE* inFile; freopen_s(&inFile, "in.txt", "r", stdin);#endif cout << setiosflags(ios_base::fixed) << setPRecision(2); int nPuzzle = 0; bool first = true; while (true) { //輸入Puzzle for (int i = 0; i < 5; ++i) { int k = 0; while (true) { char ch; cin.get(ch); if (ch == 'Z') { goto tag;//多重循環,無奈,只能這樣跳了 } //找到空格 if (ch == ' ') { Puzzle[i][k] = ch; row = i; col = k; } //找到換行就讀完了一行了,這里神坑!如果空格在最后一個,它居然直接是換行! else if (ch == '/n') { if (k == 4) { Puzzle[i][k] = ' '; row = i; col = k; } break; } else { Puzzle[i][k] = ch; } ++k; } } ++nPuzzle; //處理Puzzle //第二神坑,指令可能跨行! string Word; char temp; //為了下次處理,這個換行要去掉 while (true) { cin.get(temp); if (temp == '0') { word += temp; break; } else { word += temp; } } cin.get();//為了下次處理,這個換行要去掉 //先輸出頭 if (!first) { cout <<endl << "Puzzle #" << nPuzzle << ':' << endl; } else { first = !first; cout << "Puzzle #" << nPuzzle << ':' << endl; } for (auto element : word) { if (element == '0') { for (int i = 0; i < 5; ++i) { for (int k = 0; k < 4; ++k) { cout << Puzzle[i][k] << ' '; } cout << Puzzle[i][4] << endl; } break; } else if (element == 'A') { if (row == 0) { cout << "This puzzle has no final configuration." << endl; break; } else { swap(Puzzle[row][col], Puzzle[row - 1][col]); --row; } } else if (element == 'B') { if (row == 4) { cout << "This puzzle has no final configuration." << endl; break; } else { swap(Puzzle[row][col], Puzzle[row + 1][col]); ++row; } } else if (element == 'L') { if (col == 0) { cout << "This puzzle has no final configuration." << endl; break; } else { swap(Puzzle[row][col], Puzzle[row][col - 1]); --col; } } else if (element == 'R') { if (col == 4) { cout << "This puzzle has no final configuration." << endl; break; } else { swap(Puzzle[row][col], Puzzle[row][col + 1]); ++col; } } } }tag: return 0;}
新聞熱點
疑難解答