各位大佬,帮忙看一下我这个弱鸡的代码哪里错了
查看原帖
各位大佬,帮忙看一下我这个弱鸡的代码哪里错了
566855
miaomiaofashi楼主2023/2/16 20:06
#include<bits/stdc++.h>
using namespace std;

#define mem(a, b) memset(a, b, sizeof(a));
using ld = long double;
using ll = long long;
int INF = 1e9;
long long LINF = 9e18;
double EPS = 1e-9;
double PI = acos(-1.0);
 
static int donotused = []() {
	ios_base::sync_with_stdio(false); cin.tie(0);
#ifndef ONLINE_JUDGE
	(void)!freopen("P1126.in", "r", stdin);
	(void)!freopen("P1126.out", "w", stdout);
#endif
	return 0;
}();
//  1  
//2   0
//  3  
int n, m, sx, sy, ex, ey, sdis;
bool a[60][60];

int dis[4][2] = {{0, 1}, {-1, 0}, {0, -1}, {1, 0}};

struct node {
	int x, y, dis, step;
	node() {};
	node(int x_, int y_, int dis_, int step_) {
		x = x_, y = y_, dis = dis_, step = step_;
	}
};

bool vis[60][60][4];

bool check(int x, int y) {
	for (int i = x; i <= x + 1; i++) {
		for (int j = y; j <= y + 1; j++) {
			if (i < 1 || j < 1 || i > n || j > m || a[i][j])
				return false;
		}
	}
	return true;
}

void bfs() {
	queue<node> q;
	q.push(node(sx, sy, sdis, 0));
	while(!q.empty()) {
		node now = q.front();
		q.pop();
		if (now.x == ex && now.y == ey) {
			cout << now.step;
			return;
		}
		if (vis[now.x][now.y][now.dis] == 1)
			continue;
		vis[now.x][now.y][now.dis] = 1;
		for (int i = 1; i <= 3; i++) {
			int nx = now.x + dis[now.dis][0] * i;
			int ny = now.y + dis[now.dis][1] * i;
			if (check(nx, ny)) {
				q.push(node(nx, ny, now.dis, now.step + 1));
			}
		}
		for (int i = 1; i <= 3; i++) {
			int ndis = (now.dis + i) % 4;
			q.push(node(now.x, now.y, ndis, now.step + 1));
		}
	}
	cout << -1;
}

int main() {
	cin >> n >> m;
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= m; j++) {
			cin >> a[i][j];
		}
	}
	char c;
	cin >> sx >> sy >> ex >> ey >> c;
	switch(c) {
		case 'E':
			sdis = 0;
			break;
		case 'N':
			sdis = 1;
			break;
		case 'W':
			sdis = 2;
			break;
		case 'S':
			sdis = 3;
			break;
	}
	bfs();
	return 0;
}
2023/2/16 20:06
加载中...