Description

Fengshui is an ancient subject in Chinese tradition. Someone considers it as science and someone criticizes it as blind faith. Who knows! However, in modern days, everyone should respect culture from our ancestor!
Fengshui focus on geography,environment and staffs' position, all the theory come from a very old book named "YI". YI means change. Everything is always changing in the world. Fengshui wishes to guide changing, make life change to a better situation. Now let's look at Fengshui's changing.
At first we must know about the traditional five elements system which composed by GOLD,WOOD,GROUND,WATER and FIRE. Everything in the world can be represented by one and only one element. For example, river is represented by WATER, hill is represented by GROUND. Here, we only consider the elements. In this system, once element can kill another element, and one element can born anther element. Five elements compose as a circuit, as in Figure 1.

Every place has eight direction - east, west, north, south, northeast, northwest, southeast and southwest. Every direction has a represented element. Now, our problem is about the elements at these eight directions which form a Fengshui situation. Figure 2 is an example of one Fengshui situation.
But Fengshui situation can change! There're two change ways:
TURN: The whole situation turn clockwise one step. Figure 3 shows the situation that situation in Figure 2 makes one TURN change.
REBORN: Based on kill and born relation, one direction's element can be killed by another direction's (at any other place) element in the situation, and then the killed element will born out as the new element at its direction. Of course, kill and born are all according as the relation of the system as in Figure 1. In situation of Figure 3, WATER in east can kill FIRE in southeast, then southeast place change to be GROUND, as in Figure 4.

Each change, no matter TURN or REBORN, const one step.
Now, there're two Fengshui situation, we want to know is it possible that first one can change to the second one? And if possible, how many steps it need at least?
Input

There're several cases, the first line of input is the number of cases. Every case includes 6 lines, the first 3 lines indeicate the first Fengshui situation, the last 3 lines incicate the second Fengshui situation.
The format of one situation is as follow, there may be arbitrary blanks between adjacent directions.
northwest  north  northeast
west                   east
southwest  south  southeast
Output

For every case, output the number of the least changing steps on a single line, if it is possible, or output -1.
Sample Input

2
GOLD WOOD WATER
WATER     FIRE
WOOD GOLD GROUND
WATER GOLD WOOD
WOOD       WATER
GOLD GROUND GROUND
WATER GROUND WOOD
GOLD         FIRE
GOLD  FIRE   GROUND
GOLD  FIRE   FIRE
GOLD         FIRE
WATER GROUND WOOD

Sample Output

2
14

This problem seems like a kind of for beginners.The time limit is up to 30 seconds.Or maybe it wasn't in the competition where it from.So beginners like me can pass this question through the most simple search.
The second operate reborn means for a element in some direction,if there exist a element which is also one of the eight directions and can kill it ,it can execute the reborn operate.

/*Gold    0
Wood 1
Water 2
Fire 3
Ground 4*/
/*
012
7 3
654
01234567
701
6 2
543
70123456
*/
#include<stdio.h>
#include<string.h>
#include<queue>
#include<iostream>
using namespace std;
struct node
{
int num,step;
};
const int pow5[10]={1,5,25,125,625,3125,15625,78125,390625,1953125};
const int kill[5] ={1,4,3,0,2};
const int born[5] ={2,3,1,4,0};
bool f[500000];
int map[8],S,T;
int bfs()
{
int i,j,k;
memset(f,0,sizeof(f));
f[S]=true;
queue<node> q;
while (!q.empty()) q.pop();
node tmp;
tmp.num=S;
tmp.step=0;
q.push(tmp);
while (!q.empty())
{
node x=q.front();
q.pop();
if (x.num==T) return x.step;
tmp=x;
tmp.step++;
int fr=tmp.num%5;
tmp.num=(tmp.num/5)+fr*pow5[7];
if (!f[tmp.num])
{
f[tmp.num]=true;
q.push(tmp);
}
tmp.num=x.num;
for (i=7;i>=0;i--)
{
map[i]=tmp.num%5;
tmp.num/=5;
}
for (i=0;i<8;i++)
{
bool flag=false;
for (j=0;j<8;j++)
if (kill[map[j]]==map[i])
{
int tt=map[i];
map[i]=born[map[i]];
tmp.num=0;
for (k=0;k<8;k++)
tmp.num=tmp.num*5+map[k];
if (!f[tmp.num])
{
f[tmp.num]=true;
q.push(tmp);
}
map[i]=tt;
break;
}
}
}
return -1;
}
int main()
{
int C,i;
scanf("%d",&C);
char s[8][15],t[8][15];
while (C--)
{
scanf("%s%s%s%s%s%s%s%s",s[0],s[1],s[2],s[7],s[3],s[6],s[5],s[4]);
scanf("%s%s%s%s%s%s%s%s",t[0],t[1],t[2],t[7],t[3],t[6],t[5],t[4]);
S=0,T=0;
for (i=0;i<8;i++)
{
if (s[i][0]=='G' && s[i][1]=='O') S=S*5+0;
if (s[i][0]=='W' && s[i][1]=='O') S=S*5+1;
if (s[i][0]=='W' && s[i][1]=='A') S=S*5+2;
if (s[i][0]=='F' && s[i][1]=='I') S=S*5+3;
if (s[i][0]=='G' && s[i][1]=='R') S=S*5+4;
}
for (i=0;i<8;i++)
{
if (t[i][0]=='G' && t[i][1]=='O') T=T*5+0;
if (t[i][0]=='W' && t[i][1]=='O') T=T*5+1;
if (t[i][0]=='W' && t[i][1]=='A') T=T*5+2;
if (t[i][0]=='F' && t[i][1]=='I') T=T*5+3;
if (t[i][0]=='G' && t[i][1]=='R') T=T*5+4;
}
printf("%d\n",bfs());
}
return 0;
}

