Connection Status:
Competition Arena > ThirteenHard
Member SRM 471 · 2009-12-03 · by espr1t · Dynamic Programming, Graph Theory
Class Name: ThirteenHard
Return Type: int
Method Name: calcTime
Arg Types: (vector<string>)
Problem Statement

Problem Statement

Despite being beautiful, funny and intelligent (or at least she thinks so), Elleonora has one great disadvantage – she is really superstitious. Although at first sight this seems to be a small thing, it influences great parts of her life. For example, planning an evening out with her friends is quite a challenge for Elly.

While travelling somewhere Elly counts the minutes passed since the time she left home and insists on arriving everywhere such that the time for any segment of her trip is not divisible by 13, since 13 is believed to be one of the most fatal numbers. Here, a segment of the trip refers to any consecutive sequence of transports used by Elly. This means that if her path is home -> station1 -> station2 -> destination with travel times home -> station1 = T1, station1 -> station2 = T2 and station2 -> destination = T3 then none of T1, T2, T3, T1 + T2, T2 + T3 and T1 + T2 + T3 should be divisible by 13.

For example, let the durations of the transports Elly uses be 7, 10 and 16 minutes, respectively. Then, the segment with transports 2 and 3 has a total duration of 10 + 16 = 26 minutes, which is divisible by thirteen and this will ruin her day. On the other hand if the segments of her travel were with durations 7, 14 and 16 minutes, then none of the numbers 7, 14, 16, 21, 30 or 37 would be divisible by thirteen, so this would be a valid path.

There are N stations numbered 0 to N-1 in the city where Elly lives. There are transports between some stations and they are described in String[] city containing exactly N elements. The j-th character in i-th element of city describes the duration of the transport that goes directly from station i to station j. Durations of 1, 2, ..., 26 are encoded with 'A', 'B', ..., 'Z', correspondingly, and durations of 27, 28, ..., 52 are encoded with 'a', 'b', ..., 'z', correspondingly. If there is no transport going directly from station i to station j, the corresponding character will be '#'. All transports are directed, so if there's a transport from station i to station j, there will not necessarily be a transport from station j to station i, and if there's such a transport, it will not necessarily be of the same duration. For each station, there can be a transport from this station to itself.

Elly wants to reach station N-1 (0-based) starting from station 0 and using one or more transports. More formally, her path is a sequence of stations S[0], S[1], ..., S[k], k ≥ 1, where S[0] = 0, S[k] = N-1 and for each i, 0 ≤ i < k, there's a direct transport from S[i] to S[i+1]. The same station can appear several times in her path. If T[i], 0 ≤ i < k, is the duration of the transport from S[i] to S[i+1] in minutes, then there must not be any indices i, j, 0 ≤ i ≤ j < k, such that T[i] + T[i+1] + ... + T[j] is divisible by 13.

Return the minimum time in which she can reach station N-1 while respecting her superstitious requirements. If this is not possible, return -1.

Constraints

  • city will contain exactly N elements, where N is between 2 and 25, inclusive.
  • Each element of city will contain exactly N characters.
  • Each element of city will contain only the characters 'a'-'z', 'A'-'Z' and '#'.
Examples
0)
{ "#AB##",
  "###A#",
  "###C#",
  "####K",
  "#####" }
Returns: 16

Here the shortest path is with length 13, but that makes it forbidden. The second shortest path is with length 16 and is OK.

1)
{ "#Z",
  "Z#" }
Returns: -1

Here the only path is impossible.

2)
{ "#A#C##",
  "##FA#K",
  "###D#D",
  "A###A#",
  "##C###",
  "####A#" }
Returns: 10
3)
{ "#AAA####", "####A###", "####B###", "####C###", "#####KJ#", "#######L", "#######A", "########" }
Returns: -1
4)
{ "Good#####",
  "#Luck####",
  "##and####",
  "##Have###",
  "####Fun##",
  "#####in##",
  "#####the#",
  "CHALLENGE",
  "##PHASE##" }
Returns: 137

Good luck and have fun in the challenge phase.

9)
{ "pQHMAlldFRVcGBkNDXfFzsTjR",
  "PgrnVsMCSYqeIeFzIKAVseZYx",
  "xLGPFdOexBOJvVBYPElpnlvVP",
  "AYHqqqmujOVOuwMNbCKfkaxkL",
  "JhiJwKuNxzzmMQOeLhKAgqcDu",
  "EoWYzVCPalVuFAvZltkcWrEJh",
  "iCbcFLitDvhclVyWBsmoxlCmX",
  "HMNEfXTggPYneCKpskJGGkDze",
  "PVgIpsdJZjLhbQKMZBWbDuFKR",
  "rEMDyJEHsQoiiFoEdPzNVMTtd",
  "QOMvpPUcaumEtLgPNDbPETJPR",
  "OzQoyjbADDpjxHoPWYhqhbFVN",
  "qrjXOvIxZZskUVjrMzOkieRAD",
  "gnUHxqJJWxmshLqZdtZTjnXgs",
  "SZNCvafmoHWbZUwjRmMHhRWBr",
  "BFBEhUiwbtebcXfxfCKFCCtGu",
  "mRBzShReWjxHuYpWEQriIfDuE",
  "OQuhenHIUzSUDBRStRCYaFVde",
  "TiUujeczbKdqIFBchhdGJllaA",
  "CRSidtCLFeJThCAbexxgeYhGc",
  "RDpMCoROPFhoqLfWKfDYmCsSo",
  "SDJHWBxmUFQaQClNyQQyWYbac",
  "kkKdiCyuFBSEygvdPAVFRrskT",
  "DHRIbzZjMYpaAWEJchrvjSdOT",
  "PnpYgIOqWwZQeOesxvIPLyMmu" }
Returns: 9

A lot of transports in this city.

14)
{ "###No#####",
  "####Zaphod",
  "#####Just#",
  "######very",
  "####very##",
  "improbable",
  "##########",
  "##########",
  "##########",
  "##########" }
Returns: 103

You can use random messages as challenges.

67)
{ "#A#A##A###A#####", "##M#############", "###############A", "####E###########", "#####H##########", "###############A", "#######C########", "########G#######", "#########C######", "###############A", "###########G####", "############G###", "#############G##", "##############G#", "###############A", "################" }
Returns: 30

Fourth shortest is possible.

68)
{ "#B#C##T#M",
  "##K######",
  "########A",
  "####R####",
  "#####U###",
  "########C",
  "#######H#",
  "########S",
  "#########" }
Returns: 47

Here we have four possible paths. The shortest one is 0 -> 8 and has length 13 which makes it impossible. The second shortest is 0 -> 1 -> 2 -> 8, but the segment 0 -> 1 -> 2 has length 13, which is impossible. The third one is 0 -> 3 -> 4 -> 5 -> 8, which has length 45, but the segment 3 -> 4 -> 5 is of length 39, which is divisible by 13 and makes it impossible. The fourth one 0 -> 6 -> 7 -> 8 is possible and has length 47.

Submissions are judged against all 97 archived test cases, of which 9 are shown here. Case numbers match the judge’s.

Coding Area

Language: C++17 · define a public class ThirteenHard with a public method int calcTime(vector<string> city) · 97 test cases · 2 s / 256 MB per case

Submitting as anonymous