B. Fox And Two Dots
time limit per test
memory limit per test
input
output
n × m
Each cell contains a dot that has some color. We will use different uppercase Latin characters to express different colors.
d1, d2, ..., dk a cycle
- kdots are different: ifi≠jthendiis different fromdj.
- k
- All dots belong to the same color.
- 1 ≤i≤k- 1:dianddi+ 1 are adjacent. Also,dkandd1 should also be adjacent. Cellsxandy
cycle
Input
n and m (2 ≤ n, m ≤ 50): the number of rows and columns of the board.
n lines follow, each line contains a string consisting of m
Output
Yes" if there exists a cycle, and "No" otherwise.
Sample test(s)
input
3 4 AAAA ABCA AAAA
output
Yes
input
3 4 AAAA ABCA AADA
output
No
input
4 4 YYYR BYBY BBBY BBBY
output
Yes
input
7 6 AAAAAB ABBBAB ABAAAB ABABBB ABAAAB ABBBAB AAAAAB
output
Yes
input
2 13 ABCDEFGHIJKLM NOPQRSTUVWXYZ
output
No
Note
A' form a cycle.
In second sample there is no such cycle.
Y' = Yellow, 'B' = Blue, 'R' = Red).
对于每个‘颜色连通块’当成树dfs(不走父边),观察是否有走向已走过点的边