私は Python を初めて使用し、Kingdom Connectivity のインタビューストリートの問題を解決しようとしていました。なんとか問題を解決できましたが、指定された形式の入力に問題があり、システムでソリューションを試してみましたが、出力は正しいですが、そこでコンパイルするとすぐに出力がありません。
入力は次の形式です。
5 5
1 2
2 3
3 4
1 3
4 5
この問題を解決する方法を教えてください。
現在、raw_input()
ループ内から入力を取得し、 を使用して分割していa.split(' ')
ます。
ここに質問の一部があります:
**Input Description:**
First line contains two integers N and M.
Then follow M lines ,each having two integers say x and y, 1<=x,y<=N , indicating there is a road from city x to city y.
**Output Description:**
Print the number of different paths from city 1 to city N modulo 1,000,000,000(10^9).If there are infinitely many different paths print "INFINITE PATHS"(quotes are for clarity).
**Sample Input:**
5 5
1 2
2 4
2 3
3 4
4 5
**Sample Output:**
2
**Sample Input:**
5 5
1 2
4 2
2 3
3 4
4 5
**Sample Output:**
INFINITE PATHS
これが私の解決策です
import sys
import numpy as np
c=0
x=raw_input()
y=x.split(' ')
l=(int(y[0]),int(y[1]))
e=[raw_input() for i in range(l[1])]
f=[e[i].split(' ') for i in range(l[1])]
a=[map(int,i) for i in f]
b=[[0 for i in a] for j in range(l[0])]
for i in range(l[0]+1):
for j in range(l[0]+1):
if [i,j] in a:
b[i-1][j-1]=1
elif a[i-1][0]>=a[i-1][1]:
print "INFINITE PATHS"
sys.exit(0)
for i in range(0,l[1]):
d=np.linalg.matrix_power(b,i+1)
c+=d[0][l[1]-1]
print c
ここにスクリーンショットがあります