|
楼主 |
发表于 2014-3-10 17:22
|
显示全部楼层
jsxjd 发表于 2014-3-7 14:45
本期越殂代疱,总结如下:
本题可以把单词的首末字母看作顶点(本题最多有26个顶点),把整个单词 ...
你们太厉害了,都比我的快,不过都比我的代码长,呵呵~- Option Explicit
- Option Base 1
- Public Function CheckLinkability() As Variant
- Dim t#: t = Timer
- On Error GoTo END_FUNC
- Dim aRes(1 To 3)
- Open ThisWorkbook.Path & "\单词接龙_测试数据.csv" For Input Access Read As #1
- ' ......
- Dim sLine$, nLine&, nWords&, iInd%, aWords, i&
- Line Input #1, sLine
- nLine = CLng(sLine)
- For i = 1 To nLine
- Input #1, nWords, iInd, sLine
- aWords = Split(sLine, ",")
- aRes(3) = aRes(3) & WordsLinkable(aWords)
- Next
- ' ......
- END_FUNC:
- Close #1
- aRes(1) = "Lee1892" ' <- 你的论坛ID
- aRes(2) = Timer - t
- If Err Then Err.Clear: aRes(2) = -1: On Error GoTo 0
- ' aRes(3) 为100个字母 T 或 F 的字符串,T 为能够接龙,F 为不能
- CheckLinkability = aRes
- End Function
- Private Function WordsLinkable(ByRef aWords) As String
- Dim sWrd, aDeg&(26), iFst%, iLst%, i%, J%, k%, bFlag As Boolean
- Dim aEdg(26, 26) As Boolean, aVtx(26) As Boolean
- For Each sWrd In aWords
- iFst = Asc(Left(sWrd, 1)) - 96
- iLst = Asc(Right(sWrd, 1)) - 96
- aDeg(iFst) = aDeg(iFst) + 1
- aDeg(iLst) = aDeg(iLst) - 1
- aEdg(iFst, iLst) = True
- aEdg(iLst, iFst) = True
- aVtx(iFst) = True
- aVtx(iLst) = True
- Next
- For k = 1 To 26
- For i = 1 To 26
- For J = 1 To 26
- aEdg(i, J) = aEdg(i, J) Or (aEdg(i, k) And aEdg(k, J))
- Next
- Next
- Next
- For i = 1 To 26
- For J = 1 To 26
- If aVtx(i) And aVtx(J) And (Not aEdg(i, J)) Then
- bFlag = False: GoTo EXIT_FUNC
- End If
- Next
- Next
- J = 0: k = 0
- For i = 1 To 26
- Select Case aDeg(i)
- Case -1
- If J > 0 Then bFlag = False: GoTo EXIT_FUNC
- J = i
- Case 1
- If k > 0 Then bFlag = False: GoTo EXIT_FUNC
- k = i
- Case Is <> 0
- bFlag = False: GoTo EXIT_FUNC
- End Select
- Next
- bFlag = (J = 0 And k = 0) Or (J > 0 And k > 0)
- EXIT_FUNC:
- WordsLinkable = IIf(bFlag, "T", "F")
- Erase aDeg: Erase aEdg: Erase aVtx
- End Function
复制代码 我这测试效果如下:- 答 题 人: Lee1892
- 计算用时: 11.5
- TTTFTFTFTTTTTFTFTFTTTTTFTFTFTTTTTFTFTFTTTTTFTFTFTTTTTFTFTFTTTTTFTFTFTTTTTFTFTFTTTTTFTFTFTTTTTFTFTFTT
- 答 题 人: jsxjd_1
- 计算用时: 10.28125
- 答案正确: True
- 答 题 人: jsxjd_2
- 计算用时: 6.11328125
- 答案正确: True
- 答 题 人: wcymiss
- 计算用时: 2.71484375
- 答案正确: True
复制代码 |
|