40分求助QwQ
查看原帖
40分求助QwQ
311110
_djc_楼主2022/7/27 12:07

不知道这样写为什么错,救救孩子!

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define maxn 100
inline int read() {
	int x = 0, f = 1; char c = getchar();
	while (c < '0' || c > '9') { if (c == '-') f = -1; c = getchar(); }
	while (c >= '0' && c <= '9') { x = x * 10 + c - '0'; c = getchar(); }
	return x * f;
}
int x_1, y_1, x_2, y_2;
int vis[maxn][maxn];
int n, m;
int dis[maxn][maxn][5];
int mp[maxn][maxn];
char to;
struct node{
	int x, y, to;
};
int fx(char to) {
	switch(to) {
		case 'N': return 0; break;
		case 'S': return 1; break;
		case 'E': return 2; break;
		case 'W': return 3; break;
	}
}

int turn(int f1, int f2) {
	return min((f1 - f2 + 4) % 4, (f2 - f1 + 4) % 4);
}
queue<node> q;
signed main() {
	n = read(), m = read();
	for (int i = 1; i <= n; i++)
		for (int j = 1; j <= m; j++)
			mp[i][j] = read();
	x_1 = read(), y_1 = read(), x_2 = read(), y_2 = read();
	cin >> to;
	q.push(node({x_1, y_1, fx(to)}));
	dis[x_1][y_1][fx(to)] = 0;
	while (!q.empty()) {
		node tmp = q.front();
		q.pop();
		if(tmp.x == x_2 && tmp.y == y_2) {
			cout << dis[x_2][y_2][tmp.to];
			return 0;
		}
		if(vis[tmp.x][tmp.y]) continue;
		vis[tmp.x][tmp.y] = 1;
		for (int i = 0; i < 4; i++) {
			dis[tmp.x][tmp.y][i] = dis[tmp.x][tmp.y][tmp.to] + turn(i, tmp.to);
			for (int j = 1; j <= 3; j++) {
				int ux = tmp.x, uy = tmp.y, ot = i;
				if(ot == 1) ux -= j;
				if(ot == 2) ux += j;
				if(ot == 3) uy += j;
				if(ot == 4) uy -= j;
				if(ux > 0 && ux <= m && uy > 0 && uy <= n && !vis[ux][uy] && mp[ux][uy] == 0) {
					dis[ux][uy][i] = dis[tmp.x][tmp.y][i] + j;
					q.push(node({ux, uy, ot}));
				}
			}
		}
	}
	cout << -1;
}
2022/7/27 12:07
加载中...