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. vim 设置 颜色值

    编辑~/.vimrc文件,添加 set t_Co=8 t_Co即terminal Color之意 注意,将 t_Co 设置为256 (或8以外的所有值) 时,mark 的显示不是很正常.

  2. StringBuilder String string.Concat 字符串拼接速度

    首先看测试代码: public class StringSpeedTest { "; public string StringAdd(int count) { string str = st ...

  3. Linux用户、群组及权限

    由于对文件的操作需要切换到相应文件夹下进行,所以对文件内容的修改,最基本的是需要其文件夹执行的权限. 文件夹的读权限(read)可以独立行使,但是对文件夹内容的写权限(对其内文件的新建.删除.重命名) ...

  4. 【leetcode刷题笔记】Convert Sorted Array to Binary Search Tree

    Given an array where elements are sorted in ascending order, convert it to a height balanced BST. 题解 ...

  5. 一年java程序员的感悟

    前沿 在小公司干了差不多一年,刚进来与一个中级程序员做交接,过了大概一个月,那个中级程序员走了,从此,走上了"泥泞"的道路(独立开发),熟悉了公司的项目和业务用了一个月左右,公司当 ...

  6. 20145222 黄亚奇 《网络对抗》Exp8 Web基础

    20145222 黄亚奇 <网络对抗>Exp8 Web基础 实践具体要求 (1).Web前端HTML(1分) 能正常安装.启停Apache.理解HTML,理解表单,理解GET与POST方法 ...

  7. D3学习之动画和变换

    D3学习之动画和变换 ##(17.02.27-02.28) 主要学习到了D3对动画和缓动函数的一些应用,结合前面的选择器.监听事件.自定义插值器等,拓展了动画的效果和样式. 主要内容 单元素动画 多元 ...

  8. 总结一下TODO的用法

      1.设置任务的标签 WINDOW->preference->java->complier->task tags加一个 DONE:NORMAL表示已经完成的任务2. java ...

  9. mysql分库分表(一)

    mysql分库分表 参考: https://blog.csdn.net/xlgen157387/article/details/53976153 https://blog.csdn.net/cleve ...

  10. eclipse部署的web项目没有添加到Tomcat的webapps目录下解决方法

    eclipse没有像myeclipse那样,添加web项目时会自动部署到Tomcat的webapps目录下. 而是部署到了eclipse的.metadata\.plugins\org.eclipse. ...