USACO-Section 3.2-PROB Magic Squares

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

搜索题。注意对string和map的运用
要给string的某一位赋值,前提是它已经被赋值

#include<cstdio>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<map>
#include<queue>
#define name "msquare"
using namespace std;
map <string,int> num;
struct father
{
	int f;//是由谁变来的 
	char c;//变的方式 
}fa[1000000];
queue <string> Q;
string s,t;
char ABC[100000];
int ans,tot;
void print()
{
	int x=0,y=tot+1;
	while (y!=0)
	{
		ABC[++x]=fa[y].c;
		y=fa[y].f;
	}
	cout<<x<<endl;
	int tmp;
	for (int i=x;i>=1;i--)
	{
		tmp++;
	    cout<<ABC[i];
	    if (tmp==60) cout<<endl;
    }
	cout<<endl;
}
void bfs()
{
	int i,j;
	while (!Q.empty())
	{
		string now=Q.front();
		Q.pop();
		string tmp="00000000";
		for (i=0;i<=7;i++)
		tmp[i]=now[7-i];
		//cout<<tmp<<endl;
		if (string(t)==string(tmp)) {fa[tot+1].f=num[now];fa[tot+1].c='A';print();break;}
		if (num[tmp]==0) 
		{
			Q.push(tmp);
			++tot;
			num[tmp]=tot;
			fa[tot].f=num[now];
			fa[tot].c='A';
		}
		tmp[0]=now[3];tmp[7]=now[4];
		tmp[1]=now[0];tmp[2]=now[1];tmp[3]=now[2];
		tmp[6]=now[7];tmp[5]=now[6];tmp[4]=now[5];
		//cout<<tmp<<endl;
		if (string(t)==string(tmp)) {fa[tot+1].f=num[now];fa[tot+1].c='B';print();break;}
		if (num[tmp]==0) 
		{
			Q.push(tmp);
			++tot;
			num[tmp]=tot;
			fa[tot].f=num[now];
			fa[tot].c='B';
		}
		tmp=now;
		tmp[1]=now[6];tmp[2]=now[1];
		tmp[5]=now[2];tmp[6]=now[5];
		//cout<<tmp<<endl;
		if (string(t)==string(tmp)) {fa[tot+1].f=num[now];fa[tot+1].c='C';print();break;}
		if (num[tmp]==0) 
		{
			Q.push(tmp);
			++tot;
			num[tmp]=tot;
			fa[tot].f=num[now];
			fa[tot].c='C';
		}
	}
}
int main()
{
	freopen(name ".in","r",stdin);
	freopen(name ".out","w",stdout);
	int i,j;
	t="00000000";
	for (i=0;i<=7;i++)
	    cin>>t[i];
	//cout<<t;
    s="12345678";
    if (t=="12345678") {cout<<"0";cout<<endl<<endl;return 0;}
    num[s]=0;
    Q.push(s);
    bfs();
	return 0;
}


发布了20 篇原创文章 · 获赞 0 · 访问量 5208

猜你喜欢

转载自blog.csdn.net/SoYouTry/article/details/50594044
今日推荐