社区讨论
小心TLE!!快来看看!!
P1443马的遍历参与者 7已保存回复 6
讨论操作
快速查看讨论及其快照的属性,并进行相关操作。
- 当前回复
- 6 条
- 当前快照
- 1 份
- 快照标识符
- @mhiznxh5
- 此快照首次捕获于
- 2025/11/03 18:20 4 个月前
- 此快照最后确认于
- 2025/11/03 18:20 4 个月前
我的之前的代码(别抄哦-_-):
CPP#include<bits/stdc++.h>
using namespace std;
int n,m,x,y,dx[8]={1,1,2,2,-1,-1,-2,-2},dy[8]={2,-2,1,-1,2,-2,1,-1},l,r;
bool v[410][410];
struct node{
int x,y,step;
};
int bfs(node s){
queue<node> q;
q.push(s);
v[s.x][s.y]=1;
while(!q.empty()){
node t=q.front();
q.pop();
if(t.x==l&&t.y==r) return t.step;
for(int i=0;i<8;i++){
int nx=t.x+dx[i],ny=t.y+dy[i];
if(v[nx][ny]==0&&nx<=n&&ny<=m&&nx>=1&&ny>=1){
v[nx][ny]=1;
q.push({nx,ny,t.step+1});
}
}
}
return -1;
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0),cout.tie(0);
cin>>n>>m>>x>>y;
for(l=1;l<=n;l++){
for(r=1;r<=m;r++){
memset(v,0,sizeof(v));
cout<<bfs({x,y,0})<<" ";
}
cout<<"\n";
}
return 0;
}
TLE了1个点!!
切记,范围再小也可能超时!!
可以先求出到达每个点的最小步数,再逐个输出。
-_- 你明白了吗?
看完后请点个赞哦!!!
回复
共 6 条回复,欢迎继续交流。
正在加载回复...