#abc291f. F - Teleporter and Closed off
F - Teleporter and Closed off
Score : points
问题描述
有 座城市,依次编号为城市 、城市 、 和城市 。
同时存在一些单向传送器,可以将你传送到不同的城市。从城市 是否可以直接通过传送器到达另一座城市,由长度为 的字符串 表示,其中包含字符 0
和 1
。具体来说,对于 :
- 若 并且 中的第 个字符为
1
,则有一个传送器可以直接将你从城市 传送到城市 ; - 否则,不能直接从城市 传送到城市 。
求解以下问题,对于 :
你能否通过重复使用传送器,从城市 到达城市 且不经过城市 ?如果可以,请输出所需的最小传送器使用次数;否则,输出 。
以上为通义千问 qwen-max 翻译,仅供参考。
Problem Statement
There are cities numbered city , city , , and city .
There are also one-way teleporters that send you to different cities. Whether a teleporter can send you directly from city to another is represented by a length- string consisting of 0
and 1
. Specifically, for ,
- if and the -th character of is
1
, then a teleporter can send you directly from city to city ; - otherwise, it cannot send you directly from city to city .
Solve the following problem for :
Can you travel from city to city without visiting city by repeatedly using a teleporter? If you can, print the minimum number of times you need to use a teleporter; otherwise, print .
Constraints
- is a string of length consisting of
0
and1
. - If , then the -th character of is
0
. - and are integers.
Input
The input is given from Standard Input in the following format:
Output
Print integers, separated by spaces, in a single line. The -th integer should be the answer to the problem for .
Sample Input 1
5 2
11
01
11
10
00
Sample Output 1
2 3 2
A teleporter sends you
- from city to cities and ;
- from city to city ;
- from city to cities and ;
- from city to city ; and
- from city to nowhere.
Therefore, there are three paths to travel from city to city :
- path : city city city city ;
- path : city city city city ; and
- path : city city city .
Among these paths,
- two paths, path and path , do not visit city . Among them, path requires the minimum number of teleporter uses (twice).
- Path is the only path without city . It requires using a teleporter three times.
- Path is the only path without city . It requires using a teleporter twice.
Thus, , , and , separated by spaces, should be printed.
Sample Input 2
6 3
101
001
101
000
100
000
Sample Output 2
-1 3 3 -1
The only path from city to city is city city city city .
For , there is no way to travel from city to city without visiting city .
For , the path above satisfies the condition; it requires using a teleporter three times.
Thus, , , , and , separated by spaces, should be printed.
Note that a teleporter is one-way; a teleporter can send you from city to city , but not from city to city ,
so the following path, for example, is invalid: city city city city .
update @ 2024/3/10 12:09:18