Enmin @ 2024-08-08 16:27:10
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
using db = double;
using str = string;
const int N = 1e7;
const int M = 1e4;
//升:priority_queue <int,vector<int>,greater<int>> qe;
//降:priority_queque <int,vector<int>,less<int>> qe;
int g[9] = {0,-2,-1,1,2,2,1,-1,-2};
int h[9] = {0,1,2,2,1,-1,-2,-2,-1};
int n,m,l,r,sum=1,tail=1,x[N],y[N],z[N];
int bfs(int xy,int yx) {
bool b[M][M];
x[1]=l;
y[1]=r;
z[1]=0;
b[l][r]=1;
if(l==xy&&r==yx) {
return 0;
}
while(sum<=tail) {
for(int k=1;k<=8;k++) {
int t1 = x[sum] + g[k];
int t2 = y[sum] + h[k];
if(t1>0&&t1<=n&&t2>0&&t2<=m&&b[t1][t2]==0) {
b[t1][t2]=1;
tail++;
x[tail]=t1;
y[tail]=t2;
z[tail]=z[sum]+1;
if(t1==xy&&t2==yx) {
return z[tail];
}
}
}
sum++;
}
return -1;
}
int main() {
scanf("%d%d%d%d",&n,&m,&l,&r);
for(int i=1;i<=n;i++) {
for(int j=1;j<=m;j++) {
int ans=0;
ans=bfs(i,j);
printf("%d",ans);
sum=1,tail=1;
if(j!=m) {
printf(" ");
}else {
printf("\n");
}
}
}
return 0;
}
我用的是sublime Text的编程程序。 我电脑输出样例时输出了这个玩意儿:
bash: 第 2 行: 13681 段错误
by Enmin @ 2024-08-08 16:52:02
我刚提交完90分,没想到你就发了这个......
by Enmin @ 2024-08-08 16:57:44
找步数最长的一点,这样可以顺着把其他的点也走到,是这样吗 @Wilson_Lee
by Enmin @ 2024-08-08 16:59:29
哦,是右下角那点,懂了!!!
by Wilson_Lee @ 2024-08-08 17:00:14
@Enmin 想一想,bfs其实只用以?为起点跑一遍的
by Enmin @ 2024-08-08 17:08:51
AC!!!
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
using db = double;
using str = string;
const int N = 1e5;
const int M = 4e2 + 1e2;
//升:priority_queue <int,vector<int>,greater<int>> qe;
//降:priority_queque <int,vector<int>,less<int>> qe;
int g[9] = {0,-2,-1,1,2,2,1,-1,-2};
int h[9] = {0,1,2,2,1,-1,-2,-2,-1};
int n,m,l,r,sum=1,tail=1,x[N],y[N],z[N],ans[M][M];
int bfs(int xy,int yx) {
bool b[M][M];
memset(b,0,sizeof(b));
x[1]=l;
y[1]=r;
z[1]=0;
b[l][r]=1;
if(l==xy&&r==yx) {
return 0;
}
while(sum<=tail) {
for(int k=1;k<=8;k++) {
int t1 = x[sum] + g[k];
int t2 = y[sum] + h[k];
if(t1>0&&t1<=n&&t2>0&&t2<=m&&b[t1][t2]==0) {
b[t1][t2]=1;
tail++;
x[tail]=t1;
y[tail]=t2;
z[tail]=z[sum]+1;
if(t1==xy&&t2==yx) {
return z[tail];
}else {
ans[t1][t2]=z[tail];
}
}
}
sum++;
}
return -1;
}
int main() {
scanf("%d%d%d%d",&n,&m,&l,&r);
for(int i=1;i<=n;i++) {
for(int j=1;j<=m;j++) {
if(ans[i][j]==0) {
ans[i][j]=bfs(i,j);
}
printf("%d",ans[i][j]);
sum=1,tail=1;
if(j!=m) {
printf(" ");
}else {
printf("\n");
}
}
}
return 0;
}
by Enmin @ 2024-08-08 17:10:41
@Wilson_Lee 谢谢大佬!!!!!!!!!!!!!!!!!!!!!祝你千年好运!!!!!!!