VJ - R - 魔板

题目来源 VJ - R - 魔板
](https://vjudge.net/contest/441637#problem/O)

在魔方风靡全球之后不久,Rubik先生发明了它的简化版——魔板。魔板由8个同样大小的方块组成,每个方块颜色均不相同,可用数字1-8分别表示。任一时刻魔板的状态可用方块的颜色序列表示:从魔板的左上角开始,按顺时针方向依次写下各方块的颜色代号,所得到的数字序列即可表示此时魔板的状态。例如,序列(1,2,3,4,5,6,7,8)表示魔板状态为:

1 2 3 4 8 7 6 5

对于魔板,可施加三种不同的操作,具体操作方法如下:

A: 上下两行互换,如上图可变换为状态87654321 B: 每行同时循环右移一格,如上图可变换为41236785 C:
中间4个方块顺时针旋转一格,如上图可变换为17245368

给你魔板的初始状态与目标状态,请给出由初态到目态变换数最少的变换步骤,若有多种变换方案则取字典序最小的那种。 Input
每组测试数据包括两行,分别代表魔板的初态与目态。 Output 对每组测试数据输出满足题意的变换步骤。

Sample Input
12345678
17245368
12345678
82754631
Sample Output
C
AC

题解:
康托 + BFS() + 模拟:

/*
https://vjudge.net/contest/441637#problem/R
             R - 魔板
*/
#include<bits/stdc++.h>
using namespace std;
const int maxn = 5e5;
struct node1 {
	int a[10];
	int cantor;
};

struct node {
	char step;
	int fa;
} t[maxn];

int num[10], pos[10], goal[10];
int p[] = {1,1,2,6,24,120,720,5040,40320};
int Cantor(int *a) {
	int ans = 0;
	for(int i = 1; i <= 8; i ++) {
		int k = 0;
		for(int j = i + 1; j <= 8; j ++) {
			if(a[i] > a[j]) {
				k ++;
			}
		}ans += k*p[8-i];
	}
	return ans;
}
// A
void A(int *a) {
	swap(a[1], a[5]);
	swap(a[2], a[6]);
	swap(a[3], a[7]);
	swap(a[4], a[8]);
}

// B
void B(int *a) {
	swap(a[3], a[4]);
	swap(a[7], a[8]);
	swap(a[2], a[3]);
	swap(a[6], a[7]);
	swap(a[1], a[2]);
	swap(a[5], a[6]);
}

// C
void C(int *a) {
	swap(a[2], a[3]);
	swap(a[2], a[7]);
	swap(a[2], a[6]);
}


void init() {
	for(int i = 0; i <= maxn; i ++) {
		t[i].fa = -1;
	}
}
void bfs() {
	queue<node1> q;
	node1 S;
	for(int i = 1; i <= 8; i ++) {
		S.a[i] = i;
	}
	S.cantor = 0;
	q.push(S);
	while(!q.empty()) {
		node1 now = q.front();
		q.pop();

		node1 next = now;
		A(next.a);
		next.cantor = Cantor(next.a);
		if(t[next.cantor].fa == -1) {
			t[next.cantor].fa = now.cantor;
			t[next.cantor].step = 'A';
			q.push(next);
		}

		next = now;
		B(next.a);
		next.cantor = Cantor(next.a);
		if(t[next.cantor].fa == -1) {
			t[next.cantor].fa = now.cantor;
			t[next.cantor].step = 'B';
			q.push(next);
		}

		next = now;
		C(next.a);
		next.cantor = Cantor(next.a);
		if(t[next.cantor].fa == -1) {
			t[next.cantor].fa = now.cantor;
			t[next.cantor].step = 'C';
			q.push(next);
		}
	}
}
int main() {
	char Start[10], End[10];
	init();
	bfs();
	while(~scanf("%s %s", Start, End)) {
		for(int i = 0; i < strlen(Start); i ++) {
			num[i+1] = Start[i]-'0';
		}
		swap(num[5],num[8]);
		swap(num[6],num[7]);
		for(int i = 1; i <= 8; i ++) {
			pos[num[i]] = i;
		}
		for(int i = 0;  i < strlen(End); i++) {
			goal[i+1] = End[i] - '0';
		}
		swap(goal[5],goal[8]);
		swap(goal[6],goal[7]);
        for(int i=1; i<=8; i++){
        	goal[i]=pos[goal[i]];
		}
		int child = Cantor(goal), cnt = 0;
		char op[1000];
		while(child) {
			op[++cnt] = t[child].step;
			child = t[child].fa;
		}
		for(int i = cnt; i >= 1; i --) {
			cout << op[i];
		}
		cout << endl;
	}
	return 0;
}

类似题:H - Eight
简单介绍一下八数码问题:
在一个3×3的九宫格上,填有1~8八个数字,空余一个位置,例如下图:
1 2 3
4 5 6
7 8
在上图中,由于右下角位置是空的,你可以移动数字,比如可以将数字6下移一位:
1 2 3 1 2 3
4 5 6 → 4 5
7 8 7 8 6
或者将数字8右移一位:
1 2 3 1 2 3
4 5 6 → 4 5 6
7 8 7 8
1~8按顺序排列的情况称为“初始状态”(如最上方图)。“八数码问题”即是求解对于任意的布局,将其移动至“初始状态”的方法。
给定一个现有的九宫格布局,请输出将它移动至初始状态的移动方法的步骤。
Input
输入包含多组数据,处理至文件结束。每组数据占一行,包含8个数字和表示空位的‘x’,各项以空格分隔,表示给定的九宫格布局。
例如,对于九宫格
1 2 3
4 6
7 5 8
输入应为:1 2 3 x 4 6 7 5 8
注意,输入的数字之间可能有(不止一个?)空格。
Output
对于每组输入数据,输出一行,即移动的步骤。向上、下、左、右移动分别用字母u、d、l、r表示;如果给定的布局无法移动至“初始 状态”,请输出unsolvable。
如果有效的移动步骤有多种,输出任意即可。

Sample Input
2 3 4 1 5 x 7 6 8
Sample Output
ullddrurdllurdruldr

#include<bits/stdc++.h>
using namespace std;
const int N  = 4e5+10;
const int dx[4] = {-1,0,1,0};
const int dy[4] = {0,1,0,-1};
const char op[7] ="dlur";
bool vis[N];
int pre[N];
char preMay[N];

struct node{
    int m[3][3];
    int ok;
    int x,y;
}End;


const int jiecheng[9]={1,1,2,6,24,120,720,5040,40320}; //阶乘

int Cantor(int m[][3]){
    int res  = 0;
    
    //保存到以为数组方便计算
    int tmp[10],cnt = 0;
    for(int i = 0; i < 3; i++){
        for(int j = 0; j < 3; j++){
            tmp[cnt++] = m[i][j];
        }
    }

    //计算康托值
    for(int i = 0; i < 9; i++){
        int k = 0;
        for(int j = i+1; j < 9; j++){
            if(tmp[j] < tmp[i]) k++;
        }
        res+= jiecheng[9-i-1]*k;
    }
    return res;
}

void bfs(){

    queue<node> q;
    while(!q.empty()){
    	return ;
	}
    memset(vis,0,sizeof(vis));
    vis[End.ok] = true;
    q.push(End);
    pre[End.ok] = -1;
    while(!q.empty()){
        node now = q.front(); q.pop();
        for(int i = 0; i < 4; i++){
            int xx = now.x + dx[i];
			int yy = now.y + dy[i];
            if(xx <0 || xx >=3 || yy <0 || yy >= 3 ) continue;
            node next = now;
            swap(next.m[next.x][next.y],next.m[xx][yy]);
            next.ok = Cantor(next.m);
            if(vis[next.ok]) continue;

            vis[next.ok] = true;
            pre[next.ok] = now.ok;
            preMay[next.ok] = op[i];
            next.x = xx; next.y = yy;
            q.push(next);
        }
    }
}

void printRoad(int root){
    if(pre[root] == -1) return;
    printf("%c",preMay[root]);
    printRoad(pre[root]);
    
}

int main(){
    int cnt = 1;
    for(int i = 0; i < 3; i ++){
    	for(int j = 0; j < 3; j ++){
    		End.m[i][j] = cnt++;
		}
	}
    End.ok = Cantor(End.m);
    End.x = 2; End.y = 2;
    bfs();
     char str[20];
    while(gets(str)){
    	node Start;
        for(int i = 0,j = 0; j < 9 && str[i] != '\n'; i++){
            if(str[i] == ' ')continue;
            else if(str[i] == 'x') {
                Start.m[j/3][j%3] = 9;
                Start.x = j/3; Start.y = j%3;
            }
            else Start.m[j/3][j%3] = str[i]-'0';
            j++;
        }
        Start.ok = Cantor(Start.m);
        
        if(!vis[Start.ok]) printf("unsolvable\n");
        else{
            printRoad(Start.ok);
            printf("\n");
        }
    }
    return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值