#include <iostream>
#include <istream>
#include <sstream>
#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;
const int maxn = 7,maxc = 64;
char matrix[maxn][maxn], base[maxn][maxn] =
{
{ 0, 0, 1, 0, 1, 0, 0 },
{ 0, 0, 1, 0, 1, 0, 0 },
{ 1, 1, 1, 1, 1, 1, 1 },
{ 0, 0, 1, 0, 1, 0, 0 },
{ 1, 1, 1, 1, 1, 1, 1 },
{ 0, 0, 1, 0, 1, 0, 0 },
{ 0, 0, 1, 0, 1, 0, 0 }
};
char input[maxc];
enum Dirs{UP,DOWN,LEFT,RIGHT};
string res;
char last = 0;
void MoveOnce(int id,int dir, int rOc){
//移动行
if (rOc == 0){
if (dir == LEFT){
int a = matrix[id][0];
for (int i = 0; i < maxn - 1; i++){
matrix[id][i] = matrix[id][i + 1];
}
matrix[id][maxn - 1] = a;
}
else if (dir == RIGHT){
int a = matrix[id][maxn - 1];
for (int i = maxn - 1; i > 0; i--){
matrix[id][i] = matrix[id][i-1];
}
matrix[id][0] = a;
}
}
else {
//移动列
if (dir == UP){
int a = matrix[0][id];
for (int i = 0; i < maxn - 1; i++){
matrix[i][id] = matrix[i+1][id];
}
matrix[maxn - 1][id] = a;
}
else if(dir == DOWN){
int a = matrix[maxn - 1][id];
for (int i = maxn - 1; i > 0; i--){
matrix[i][id] = matrix[i-1][id];
}
matrix[0][id] = a;
}
}
}
void Moved(int type){
if (type == 'A'){
MoveOnce(2, UP, 1);
}
else if (type == 'B'){
MoveOnce(4, UP, 1);
}
else if (type == 'C'){
MoveOnce(2, RIGHT, 0);
}
else if (type == 'D'){
MoveOnce(4, RIGHT, 0);
}
else if (type == 'E'){
MoveOnce(4, DOWN, 1);
}
else if (type == 'F'){
MoveOnce(2, DOWN, 1);
}
else if (type == 'G'){
MoveOnce(4, LEFT, 0);
}
else if (type == 'H'){
MoveOnce(2, LEFT, 0);
}
}
void MoveBack(int type){
if (type == 'A'){
Moved('F');
}
else if (type == 'B'){
Moved('E');
}
else if (type == 'C'){
Moved('H');
}
else if (type == 'D'){
Moved('G');
}
else if (type == 'E'){
Moved('B');
}
else if (type == 'F'){
Moved('A');
}
else if (type == 'G'){
Moved('D');
}
else if (type == 'H'){
Moved('C');
}
}
int GetCnt()
{
int one = 0, two = 0, three = 0;
for (int i = 2; i <= 4; i++){
for (int j = 2; j <= 4; j++){
if (matrix[i][j] == '1') one++;
else if (matrix[i][j] == '2') two++;
else if (matrix[i][j] == '3') three++;
}
}
return 8 - max(one, max(two, three));
}
bool dfs(string &s, int len){
int cnt = GetCnt();
if (cnt == 0) {
if (res == "" || res > s) { res = s; last = matrix[2][2]; }
return true;
}
if (cnt > len) return false;
for (int i = 'A'; i <= 'H'; i++){
s.push_back(i);
Moved(i);
if (dfs(s, len - 1)) return true;
MoveBack(i);
s.pop_back();
}
return false;
}
int GetResult(){
res = "";
last = matrix[2][2];
if (GetCnt() == 0) return 0;
string temp;
for (int i = 1;; i++){
if (dfs(temp, i)) return i;
}
return 0;
}
int main()
{
while (true){
int cur = 0;
fgets(input, maxc, stdin);
if (input[0] == '0') break;
memset(matrix, 0, sizeof(matrix));
for (int i = 0; i < maxn; i++){
for (int j = 0; j < maxn; j++){
if (!base[i][j])continue;
matrix[i][j] = input[cur];
cur += 2;
}
}
int cnt = GetResult();
if (cnt == 0){
cout << "No moves needed" << endl;
}
else
cout << res << endl;
cout << last << endl;
}
return 0;
}
例题7-12(uva-1343)
最新推荐文章于 2021-05-18 14:12:33 发布