Fengshui-[SZU_B40]的更多相关文章

  1. CVE-2015-7645 analyze and exploit

    Hack team之后adobe和google合作对flash进行了大改,一度提高了flash的利用门槛,CVE-2015-7645作为第一个突破这些限制的漏洞利用方式,可以作为vetect利用方式之 ...

  2. [2014-09-21]如何在 Asp.net Mvc 开发过程中更好的使用Enum

    场景描述 在web开发过程中,有时候需要根据Enum类型生成下拉菜单: 有时候在输出枚举类型的时候,又希望输出对应的更具描述性的字符串. 喜欢直接用中文的请无视本文 不多说,直接看代码. 以下代码借鉴 ...

  3. [web]2019第一起数据泄露事件

    -rwxrwxrwx 33405108 Jan 22 2016 000webhost.txt -rwxrwxrwx 165025 Jul 29 2017 01nii.ru {1.931} [HASH] ...

  4. webug4.0 打靶笔记-02【完结】

    webug4.0打靶笔记-02 3. 延时注入(时间盲注) 3.1 访问靶场 3.2 寻找注入点 貌似一样的注入点: ?id=1' --+ 3.3 判断输出位置 同前两关一样的位置,时间盲注应该不是这 ...

随机推荐

  1. python学习之操作mysql

    欢迎点击个人博客 http://www.iwangzheng.com/ 刚开始学python,所以很多代码都需要在ipython里尝试一下.今天记录的是最基本的操作mysql数据库. 写数据库连接操作 ...

  2. vim中的查找和替换

    (文章是从我的个人主页上粘贴过来的,大家也可以访问我的主页 www.iwangzheng.com) 查找: Gsearch -F 'aa' -R  --include=*rb 替换: (1)在查找结果 ...

  3. [POJ1003]Hangover

    [POJ1003]Hangover 试题描述 How far can you make a stack of cards overhang a table? If you have one card, ...

  4. 影像工作站的数据库安装错误之Win7系统下pg服务无法启动

    1.关闭批处理 2.修改 PG安装路径下的Data文件下的pg_hba.conf文件中去掉IPv6的井号,如下图 3.结束pg进程 4.重启PG服务.

  5. HDOJ 1102 生成树

    Constructing Roads Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  6. 【转】Maven最佳实践:划分模块

    转自:http://juvenshun.iteye.com/blog/305865 “分天下为三十六郡,郡置守,尉,监” —— <史记·秦始皇本纪> 所有用Maven管理的真实的项目都应该 ...

  7. Expected MultipartHttpServletRequest: is a MultipartResolver configured?

    2015-05-05 19:09:47.510::WARN: /purchase/long-term-contract/uploading.htmjava.lang.IllegalArgumentEx ...

  8. iOS NSURLConnection和异步网络请求

    在日常应用中,我们往往使用AFNetworking等第三方库来实现网络请求部分.这篇文章会简要地介绍一下如何使用NSURLConnection来进行异步的网络请求. 我们先看一个小demo - (vo ...

  9. 简单的2d图形变换--仿设变换AffineTransform

    在ios中常常遇到些小的动画效果,比如点击一个按钮后,按钮上的三角形图片就旋转了.这种简单的小动画,常常通过更改view的transform属性来实现.这个transform属性,就是一个仿射变化矩阵 ...

  10. (转)SQL Server 的事务和锁(二)-Range S-S锁

    在这篇随笔中,我们的主要关注点在 Key-Range Lock.Key-Range Lock有 S-S.S-U.I-N.X-X几种情况.我们一个一个来说,力求明白.遗憾的是,这里可能会比较冗长,那么死 ...