例题7-12(uva-1343)

#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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值