【发布时间】:2012-03-19 22:04:09
【问题描述】:
我是 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
这是截图
【问题讨论】:
-
向我们展示您的代码,以便我们查看错误所在的位置。
-
顺便说一句——这是作业吗?如果是,请这样标记。
-
@hochl 我已经编辑并添加了我的代码
-
这个作业是怎样的?我有一个真正的问题,我不是要求做我的代码,我只需要输入部分的帮助,如果我能得到帮助会很棒,因为在所有其他问题中,我面临同样的问题
-
“没有输出”是什么意思?您确定问题出在您的输入方式上吗?