#include <iostream>
#include <vector>
#include <stack>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <cstring>
#include <unordered_map>
#include <unordered_set>
#include <algorithm>
#include <numeric>
#include <chrono>
#include <ctime>
#include <cmath>
#include <cctype>
#include <string>
#include <cstdio>
#include <iomanip>
#include <thread>
#include <mutex>
#include <condition_variable>
#include <functional>
#include <iterator>
using namespace std;
char matrix[5][7], order[1024];
int x, y, len = 0;
bool IsOk(char in) {
int nx = x, ny = y;
switch (in)
{
case 'A': nx = x - 1; break;
case 'B': nx = x + 1; break;
case 'L': ny = y - 1; break;
case 'R': ny = y + 1; break;
}
if (nx < 0 || ny < 0 || nx >= 5 || ny >= 5) return false;
swap(matrix[x][y], matrix[nx][ny]);
x = nx;
y = ny;
return true;
}
bool Init() {
memset(matrix, ' ', sizeof(matrix));
//gets 也是可以的
fgets(matrix[0],7,stdin);
return true;
}
void GetStart() {
for (int i = 0; i < 5; ++i) {
for (int j = 0; j < 5; ++j) {
//vs2013 上面 换行前一个是空格 不输入到字符串中 但是提交代码 可以不用判断 似乎空格也输入到字符串中了 gets fges getline 都是这种情况
if (matrix[i][j] == ' ' || matrix[i][j] == '\n') {
x = i;
y = j;
matrix[i][j] = ' ';
return;
}
}
}
}
int main()
{
int nCount = 0;
while (Init() && matrix[0][0] != 'Z') {
int canDo = 1;x = y = len = 0;
for (int i = 1; i < 5; ++i) {
fgets(matrix[i], 7, stdin);
}
GetStart();
while (cin >> order[len] && order[len] != '0') ++len;
cin.get();
if (nCount) cout << endl;
cout << "Puzzle #" << ++nCount << ":" << endl;
for (int i = 0; i < len; ++i) {
if (canDo && !IsOk(order[i])) {
cout << "This puzzle has no final configuration." << endl; canDo = 0; break;
}
}
if (!canDo) { continue; }
for (int i = 0; i < 5; ++i) {
cout << matrix[i][0];
for (int j = 1; j < 5; ++j) {
cout << " "<<matrix[i][j] ;
}
cout << endl;
}
}
return 0;
}
习题3-5(uva-227)
最新推荐文章于 2023-07-09 21:37:58 发布