社区讨论

求助80pts WA on#8#9

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

讨论操作

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

当前回复
0 条
当前快照
1 份
快照标识符
@lr94b9r6
此快照首次捕获于
2024/01/11 19:20
2 年前
此快照最后确认于
2024/01/11 22:09
2 年前
查看原帖
改了两天了QAQ,各位帮忙指个错误
CPP
#include<bits/stdc++.h>
using namespace std;
int a[60][60];
int dis[60][60];
int fx[] = {-1,1,0,0};
int fy[] = {0,0,-1,1};
enum dir {
    N,S,E,W
};
struct edge {
	int x, y;
	dir d;
};
struct node {
	int step;
	int x, y;
	dir d;
	bool operator<(const node& N)const {
		return N.step < step;
	}
};
vector<edge> e[60][60];
int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	int n, m;
	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][j-1] = a[i-1][j] = a[i-1][j-1] = 1;
			}
			dis[i][j] = 0x3f3f3f3f;
		}
	}
	n--; m--;
//	cout << endl;
//	for(int i = 1; i<=n; i++){
//		for(int j = 1; j<=m; j++){
//			cout << a[i][j] << " "; 
//		}
//		cout << endl;
//	}
	for(int h = 1; h<=n; h++) {
		for(int l = 1; l<=m; l++) {
			for(int k = 0; k<4; k++){
				for(int g = 1; g<=3; g++){
					int nx = h+fx[k]*g;
					int ny = l+fy[k]*g;
					if(nx>=1&&ny>=1&&nx<=n&&ny<=m&&a[nx][ny]!=1){
						e[h][l].push_back(edge{nx,ny,(dir)k});
					}else{
						break;
					}
				}
			}
			
		}
	}
	int ax, ay, bx, by;
	cin >> ax >> ay >> bx >> by;
	char d;
	cin >> d;
	dir empt;
	switch(d) {
		case 'N':
			empt = N;
			break;
		case 'S':
			empt = S; 	
			break;
		case 'E':
			empt = E;
			break;
		case 'W':
			empt = W;
			break;
	}
	priority_queue<node> q;
	q.push(node {0,ax,ay,empt});
	dis[ax][ay] = 0;
	while(!q.empty()){
		node x = q.top();
		q.pop();
		for(auto i : e[x.x][x.y]){
			int nx = i.x;
			int ny = i.y;
			int base = 1;
			if(i.d==x.d) base = 1;
			else if( ((i.d==N||i.d==S)&&(x.d!=i.d)&&(x.d==N||x.d==S)) || ((i.d==E||i.d==W)&&(x.d!=i.d)&&(x.d==E||x.d==W)) ) base = 3;
			else base = 2;
			if(dis[nx][ny]>dis[x.x][x.y]+base){
				dis[nx][ny] = dis[x.x][x.y]+base;
				q.push(node{dis[nx][ny],nx,ny,i.d});
			}
		}
	}
//	for(int i = 1; i<=n; i++){
//		for(int j = 1; j<=m; j++){
//			if(dis[i][j]!=0x3f3f3f3f)cout << dis[i][j] << " ";
//			else cout << "-1 ";
//		}
//		cout << endl;
//	}
	if(dis[bx][by]!=0x3f3f3f3f) cout << dis[bx][by];
	else cout << -1;
	return 0;
}
求助各位!(玄关

回复

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

正在加载回复...