社区讨论

bfs WA/MLE 0pts 求助!

P1126[CERC1996] 机器人搬重物参与者 1已保存回复 0

讨论操作

快速查看讨论及其快照的属性,并进行相关操作。

当前回复
0 条
当前快照
1 份
快照标识符
@lv9079ix
此快照首次捕获于
2024/04/21 12:03
2 年前
此快照最后确认于
2024/04/21 14:45
2 年前
查看原帖
CPP
#include <bits/stdc++.h>
using namespace std;

int n,m;
int sx,sy,ex,ey;
const int dx[5][5] = {{0,0,0,0},{0,0,0,0},{0,1,2,3},{0,0,0,0},{0,-1,-2,-3}};
const int dy[5][5] = {{0,0,0,0},{0,1,2,3},{0,0,0,0},{0,-1,-2,-3},{0,0,0,0}};
const int dd[] = {0,1,-1};
char d;
bool a[55][55],vis[55][55];
struct node
{
	int x,y,step,dir;
};
queue <node> q;

int change(char x)
{
	if (x == 'E') return 1;
	if (x == 'S') return 2;
	if (x == 'W') return 3;
	return 4;
}

int main()
{
	cin >> n >> m;
	for (int i = 1;i <= n;i ++){
		for (int j = 1;j <= m;j ++){
			cin >> a[i][j];
			if (a[i][j] == 1){
				a[i - 1][j] = 1;
				a[i][j - 1] = 1;
				a[i - 1][j - 1] = 1;
			}
		}
	}
	cin >> sx >> sy >> ex >> ey >> d;
	for (int i = 1;i <= n;i ++){
		for (int j = 1;j <= m;j ++){
			cout << a[i][j] << " \n"[j == m];
		}
	}
	vis[sx][sy] = 1;
	q.push({sx,sy,0,change(d)});
	while (!q.empty()){
		node tmp = q.front();
		q.pop();
		if (tmp.x == ex and tmp.y == ey){
			cout << tmp.step << endl;
			return 0;
		}
		for (int i = 1;i <= 3;i ++){
			int nx = tmp.x + dx[tmp.dir][i],ny = tmp.y + dy[tmp.dir][i];
			if (nx < 2 or ny < 2 or nx > n - 1 or ny > m - 1) continue;
			if (vis[nx][ny] or a[nx][ny]) continue;
			vis[nx][ny] = 1;
			q.push({nx,ny,tmp.step + 1,tmp.dir});
		}
		for (int i = 1;i <= 2;i ++){
			int nd = tmp.dir + dd[i];
			if (nd < 1) nd += 4;
			if (nd > 4) nd -= 4;
			q.push({tmp.x,tmp.y,tmp.step + 1,nd});
		}
	}
	cout << -1 << endl;
	return 0;
}

回复

0 条回复,欢迎继续交流。

正在加载回复...