The toy company "Babies Toys" has hired you to help develop educational toys. The current project is a word toy that displays three letters at all times. Below each letter are two buttons that cause the letter above to change to the previous or next letter in alphabetical order. So, with one click of a button the letter 'c' can be changed to a 'b' or a 'd'. The alphabet is circular, so for example an 'a' can become a 'z' or a 'b' with one click.

In order to test the toy, you would like to know if a word can be reached from some starting word, given one or more constraints. A constraint defines a set of forbidden words that can never be displayed by the toy. Each constraint is formatted like "X X X", where each X is a string of lowercase letters. A word is defined by a constraint if the ith letter of the word is contained in the ith X of the constraint. For example, the constraint "lf a tc" defines the words "lat", "fat", "lac" and "fac".

You will be given a string start, a string finish, and some forbidden strings. Calculate and return the minimum number of button presses required for the toy to show the word finish if the toy was originally showing the word start. Remember, the toy must never show a forbidden word. If it is impossible for the toy to ever show the desired word, return -1.

Input

Input starts with an integer T (≤ 50), denoting the number of test cases.

Each case begins with a blank line and two strings in two lines, start and finish both having exactly three characters each. The next line contains an integer n (1 ≤ n ≤ 50) denoting the number of forbidden words. Each of the next n lines will contain three strings each, separated by a single space. Each string (all the three strings) in a line will contain only distinct letters. Remember that start or finish can be forbidden. You can assume that all the characters are lowercase.

Output

For each case of input you have to print the case number and desired result.

Sample Input

Output for Sample Input

3

aab

zna

8

a a a

a a z

a z a

z a a

a z z

z a z

z z a

z z z

aaa

aaa

0

aab

nnn

1

a a ab

Case 1: 15

Case 2: 0

Case 3: -1

【题意】

给两个字符串和几个限制,求出第一个变成第二个字符串所需要的最小步数,不能输出-1.

【代码】

#include <bits/stdc++.h>
using namespace std;
char s[], e[], s1[], s2[], s3[];
bool vis[][][], flag;
struct node
{
int a, b, c, step;
};
node S, E;
int bfs()
{
if(flag) return -;
queue<node>Q;
S.step = ;
Q.push(S);
vis[S.a][S.b][S.c] = true;
int m;
while(!Q.empty())
{
node t = Q.front();
Q.pop();
if(t.a == E.a && t.b == E.b && t.c == E.c) return t.step;
m = (t.a+)%;
if(!vis[m][t.b][t.c])
{
node tt;
tt.a = m, tt.b = t.b, tt.c = t.c;
tt.step = t.step+;
vis[m][t.b][t.c] = ;
Q.push(tt);
}
m = (t.a+-)%;
if(!vis[m][t.b][t.c])
{
node tt;
tt.a = m, tt.b = t.b, tt.c = t.c;
tt.step = t.step+;
vis[m][t.b][t.c] = ;
Q.push(tt);
}
m = (t.b+)%;
if(!vis[t.a][m][t.c])
{
node tt;
tt.a = t.a, tt.b = m, tt.c = t.c;
tt.step = t.step+;
vis[t.a][m][t.c] = ;
Q.push(tt);
}
m = (t.b+-)%;
if(!vis[t.a][m][t.c])
{
node tt;
tt.a = t.a, tt.b = m, tt.c = t.c;
tt.step = t.step+;
vis[t.a][m][t.c] = ;
Q.push(tt);
}
m = (t.c+)%;
if(!vis[t.a][t.b][m])
{
node tt;
tt.a = t.a, tt.b = t.b, tt.c = m;
tt.step = t.step+;
vis[t.a][t.b][m] = ;
Q.push(tt);
}
m = (t.c+-)%;
if(!vis[t.a][t.b][m])
{
node tt;
tt.a = t.a, tt.b = t.b, tt.c = m;
tt.step = t.step+;
vis[t.a][t.b][m] = ;
Q.push(tt);
}
}
return -;
}
int main()
{
int t, n;
cin>>t;
int cas = ;
while(t--)
{
memset(vis, , sizeof vis);
scanf("%s%s%d", s, e, &n);
S.a = s[]-'a', S.b = s[]-'a', S.c = s[]-'a';
E.a = e[]-'a', E.b = e[]-'a', E.c = e[]-'a';
for(int i = ; i <= n; i++)
{
scanf("%s%s%s", s1, s2, s3);
int l1 = strlen(s1);
int l2 = strlen(s2);
int l3 = strlen(s3);
for(int j = ; j < l1; j++)
for(int k = ; k < l2; k++)
for(int l = ; l < l3; l++)
vis[s1[j]-'a'][s2[k]-'a'][s3[l]-'a'] = true;
}
flag = false;
if(vis[S.a][S.b][S.c] || vis[E.a][E.b][E.c]) flag = true;
printf("Case %d: %d\n", ++cas, bfs());
}
return ;
}

