后七个点全爆了内存,求看看有啥问题。
#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;
}
}