【lightoj-1039】A Toy Company(BFS)
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)的更多相关文章
- 【LightOJ - 1205】Palindromic Numbers
[链接]https://cn.vjudge.net/problem/LightOJ-1205 [题意] 求出L..R范围内的回文个数 [题解] 数位DP; 先求出1..x里面的回文串个数.则做一下前缀 ...
- 【POJ 2251】Dungeon Master(bfs)
BUPT2017 wintertraining(16) #5 B POJ - 2251 题意 3维的地图,求从S到E的最短路径长度 题解 bfs 代码 #include <cstdio> ...
- 【HIHOCODER 1478】 水陆距离(BFS)
描述 给定一个N x M的01矩阵,其中1表示陆地,0表示水域.对于每一个位置,求出它距离最近的水域的距离是多少. 矩阵中每个位置与它上下左右相邻的格子距离为1. 输入 第一行包含两个整数,N和M. ...
- 【POJ - 2251】Dungeon Master (bfs+优先队列)
Dungeon Master Descriptions: You are trapped in a 3D dungeon and need to find the quickest way out! ...
- 【HDU - 2102】A计划(bfs)
-->A计划 Descriptions: 可怜的公主在一次次被魔王掳走一次次被骑士们救回来之后,而今,不幸的她再一次面临生命的考验.魔王已经发出消息说将在T时刻吃掉公主,因为他听信谣言说吃公主的 ...
- 【POJ - 3669】Meteor Shower(bfs)
-->Meteor Shower Descriptions: Bessie听说有场史无前例的流星雨即将来临:有谶言:陨星将落,徒留灰烬.为保生机,她誓将找寻安全之所(永避星坠之地).目前她正在平 ...
- 【HDU - 1043】Eight(反向bfs+康托展开)
Eight Descriptions: 简单介绍一下八数码问题:在一个3×3的九宫格上,填有1~8八个数字,空余一个位置,例如下图: 1 2 3 4 5 6 7 8 在上图中,由于右下角位置是空的 ...
- 【FZU - 2150】Fire Game(bfs)
--> Fire Game 直接写中文了 Descriptions: 两个熊孩子在n*m的平地上放火玩,#表示草,两个熊孩子分别选一个#格子点火,火可以向上向下向左向右在有草的格子蔓延,点火的地 ...
- 【POJ - 3126】Prime Path(bfs)
Prime Path 原文是English 这里直接上中文了 Descriptions: 给你两个四位的素数a,b.a可以改变某一位上的数字变成c,但只有当c也是四位的素数时才能进行这种改变.请你计算 ...
随机推荐
- 0408-服务注册与发现-Eureka常用配置
一.概述 参看地址:https://cloud.spring.io/spring-cloud-static/Edgware.SR3/single/spring-cloud.html#_appendix ...
- tensorflow 中 name_scope 及 variable_scope 的异同
Let's begin by a short introduction to variable sharing. It is a mechanism in TensorFlow that allows ...
- C#设置当前程序通过IE代理服务器上网
注意:以下设置只在当前程序中有效,对IE浏览器无效,且关闭程序后,自动释放代码. using System; using System.Collections.Generic; using Syste ...
- VGA显示
VGA控制器的编写主要是了解VGA的显示标准和时序,如1024X768@60Hz,确定时钟频率(65MHz=1344X806X60),列像素时间等于时钟周期,扫描从左到右.从上到下(类似于电视扫描PA ...
- Java读写.properties文件实例,解决中文乱码问题
package com.lxk.propertyFileTest; import java.io.*; import java.util.Properties; /** * 读写properties文 ...
- day8 一些字符转换和深浅拷贝 和枚举
# li =[11,22,33,44,55,66,77,88]# del li[-2::-5]# print(li)# # dic = {'k1':'barry','k2':'alex','name' ...
- s5_day8作业
# 1 整理今天装饰器代码(每人手写一份,注意,是手写,交到小组长手里,明天我检查),准备明天默写 # 2 编写日志装饰器,实现功能如:一旦函数f1执行,则将消息2017-07-21 11:12:11 ...
- service 需要注意的地方
@service标记的class,只能用于标记了@controller的类,用于其他的会出错 mybatis查询查询到返回记录,查询不到返回null
- python之路 面向对象基础 XML
一.面向对象基础 1.类(Class): 用来描述具有相同的属性和方法的对象的集合.它定义了该集合中每个对象所共有的属性和方法.对象是类的实例. 类变量:类变量在整个实例化的对象中是公用的.类变量定义 ...
- JAVA发送HttpClient
http://bijian1013.iteye.com/blog/2310211 在发送HTTP请求的时候会使用到POST和GET两种方式,如果是传送普通的表单数据,我们直接将参数到一个Key-val ...