我是 Python 新手,我正在尝试解决 Kingdom Connectivity 的 interviewstreet 问题。虽然,我设法解决了这个问题,但我在输入给定格式时遇到了麻烦,我已经在我的系统上尝试了我的解决方案并且输出是正确的,但是一旦我在那里编译,就没有输出。
输入的形式为:
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
这是截图