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. DjangoDRF序列化组件使用

    创建一个Django项目,名字:untitled1 创建三张表 from django.db import models class Publish(models.Model): nid = mode ...

  2. java.lang.instrument: 一个Java对象占用多少字节?

    一.对象头包括两部分信息:Mark Word(标记字段)和 Klass Pointer(类型指针)   1. Mark Word 用于存储对象自身的运行时数据,如哈希码(HashCode).GC分代年 ...

  3. Python3+Selenium3自动化测试-(二)

    python3 元素定位和操作方法总结 # coding=utf-8 ''' #8种元素定位方法 find_element_by_id() find_element_by_name() find_el ...

  4. 假设做一个精美的Login界面(攻克了一EditText自带clear的功能,相似iphone的UITextField)

    先上图:     XML为: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" ...

  5. java 多线程 day05 线程范围内的数据共享

    import java.util.HashMap;import java.util.Map;import java.util.Random;/** * Created by chengtao on 1 ...

  6. VS2015配置安卓Android和iOS开发环境

    http://www.cjjjs.cn/paper/gzsh/627201502818357.aspx [摘要] 本文按照步骤一步步的介绍要下载安装的东西,都提供了下载地址.最后将所有需要的程序都打包 ...

  7. [笔记]Go语言实现同一结构体适配多种消息源

    问题: 提供天气信息的网站有很多,每家的数据及格式都不同,为了适配各种不同的天气接口,写了如下程序. 代码如下: package main import ( "encoding/json&q ...

  8. curl类封装

    <?php /**   * @author askwei **/   class CURL   {      private $ch;      private $url = "htt ...

  9. 针对Oracle表 列字段的增加、删除、修改以及重命名操作sql

    增加字段语法:alter table tablename add (column datatype [default value][null/not null],….); 说明:alter table ...

  10. sql中1=1和1=0的用处

    where 1=1 where 1=1有什么用?在SQL语言中,写这么一句话就跟没写一样. select * from table1 where 1=1与select * from table1完全没 ...