70分 bfs WA #4#7#10求调
查看原帖
70分 bfs WA #4#7#10求调
933802
ICU152_lowa_IS8楼主2024/3/2 01:36
#include<bits/stdc++.h>
#define int long long
using namespace std;
int n,m;
int a[1005][1005];
int dx[8]={1,0,-1,0,1,1,-1,-1};
int dy[8]={0,1,0,-1,1,-1,1,-1};
struct node{
	int x,y,lastm,step;
};
queue<node>q;
bool vis[1005][1005];
void bfs(){
	while(!q.empty()){
		node v=q.front();
		q.pop();
		if(v.x==n&&v.y==m){
			cout<<v.step;
			return;
		} 
		for(int i=0;i<8;i++){
			if(i==v.lastm)continue;
			int x=v.x+dx[i]*a[v.x][v.y],y=v.y+dy[i]*a[v.x][v.y];
			if(x>=1&&x<=n&&y>=1&&y<=m&&!vis[x][y]){
				vis[x][y]=true;
				q.push((node){
					x,y,i,v.step+1
				});
			}
		}
	}
	cout<<"NEVER";
}
signed main(){
	cin>>m>>n;
	for(int i=1;i<=n;i++){
		for(int j=1;j<=m;j++){
			cin>>a[i][j];
		}
	}
	q.push((node){
		1,1,-1,0
	});
	vis[1][1]=true;
	bfs();
	return 0;
}

2024/3/2 01:36
加载中...