A pitch-black room When I woke up, Mr. A was in a pitch-black room. Apparently, Mr. A…
- User
A pitch-black room
When I woke up, Mr. A was in a pitch-black room. Apparently, Mr. A got lost in a dungeon consisting of N rooms. You couldn't know which room A got lost in, but fortunately he got a map of the dungeon. Let's show A the way to go and lead to a bright room.
It is known that M of the N rooms are pitch black, and the D_1, D_2, ..., and D_Mth rooms are pitch black, respectively. Also, there are exactly K one-way streets from all the rooms in order, and the roads from the i-th room are v_ {i, 1}, v_ {i, 2}, ..., v_ {i, respectively. , K} Connected to the third room. You can follow the a_1, a_2, ..., a_lth roads in order from the room you are in for Mr. A. However, when Mr. A reaches the bright room, he ignores the subsequent instructions. You cannot know the information of the room where Mr. A is currently before and after the instruction, so you must give the instruction sequence so that you can reach the bright room no matter which room you are in. Answer the length of the shortest of such instructions.
Constraints
- 2 ≤ N ≤ 100
- 1 ≤ M ≤ min (16, N − 1)
- 1 ≤ K ≤ N
- 1 ≤ D_i ≤ N
- D_i are all different
- 1 ≤ v_ {i, j} ≤ N
- All dark rooms can reach at least one bright room Input Format
Input is given from standard input in the following format.
NMK D_1 D_2 ... D_M v_ {1,1} v_ {1,2} ... v_ {1,K} v_ {2,1} v_ {2,2} ... v_ {2, K} ... v_ {N, 1} v_ {N, 2} ... v_ {N, K}
Output Format
Print the answer in one line.
Sample Input 1
4 2 2 1 2 twenty four 3 1 4 2 13
Sample Output 1
2
- If A's initial position is room 1, the second move will reach room 3.
- If A's initial position is room 2, the first move will reach room 3.
Sample Input 2
3 2 2 1 2 twenty three 1 2 twenty one
Sample Output 2
3
- If A's initial position is room 1, the first move will reach room 3.
- If A's initial position is room 2, the third move will reach room 3.
Sample Input 3
6 3 3 one two Three 4 1 1 2 5 2 3 3 6 4 4 4 5 5 5 6 6 6
Sample Output 3
3
Example
Input
4 2 2 1 2 2 4 3 1 4 2 1 3
Output
2
Assistant
from collections import deque def main(): import sys input = sys.stdin.read().split() ptr = 0 N = int(input[ptr]) ptr +=1 M = int(input[ptr]) ptr +=1 K = int(input[ptr]) ptr +=1 D = list(map(int, input[ptr:ptr+M])) ptr +=M dark_rooms = set(D) bright_rooms = set(range(1, N+1)) - dark_rooms room_to_index = {d:i for i, d in enumerate(D)} transitions = [] for _ in range(N): row = list(map(int, input[ptr:ptr+K])) ptr += K transitions.append(row) initial_mask = (1 << M) - 1 visited = set() queue = deque() queue.append( (initial_mask, 0) ) visited.add(initial_mask) while queue: current_mask, steps = queue.popleft() if current_mask == 0: print(steps) return for a in range(1, K+1): new_mask = 0 for i in range(M): if (current_mask & (1 << i)) == 0: continue d_room = D[i] next_room = transitions[d_room -1][a-1] if next_room in bright_rooms: continue j = room_to_index[next_room] new_mask |= (1 << j) if new_mask not in visited: visited.add(new_mask) queue.append( (new_mask, steps +1) ) if new_mask == 0: print(steps +1) return print(-1) # Should not reach here as per problem statement if __name__ == "__main__": main()
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.