版权声明:本文为博主Ticholas-Huang原创文章,转载请注明出处
Octagons
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 217 Accepted Submission(s): 82
Problem Description
Below is a picture of an infinite hyperbolic tessellation of octagons. If we think of this as a graph of vertices (of degree three), then there exists an isomorphism of the graph which maps any vertex x onto any other vertex y. Every edge is given a label from the set {a,b,c} in such a way that every vertex has all three types of edges incident on it, and the labels alternate around each octagon. Part of this labeling is illustrated in the diagram.
So a path in this graph (starting from any vertex) can be specified by a sequence of edge labels. Your job is to write a program which, given a squence of labels such as “abcbcbcabcaccabb”, returns “closed” if the path ends on the same vertex where it starts, and returns “open” otherwise.
Input
The input will begin with a number Z ≤ 200 on a line by itself. This is followed by Z lines, each of which is a squence of length at least 1 and at most 40 of ‘a’s ‘b’s and ‘c’s.
Output
For each input instance, the output will be the words “closed” or “open”, each on a single line.
Sample Input
2
abababab
abcbcbcbcba
Sample Output
closed
open
这道题题意就是有 一堆 八边形 每条边可以用a b c中任一个标记
相邻两条边的标记不同
每个八边形中只有两个标记
比如: babababa
或 cacacaca
输入一个字串
问能不能按字串的顺序来走八边形 最终回到起点
这道题其实就是字符串处理:
根据几何知识可以知道
规则1 abababa 可换成 b 八边形两个方向走 走7条边和走另一方向的1条边是一样的
规则2 ababab 可换成 ba 6条边 换 两条边
规则3 ababa 可换成 bab 5条边换3条边
规则4 bb 可去掉 一条边来回走
然后仔细点可以发现
只需要 规则3 和 规则4 就可以实现所有规则
不说了
上代码
/*author: Ticholas-Huang*/
/*2016.4.3*/
#include <iostream>
#include <string>
using namespace std;
bool transfer(string ss)
{
size_t size = ss.size();
if (size == 0)
return true;
for (int i = 0; i + 1 < size; i++)
{
if (ss[i] == ss[i + 1])
return transfer(ss.substr(0, i) + ss.substr(i + 2));
}
for (int i = 0; i + 4 < size; i++)
{
if (ss[i] == ss[i + 2] && ss[i] == ss[i + 4] && ss[i + 1] == ss[i + 3])
return transfer(ss.substr(0, i) + ss.substr(i + 1,3) + ss.substr(i + 5));
}
return false;
}
int main(int argc, char const *argv[])
{
int Case = 0;
cin >> Case;
string testStr;
while (Case)
{
cin >> testStr;
if (transfer(testStr))
{
cout << "closed" << endl;
}
else
{
cout << "open" << endl;
}
Case--;
}
return 0;
}