【SPOJ-DESRUG】Desrugenstein【Floyd】

题意:

给出一个NxN的网格,给出每个网格可以走的方向,有一些询问,问从一点到另一点的最短路长度。


多源最短路裸题,但是读入太恶心了,而且表述不清楚...


附自己的画的样例的图:

(比较丑)



#include <cstdio>
#include <algorithm>

using namespace std;

const int maxn = 105, inf = 0x3f3f3f3f;

int n, g[maxn][maxn];

inline int iread() {
	int f = 1, x = 0; char ch = getchar();
	for(; ch < '0' || ch > '9'; ch = getchar()) f = ch == '-' ? -1 : 1;
	for(; ch >= '0' && ch <= '9'; ch = getchar()) x = x * 10 + ch - '0';
	return f * x;
}

inline int getid(int x, int y) {
	return y * n + x;
}

int main() {
	while(1) {
		n = iread();
		if(n == 0) break;

		int tot = n * n;
		for(int i = 0; i < tot; i++) for(int j = 0; j < tot; j++) g[i][j] = (i != j) * inf;

		for(int i = n - 1; i >= 0; i--) for(int j = 0; j < n; j++) {
			int a = iread(), b = iread(), c = iread(), d = iread();
			if(a == 1) g[getid(j, i)][getid(j, i + 1)] = 1;
			if(b == 1) g[getid(j, i)][getid(j, i - 1)] = 1;
			if(c == 1) g[getid(j, i)][getid(j - 1, i)] = 1;
			if(d == 1) g[getid(j, i)][getid(j + 1, i)] = 1;
		}

		for(int k = 0; k < tot; k++) for(int i = 0; i < tot; i++) for(int j = 0; j < tot; j++)
			g[i][j] = min(g[i][j], g[i][k] + g[k][j]);

		int m = iread();
		while(m--) {
			int x0 = iread(), y0 = iread(), x1 = iread(), y1 = iread(), ans = g[getid(x0, y0)][getid(x1, y1)];
			ans != inf ? printf("%d\n", ans) : printf("Impossible\n");
		}
	}

	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值