C++广搜,30分,七个点MLE,求解决~
  • 板块P1443 马的遍历
  • 楼主萝鱼
  • 当前回复4
  • 已保存回复4
  • 发布时间2021/5/13 19:45
  • 上次更新2023/11/4 23:19:07
查看原帖
C++广搜,30分,七个点MLE,求解决~
313023
萝鱼楼主2021/5/13 19:45

后七个点全爆了内存,求看看有啥问题。

#include<bits/stdc++.h>
using namespace std;
const int dx[8]={1,1,2,2,-1,-1,-2,-2};
const int dy[8]={2,-2,1,-1,2,-2,1,-1};

struct H{
    int x,y,s;
};
int m,n,x,y;
int result[400][400];
queue <H> q;

bool check(int x, int y){
    if ((x<0)||(y<0)||(x>n-1)||(y>m-1)) return false;
    if (result[x][y] != -1) return false;
    return true;
}

int main(){
    memset(result,-1,sizeof(result));
    cin >> n >> m >> x >> y;
    H h;
    h.x = x-1;	h.y = y-1;	h.s = 0;
    q.push(h);
    
    while (not q.empty()){
        h = q.front();
        q.pop();
        result[h.x][h.y] = h.s;
        
        for (int i=0; i<8; i++){
        	H nh;
        	if (check(h.x+dx[i], h.y+dy[i])){
            	nh.x = h.x+dx[i];	nh.y = h.y+dy[i];
            	nh.s = h.s + 1;
        		q.push(nh);
        	}
        }
    }
    
    for(int i=0; i<m; i++){
        for(int j=0; j<n; j++){
            printf("%-5d",result[j][i]);
        }
        cout << endl;
    }
}
2021/5/13 19:45
加载中...