Problem Description
There are N cities in the country. Each city is represent by a matrix size of M*M. If city A, B and C satisfy that A*B = C, we say that there is a road from A to C with distance 1 (but that does not means there is a road from C to A).
Now the king of the country wants to ask me some problems, in the format:
Is there is a road from city X to Y?
I have to answer the questions quickly, can you help me?

Input
Each test case contains a single integer N, M, indicating the number of cities in the country and the size of each city. The next following N blocks each block stands for a matrix size of M*M. Then a integer K means the number of questions the king will ask, the following K lines each contains two integers X, Y(1-based).The input is terminated by a set starting with N = M = 0. All integers are in the range [0, 80].

Output
For each test case, you should output one line for each question the king asked, if there is a road from city X to Y? Output the shortest distance from X to Y. If not, output "Sorry".

Sample Input
3 2
1 1
2 2
1 1
1 1
2 2
4 4
1
1 3
3 2
1 1
2 2
1 1
1 1
2 2
4 3
1
1 3
0 0

Sample Output
1
Sorry

1个回答

-
C语言 请教有关最短路径算法的伪代码
-
C语言实现每个节点到终点的最短路径问题？
-
Java求最短路径的问题
-

-
C# 最短路径问题，取出最短路径Node编号
-
C语言解决一个关于最短路径的花费的问题，怎么利用C解决
-

-

-

-
spark graphx框架下怎样求两点间最短路径的条数
-

-

-

-

-

-

-

-
C语言算法实现查找二叉树最短路径的问题
-

GitHub开源的10个超棒后台管理面板

Git 天天用 但是 Git 原理你了解吗？
Git 原理 做技术一定要知其然知其所以然，意思就是：知道它是这样的，更知道它为什么是这样的。我主要通过4块内容来简单介绍 Git 是原理是什么样的。这4块内容如下： Git 存储目录结构介绍 Git 是如何存储的 Git 的对象 Git引用 当然 Git 原理不仅仅包含这些，想要更深入了解请查看官方教程 https://git-scm.com/book/zh/v2/。 本文内容是我在 Git...

100 个网络基础知识普及，看完成半个网络高手

C语言实现推箱子游戏

Java 的每个基本类型都对应了一个包装类型，比如说 int 的包装类型为 Integer，double 的包装类型为 Double。基本类型和包装类型的区别主要有以下 4 点。
8000字干货：那些很厉害的人是怎么构建知识体系的

Android完整知识体系路线(菜鸟-资深-大牛必进之路)

【公众号回复 “1024”，免费领取程序员赚钱实操经验】今天我章鱼猫给大家带来的这个开源项目，估计很多喜欢听音乐的朋友都会喜欢。就目前来讲，很多人对这款音乐 App 都抱...