USACO Section 3.2 PROB Magic Squares
来源:互联网 发布:手机数据连接不到网络 编辑:程序博客网 时间:2024/04/28 12:15
题目:(摘自USACO官网)
Following the success of the magic cube, Mr. Rubik invented its planarversion, called magic squares. This is a sheet composed of 8 equal-sizedsquares:
In this task we consider the version where each square has a differentcolor. Colors are denoted by the first 8 positive integers. A sheetconfiguration is given by the sequence of colors obtained by readingthe colors of the squares starting at the upper left corner and goingin clockwise direction. For instance, the configuration of Figure3 is given by the sequence (1,2,3,4,5,6,7,8). This configurationis 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 tothe initial squares given above:
All possible configurations are available using the three basictransformations.
You are to write a program that computes a minimal sequence of basictransformations that transforms the initial configuration above to aspecific 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 lengthof the shortest transformation sequence.Line 2:The lexically earliest string of transformations expressedas a string of characters, 60 per line except possibly the last line.SAMPLE OUTPUT (file msquare.out)
7BCABCCB
分析:
因为只有8!种状态,故穷举即可。实际上状态间形成一个有向图,图中节点表示各个square,边表示变换。
由于是求最少次数的变换,故使用BFS搜索。
判重采用Hashtable最好了,但C++标准库中暂时没有,可以使用set。
代码:C++
ID: randyji1
PROG: msquare
LANG: C++
*/
#include <fstream>
#include <iostream>
#include <set>
#include <queue>
#include <string>
#include <cassert>
using namespace std;
ifstream fin("msquare.in");
ofstream fout("msquare.out");
unsigned int sq;
string trans;
Node() {}
Node(string t, unsigned int n) {trans = t; sq = n;}
};
unsigned int initState = 12345678;
unsigned int dest;
void getInput() {
dest = 0;
int tmp;
for(int i = 0; i < 8; i++) {
fin >> tmp;
dest = dest * 10 + tmp;
}
}
unsigned int transA(unsigned int orig) {
unsigned int rs = 0;
for(int i = 0; i < 8; i++) {
rs = rs * 10 + orig % 10;
orig /= 10;
}
return rs;
}
unsigned int transB(unsigned int orig) {
unsigned int rs = 0;
unsigned int upper4 = orig / 10000;
unsigned int lower4 = orig % 10000;
upper4 = upper4%10*1000+upper4/10;
lower4 = lower4/1000 + lower4%1000*10;
return upper4 * 10000 + lower4;
}
unsigned int transC(unsigned int orig) {
unsigned int rs = 0;
unsigned int digits[8];
digits[0] = orig / 10000000;
digits[3] = orig / 10000 % 10;
digits[4] = orig / 1000 % 10;
digits[7] = orig % 10;
digits[1] = orig / 10 % 10;
digits[2] = orig / 1000000 % 10;
digits[5] = orig / 100000 % 10;
digits[6] = orig / 100 % 10;
for(int i = 0; i < 8; i++)
rs = rs * 10 + digits[i];
return rs;
}
void printResult(string trans) {
fout << trans.size() << endl;
for(int i = 0; i < trans.size(); i++) {
fout << trans[i];
if((i + 1) % 60 == 0) fout << endl;
}
if((trans.size() % 60) != 0) fout << endl;
if(trans.size() == 0) fout << endl;
}
void process(unsigned int init, unsigned int des) {
set<unsigned int> sqSet;
queue<Node> st;
st.push(Node("", init));
sqSet.insert(init);
while(!st.empty()) {
Node node = st.front();
st.pop();
unsigned int current = node.sq;
//cout << current << endl;
if(current == des) {
printResult(node.trans);
return;
}
unsigned int t;
t = transA(current);
if(sqSet.count(t) == 0) {
st.push(Node(node.trans + 'A', t));
sqSet.insert(t);
}
t = transB(current);
if(sqSet.count(t) == 0) {
st.push(Node(node.trans + 'B', t));
sqSet.insert(t);
}
t = transC(current);
if(sqSet.count(t) == 0) {
st.push(Node(node.trans + 'C', t));
sqSet.insert(t);
}
}
//assert(false);
}
int main() {
assert(fin && fout);
getInput();
process(initState, dest);
return 0;
}
- USACO Section 3.2 PROB Magic Squares
- USACO-Section 3.2-PROB Magic Squares
- usaco Chapter 3 section 3.2 Magic Squares
- USACO-Section 3.2 Magic Squares(BFS)
- USACO section 3.2 Magic Squares(STL+广搜)
- USACO Section 3.2 PROB Stringsobits
- USACO-Section 3.2-PROB Factorials
- USACO-Section 3.2-PROB Stringsobits
- usaco 3.2 Magic Squares
- usaco 3.2 Magic Squares
- usaco 3.2 Magic Squares
- USACO Section 3.2 PROB Spinning Wheels
- USACO-Section 3.2-PROB Spinning Wheels
- USACO 3.2.5 Magic Squares
- USACO 3.2 Magic Squares (msquare)
- USACO 3.2 Magic Squares 魔板
- USACO Section 3.2 Magic Squares - 我承认我是瞎混过去的..
- USACO-Section 3.1-PROB Contact
- 编程匠艺(三)名正言顺
- MCU应用中的矩阵键盘结构及原理
- 面向对象编程(OOP)、面向组件编程(COP)、面向方面编程(AOP)和面向服务编程(SOP)
- 如何移植uCOS-III到Linux系统 How to Port uCOS-III on Linux OS
- c#多层架构
- USACO Section 3.2 PROB Magic Squares
- 有些事,在开始的刹那就注定了最终的结局。
- javascript基础知识
- dreamweaver随记----关于网页状态栏
- Linux内核实时操作性能有望将大幅提高
- 解决Response中文乱码
- Android杀毒实现原理及实例
- 从"RTOS移植"迁移到"实时Linux"
- Oracle 10g 中 X$KCVFH 说明