题目难度不大,主要(基本上是完全的)是输入输出的格式控制了……
两大坑:
输入里面如果空格在行尾,会直接换行!(是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;
}