Connection Status:
Competition Arena > DungeonBuilder
SRM 193 · 2004-05-05 · by brett1479 · Graph Theory
Class Name: DungeonBuilder
Return Type: int
Method Name: howMany
Arg Types: (vector<string>, int, vector<int>, int)
Problem Statement

Problem Statement

Your randomized map builder has spit out the dungeon layout for your new computer-based role playing game. The map is complex, with each room leading to at most 8 other rooms. Each path out of a room is given a number, so the player can simply hit a digit (1-8) on the keyboard to select his path. The builder also picked one room to be the starting location, and a group of the rooms to be winning positions (this group may be empty). A player will traverse the dungeon making a sequence of moves (digit selections). When done he will click 'Finish'. If the final room happens to be one of the winning positions, the player wins. Otherwise, the player loses. In addition, if a player tries to take a nonexistent path out of a room, he immediately loses.

The first 20 or so maps you built were excellent, but after a while you realized that the builder was somewhat predictable. Something had to be done, but you didn't want to recode the entire program. You decided to let the builder create a map M. Then you would generate a random number R between 1 and 7 inclusive. Using R and M you would construct a new map N. A sequence of moves wins in N if and only if the same sequence used in M would result in the player winning and cause the player to enter winning positions exactly R times. If you repeatedly enter the same winning position, each time contributes to the total. If the starting location is a winning position, the number of winning positions entered will be 1 prior to the first move of the sequence.

Given the map M and the number R, return the size, in number of rooms, of the smallest map equivalent to N. Two maps are equivalent if and only if every sequence of moves will produce the same outcome in both maps. In other words, two maps are different if and only if there exists some sequence of moves that will win on one map and lose on the other. A winning sequence of moves in a map K must begin at the starting location of K and end at one of the winning positions of K.

Element i of M will describe the paths leaving room i. It will contain precisely 8 terms separated by single spaces. Each term will either be X or some number j. If the kth number of element i of M is j, then the kth path out of room i leads to room j. X denotes no path exists corresponding to that number. For example, if
	M = { "0 0 1 1 X X X X",
              "2 1 0 0 X X X 2",
              "X X X X X X X X" },
then from room 0, paths 1 and 2 lead back to room 0. Paths 3 and 4 in room 0 lead to room 1. In room 1, paths 1 and 8 lead to room 2, paths 3 and 4 lead to room 0, and path 1 leads back to room 1. No paths lead out of room 2. Note that the paths are not bidirectional. start will say which room is the starting position, and finish will contain the winning positions. If the resulting map N is unwinnable, return 1.

Constraints

  • M will contain between 1 and 50 elements inclusive.
  • start will be between 0 and m-1 inclusive, where m is the number of elements in M.
  • finish will contain between 0 and 50 elements inclusive.
  • Each element of finish will be between 0 and m-1 inclusive, where m is the number of elements in M.
  • Each element of finish will be distinct.
  • R will be between 1 and 7 inclusive.
  • Each element of M will have the format "Y Y Y Y Y Y Y Y" where each Y is a number or (quotes for clarity) 'X'. If a number, Y has no extra leading zeros, and is between 0 and m-1 inclusive. Here m denotes the number of elements in M.
Examples
0)
{"1 X X X X X X X",
 "2 X X X X X X X",
 "X X X X X X X X"}
0
{2}
1
Returns: 3

N only allows paths in M to enter winning positions exactly once. Since M already behaves this way, and is minimal, the result is 3.

1)
{"1 X X X X X X X",
 "2 X X X X X X X",
 "X X X X X X X X"}
0
{2}
2
Returns: 1

There are no paths in M that enter a winning position twice. Thus N is unwinnable.

2)
{"X X X X X X X 1",
 "0 X X X X X X X"}
0
{1}
7
Returns: 14
3)
{"X X X X X X X 1", "X X X X X X X 2", "X X X X X X X 3", "X X X X X X X 4", 
 "X X X X X X X 5", "X X X X X X X 6", "X X X X X X X 7", "X X X X X X X 8", 
 "X X X X X X X 9", "X X X X X X X 10", "X X X X X X X 11", "X X X X X X X 12", 
 "X X X X X X X 13", "X X X X X X X 14", "X X X X X X X 15", "X X X X X X X 16",
 "X X X X X X X 17", "X X X X X X X 18", "X X X X X X X 19", "X X X X X X X 20",
 "X X X X X X X 21", "X X X X X X X 22", "X X X X X X X 23", "X X X X X X X 24",
 "X X X X X X X 25", "X X X X X X X 26", "X X X X X X X 27", "X X X X X X X 28",
 "X X X X X X X 29", "X X X X X X X 30", "X X X X X X X 31", "X X X X X X X 32",
 "X X X X X X X 33", "X X X X X X X 34", "X X X X X X X 35", "X X X X X X X 36",
 "X X X X X X X 37", "X X X X X X X 38", "X X X X X X X 39", "X X X X X X X 40",
 "X X X X X X X 41", "X X X X X X X 42", "X X X X X X X 43", "X X X X X X X 44",
 "X X X X X X X 45", "X X X X X X X 46", "X X X X X X X 47", "X X X X X X X 48",
 "X X X X X X X 49","X X X X X X X X"}
0
{49}
7
Returns: 1
4)
{"X X X X X X X X"}
0
{}
7
Returns: 1

No winning states at all.

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

Coding Area

Language: C++17 · define a public class DungeonBuilder with a public method int howMany(vector<string> M, int start, vector<int> finish, int R) · 68 test cases · 2 s / 256 MB per case

Submitting as anonymous