`
Simone_chou
  • 浏览: 192638 次
  • 性别: Icon_minigender_2
  • 来自: 广州
社区版块
存档分类
最新评论

Magic Squares(BFS + string)

 
阅读更多
Magic Squares
IOI'96

Following the success of the magic cube, Mr. Rubik invented its planar version, called magic squares. This is a sheet composed of 8 equal-sized squares:

1 2 3 4
8 7 6 5

In this task we consider the version where each square has a different color. Colors are denoted by the first 8 positive integers. A sheet configuration is given by the sequence of colors obtained by reading the colors of the squares starting at the upper left corner and going in clockwise direction. For instance, the configuration of Figure 3 is given by the sequence (1,2,3,4,5,6,7,8). This configuration is the initial configuration.

Three basic transformations, identified by the letters `A', `B' and `C', can be applied to a sheet:

  • 'A': exchange the top and bottom row,
  • 'B': single right circular shifting of the rectangle,
  • 'C': single clockwise rotation of the middle four squares.

Below is a demonstration of applying the transformations to the initial squares given above:

A:
8 7 6 5
1 2 3 4
B:
4 1 2 3
5 8 7 6
C:
1 7 2 4
8 6 3 5

All possible configurations are available using the three basic transformations.

You are to write a program that computes a minimal sequence of basic transformations that transforms the initial configuration above to a specific target configuration.

PROGRAM NAME: msquare

INPUT FORMAT

A single line with eight space-separated integers (a permutation of (1..8)) that are the target configuration.

SAMPLE INPUT (file msquare.in)

2 6 8 4 5 7 3 1 

OUTPUT FORMAT

Line 1: A single integer that is the length of the shortest transformation sequence.
Line 2: The lexically earliest string of transformations expressed as a string of characters, 60 per line except possibly the last line.

SAMPLE OUTPUT (file msquare.out)

7
BCABCCB

 

      题意:

      有一个 2 X 4 的矩阵,排列方式如图所示,后给出 A,B,C 三种操作,问经过最少多少次的变换能得到所输入的序列,输出步数还有每一个步骤。

 

      思路:

      BFS + string。string 保存这个矩阵的情况,map 标记状态是否出现过。最后走一遍 BFS 即可。

 

      AC:

/*
ID:sum-g1
LANG:C++
PROG:msquare
*/

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <string>
#include <iostream>
#include <map>

using namespace std;

typedef struct {
    string s, way;
    int ans;
} node;

string fin;
map<string, bool> m;

string A (string a) {
    for (int i = 0, j = 3; i < 4; ++i, --j) {
        char t;
        t = a[i];
        a[i] = a[4 + j];
        a[4 + j] = t;
    }
    return a;
}

string B (string a) {
    int ans = 0;
    char t = a[3], k = a[4];
    for (int i = 3, j = 0; i > 0; --i, ++j) {
        a[i] = a[i - 1];
        a[4 + j] = a[4 + j + 1];
    }
    a[0] = t;
    a[7] = k;
    return a;
}

string C (string a) {
    char c = a[2];
    a[2] = a[1];
    a[1] = a[6];
    a[6] = a[5];
    a[5] = c;
    return a;
}

void bfs() {
    node k;
    queue<node> q;
    k.s = "12345678";
    k.way = "";
    k.ans = 0;
    m[k.s] = 1;
    q.push(k);

    while (!q.empty()) {
        node x = q.front(); q.pop();

        if (fin == x.s) {
            cout << x.ans << endl;
            cout << x.way << endl;
            return;
        }

        node y;
        y.ans = x.ans + 1;
        y.s = A(x.s);
        if (!m[y.s]) {
            m[y.s] = 1;
            y.way = x.way + 'A';
            q.push(y);
        }

        y.s = B(x.s);
        if (!m[y.s]) {
            m[y.s] = 1;
            y.way = x.way + 'B';
            q.push(y);
        }

        y.s = C(x.s);
        if (!m[y.s]) {
            m[y.s] = 1;
            y.way = x.way + 'C';
            q.push(y);
        }
    }
}

int main() {

    freopen("msquare.in","r",stdin);
    freopen("msquare.out","w",stdout);

    for (int i = 1; i <= 8; ++i) {
        char c;
        scanf(" %c", &c);
        fin += c;
    }

    bfs();

    return 0;
}

 

 

分享到:
评论

相关推荐

    魔方和立方体(威廉·西姆斯·安德鲁斯)Magic Squares and Cubes (William Symes Andrews)

    本书涵盖诸如魔术方格,魔术方格,富兰克林方格,魔术和毕达哥拉斯数字,复归理论,魔术圆,球体和星星以及魔术八面体等主题。

    通用最小二乘回归Matlab代码General+Least+Squares+Regression

    通用最小二乘回归(Generalized Least Squares Regression, GLS)是一种统计分析方法,用于解决线性回归模型中因变量与自变量之间的关系受到非恒定方差或相关性影响的问题。在Matlab中实现GLS,我们可以利用矩阵代数...

    USACO-Magic-Squares.zip_magic

    在这个问题中,我们关注的是“Magic Squares”,这是一个经典的数学概念,与C++编程相结合,构成了一个有趣的挑战。 魔法方阵是一个n×n的矩阵,其中每个单元格填充了从1到n²的不重复整数,并且每一行、每一列以及...

    Magic Squares-crx插件

    语言:English 一个易于使用和leqity魔法广场游戏 魔法方块是您的浏览器的乐趣和... 如果您有一个功能请求或找到一个要报告的错误,请填写Add-On的主页中的错误报告表单:https://mybrowseraddon.com/magic-squares.html

    POJ2002-Squares

    【标题】"POJ2002-Squares"是一个经典的计算机编程题目,源自北京大学的在线判题系统(POJ,即PKU Online Judge)。这个题目主要涉及到算法设计和实现,尤其是数学和动态规划方面的知识。 【描述】"解题报告+AC代码...

    Water Retention on Magic Squares Solver:基于约束的局部搜索在魔方上的保水-开源

    基于约束的局部搜索求解器,用于Magic Squares问题上的保水。 该问题是由Craig Knecht发明的非常困难的组合优化问题。 有关此问题的更多信息,请访问:...

    least squares_递推最小二乘法_matlab_leastsquares_遗忘因子最小二乘法_最小二乘法_

    最小二乘法(Least Squares)是解决线性回归问题的一种常见方法,广泛应用于数据分析、信号处理和工程领域。在给定的标题和描述中,我们聚焦于几个关键概念:递推最小二乘法(Recursive Least Squares, RLS)、...

    USACO题目Palindromic Squares(回文平方数)及代码解析

    USACO题目Palindromic Squares(回文平方数)及代码解析 在计算机科学和信息学中,回文数(Palindromic Number)是一种数字,它从左向右念和从右向左念都一样。例如,12321是一个典型的回文数。给定一个进制B(2,...

    Burrus C S. Iterative re-weighted least squares.pdf

    迭代(重)加权最小二乘 论文 Describes a powerful optimization algorithm which iteratively solves a weighted least squares approximation problem in order to solve an L_p approximation problem

    squares.zip_squares_zip

    squares of integers program

    Least-squares estimation of transformation parameters between tw

    Least-squares estimation of transformation parameters between two point patterns。 相似变换相当于等距变换和均匀缩放的一个复合,即为: 左上角2*2矩阵为旋转部分,右上角为平移因子。它有四个自由度,即旋转...

    POJ3432-Count Squares

    【标题】"POJ3432-Count Squares"是一个经典的计算机编程竞赛题目,源自北京大学的在线评测系统POJ(PKU Online Judge)。这个题目主要涉及到算法设计和实现,尤其是几何计算和动态规划(Dynamic Programming)的...

    vtks_squares

    标题“vtks_squares”可能指的是一个特定的字体库或者设计项目,其中包含了以方形元素为基础的图形或字体样式。这种命名通常暗示了这个资源集可能是由一系列具有方形特征的图形或文字组成的,可能适用于图形设计、...

    Least Squares c code_C-C++_

    【标题】"Least Squares c code_C-C++_" 指的是一个使用 C 或 C++ 编程语言实现的最小二乘法(Least Squares Method)代码库。最小二乘法是一种广泛应用的数据拟合方法,它通过最小化误差平方和来寻找一组数据的最佳...

    magic-squares-encryption:对称加密知识的实际应用

    magic-squares-encryption:对称加密知识的实际应用

    the total least squares problem computational aspect and analysis

    整体最小二乘问题(The Total Least Squares Problem)是统计学与数值分析领域中的一个重要概念,尤其是在处理具有误差的数据集时尤为关键。本文将基于提供的标题、描述、标签以及部分内容,深入探讨整体最小二乘的...

    LEAST SQUARES SUPPORT VECTOR MACHINES

    This book focuses on Least Squares Support Vector Machines (LS-SVMs) which are reformulations to standard SVMs. LS-SVMs are closely related to regularization networks and Gaussian processes but ...

    偏最小二乘partial least squares(PLS)matlab代码

    偏最小二乘partial least squares(PLS)的matlab代码实现,整理为matlab函数,方便实用。

    libPLS for Partial Least Squares总算法matlab代码_matlab

    资源名:libPLS for Partial Least Squares总算法matlab代码_matlab 资源类型:matlab项目全套源码 源码说明: 全部项目源码都是经过测试校正后百分百成功运行的,如果您下载后不能运行可联系我进行指导或者更换。 ...

Global site tag (gtag.js) - Google Analytics