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. Perceptual Generative Adversarial Networks for Small Object Detection

    Perceptual Generative Adversarial Networks for Small Object Detection 感知生成对抗网络用于目标检测 论文链接:https://ar ...

  2. Android开发之事件和事件监听器

    写了一个打飞机的小程序,用于作为事件监听的学习,此程序须要有实体按键的手机才干运行. PlaneView.java: public class PlaneView extends View{ publ ...

  3. 【Sql Server】—sql Servler登录失败

    登录失败报错信息如下: 标题: 连接到服务器 ------------------------------ 无法连接到 localhost. ----------------------------- ...

  4. MariaDB复制架构中应该注意的问题

    一.复制架构中应该注意的问题: 1.限制从服务器只读 在从服务器上设置read_only=ON,此限制对拥有SUPPER权限的用户均无效: 阻止所有用户(在从服务器执行一下命令并保持此线程,也就是执行 ...

  5. python16_day17【Django_session、ajax】

    一.Session 1.settings.py SESSION_ENGINE = 'django.contrib.sessions.backends.db' # 引擎(默认) SESSION_COOK ...

  6. ImageMagick来处理图片,缩放,调整高度等操作

    单个缩放图片 convert 911.jpg -resize 25% 911.jpg 前面是要处理的图片路径,后面是输出的图片路径,我这么写就把原先图片缩放了 批量缩放图片 mogrify -samp ...

  7. linux网络基础设置 以及 软件安装

    ifconfig #查看所有已激活的网卡信息 临时配置 #yum install net-tools -y 默认ifconfig是没有安装的,可能需要安装 ifconfig eth0 #查看单独一块网 ...

  8. xpath中遇到[<Element a at 0x39a9a80>](转)

    Element是什么 回归正题,大家晕头转脑的看完繁杂的语法之后,已经迫不及待写点什么东西了,然后部分同学可能遇到了这个 <Element a at 0x39a9a80>或者类似 Elem ...

  9. PHP范例注册审核

    <body> <h1>注册</h1> <form action="zcchuli.php" method="post" ...

  10. mysql 系列文章推荐

    1. mysql日志详细解析     http://www.cnblogs.com/wangkongming/p/3684950.html 2. mysql 主从同步实验     http://pmg ...