蒟蒻求助!!!!!!help!!!!!
  • 板块P1443 马的遍历
  • 楼主leo888
  • 当前回复3
  • 已保存回复3
  • 发布时间2021/7/28 20:27
  • 上次更新2023/11/4 12:49:42
查看原帖
蒟蒻求助!!!!!!help!!!!!
543527
leo888楼主2021/7/28 20:27

样例对了,但提交只有20分,帮忙看一下!!!!!!!!!help!!!!! save me!!!

#include<bits/stdc++.h>
using namespace std;
int n,m,x,y,rear,head;
const int fx[9]={0,2,2,1,-1,-2,-2,-1,1};
const int fy[9]={0,-1,1,2,2,-1,1,-2,-2};
bool vis[10001][10001];
int a[10001][10001];
struct node{
	int tx;
	int ty;
	int step=0;
}q[10001];
int bfs(int xx,int yy){
	q[rear].tx=xx;
	q[rear].ty=yy;
	q[rear].step=0;
	vis[xx][yy]=1;
	rear++;
	while(head<rear){
		node now=q[head];
		head++;
		for(int i=1;i<=8;i++){
			int txx=now.tx+fx[i];
			int tyy=now.ty+fy[i];
			if(vis[txx][tyy]==0 && txx>=1 && txx<=n && tyy>=1 && tyy<=m){
				vis[txx][tyy]=1;
				q[rear].tx=txx;
				q[rear].ty=tyy;
				q[rear].step=now.step+1;
				a[txx][tyy]=now.step+1;
				rear++;
			}
		}
	}
}
int main(){
	cin>>n>>m>>x>>y;
	bfs(x,y);
	for(int i=1;i<=n;i++){
		for(int j=1;j<=m;j++){
			if (j>1)cout<<setw(5);
			if(i==x && j==y)cout<<0;
			else if(a[i][j]==0)cout<<-1;
			else cout<<a[i][j];
		}
		cout<<endl;
	}
	return 0;
}
2021/7/28 20:27
加载中...