【lightoj-1039】A Toy Company(BFS)的更多相关文章

  1. 【LightOJ - 1205】Palindromic Numbers

    [链接]https://cn.vjudge.net/problem/LightOJ-1205 [题意] 求出L..R范围内的回文个数 [题解] 数位DP; 先求出1..x里面的回文串个数.则做一下前缀 ...

  2. 【POJ 2251】Dungeon Master(bfs)

    BUPT2017 wintertraining(16) #5 B POJ - 2251 题意 3维的地图,求从S到E的最短路径长度 题解 bfs 代码 #include <cstdio> ...

  3. 【HIHOCODER 1478】 水陆距离(BFS)

    描述 给定一个N x M的01矩阵,其中1表示陆地,0表示水域.对于每一个位置,求出它距离最近的水域的距离是多少. 矩阵中每个位置与它上下左右相邻的格子距离为1. 输入 第一行包含两个整数,N和M. ...

  4. 【POJ - 2251】Dungeon Master (bfs+优先队列)

    Dungeon Master  Descriptions: You are trapped in a 3D dungeon and need to find the quickest way out! ...

  5. 【HDU - 2102】A计划(bfs)

    -->A计划 Descriptions: 可怜的公主在一次次被魔王掳走一次次被骑士们救回来之后,而今,不幸的她再一次面临生命的考验.魔王已经发出消息说将在T时刻吃掉公主,因为他听信谣言说吃公主的 ...

  6. 【POJ - 3669】Meteor Shower(bfs)

    -->Meteor Shower Descriptions: Bessie听说有场史无前例的流星雨即将来临:有谶言:陨星将落,徒留灰烬.为保生机,她誓将找寻安全之所(永避星坠之地).目前她正在平 ...

  7. 【HDU - 1043】Eight(反向bfs+康托展开)

    Eight Descriptions: 简单介绍一下八数码问题:在一个3×3的九宫格上,填有1~8八个数字,空余一个位置,例如下图: 1 2 3 4 5 6 7 8   在上图中,由于右下角位置是空的 ...

  8. 【FZU - 2150】Fire Game(bfs)

    --> Fire Game 直接写中文了 Descriptions: 两个熊孩子在n*m的平地上放火玩,#表示草,两个熊孩子分别选一个#格子点火,火可以向上向下向左向右在有草的格子蔓延,点火的地 ...

  9. 【POJ - 3126】Prime Path(bfs)

    Prime Path 原文是English 这里直接上中文了 Descriptions: 给你两个四位的素数a,b.a可以改变某一位上的数字变成c,但只有当c也是四位的素数时才能进行这种改变.请你计算 ...

随机推荐

  1. CodeForces 215B Olympic Medal(数学啊)

    题目链接:http://codeforces.com/problemset/problem/215/B Description The World Programming Olympics Medal ...

  2. oracle 禁用索引

    同步数据的时候 有索引会比较慢 可以暂时禁用索引 --禁用索引 ALTER INDEX PK_T_AUTH_USERROLE_ID UNUSABLE; --恢复索引ALTER INDEX UK_T_A ...

  3. Android学习八---OpenCV JAVA API

    OpenCV java API的文档说明在OpenCV-2.4.10-android-sdk/sdk/java/javadoc/index.html的文件夹下. 想用java API的方式进行Open ...

  4. JVM虚拟机—JVM的类加载机制

    1 什么是类的加载 类的加载指的是将类的.class文件中的二进制数据读入到内存中,将其放在运行时数据区的方法区内,然后在堆区创建一个java.lang.Class对象,用来封装类在方法区内的数据结构 ...

  5. redis的数据类型与应用场景(二)

    1. 如何学习 redis有好多数据类型,有这么多数据类型,我们不可能每个都记得完完全全.但是我们必须知道它有哪些数据类型,每个数据类型是怎样的,有什么作用.redis的每一个数据类型都有一大堆命令, ...

  6. appium 中部分 api 的使用方法

    使用的语言是java,appium的版本是1.3.4,java-client的版本是java-client-2.1.0,建议多参考java-client-2.1.0-javadoc. 1.使用Andr ...

  7. java基础知识框图

  8. python网络编程——IO多路复用之select

    1 IO多路复用的概念 原生socket客户端在与服务端建立连接时,即服务端调用accept方法时是阻塞的,同时服务端和客户端在收发数据(调用recv.send.sendall)时也是阻塞的.原生so ...

  9. 【Java Web】新手教程(转)

    转自:http://www.journaldev.com/1854/java-web-application-tutorial-for-beginners#web-server-client Web ...

  10. 存储库之mongodb,redis,mysql

    一.简介 MongoDB是一款强大.灵活.且易于扩展的通用型数据库 MongoDB 是由C++语言编写的,是一个基于分布式文件存储的开源数据库系统. 在高负载的情况下,添加更多的节点,可以保证服务器性 ...