P1301 魔鬼之城 求调
  • 板块灌水区
  • 楼主zzx1228
  • 当前回复0
  • 已保存回复0
  • 发布时间2024/3/14 21:03
  • 上次更新2024/3/14 22:50:53
查看原帖
P1301 魔鬼之城 求调
996601
zzx1228楼主2024/3/14 21:03
#include<bits/stdc++.h>
using namespace std;

const int N = 107;

int fx[] = {0,-1,-1,-1,0,0,1,1,1};
int fy[] = {0,1,0,-1,1,-1,1,0,-1};

int n,m;
int sum[N][N];
int ans[N][N];

struct Node{
	int x;
	int y;
	int d;
};

void bfs(){
	for(int i = 0;i < N;i++)
		for(int j = 0;j < N;j++)
			ans[i][j] = -1;
	ans[1][1] = 0;
	queue<Node> Q;
	Q.push({1,1,0});
	while(Q.size()){
		Node now = Q.front();
		Q.pop();
		for(int i = 1;i <= 8;i++){
			if(i == now.d) continue;
			int nx = now.x + (sum[now.x][now.y]*fx[i]);
			int ny = now.y + (sum[now.x][now.y]*fy[i]);
			if(nx < 1 || nx > m || ny < 1 || ny > n) continue;
			if(ans[nx][ny] != -1) continue;
			ans[nx][ny] = ans[now.x][now.y] + 1;
			Q.push({nx,ny,i});
		}
	}
}

int main(){
	cin>>n>>m;
	for(int i = 1;i <= m;i++)
		for(int j = 1;j <= n;j++)
			cin>>sum[i][j];
	bfs();
	
	if(ans[m][n] == -1){
		cout<<"NEVER";
		return 0; 
	}
	cout<<ans[m][n];
	
	return 0;
}

如上,80分。

我有关于为什么错的思路:有的时候可以绕长一点来到一个点使这个方向限制换一个从而达到终点。而我现在优先以快为目的,所以出现了 WA 了2个点的情况,但是我不会调,求助

2024/3/14 21:03
加载中...