社区讨论

输出格式错还有TLE??

P1443马的遍历参与者 2已保存回复 1

讨论操作

快速查看讨论及其快照的属性,并进行相关操作。

当前回复
1 条
当前快照
1 份
快照标识符
@mi7per4h
此快照首次捕获于
2025/11/21 01:27
4 个月前
此快照最后确认于
2025/11/21 01:27
4 个月前
查看原帖
大佬帮忙看一下我这个题为什么WA啊??输出格式有问题吗??还有为什么TLE??
CPP
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<queue>
#include<cstring>
#include<iomanip>
using namespace std;
struct node{
    int x,y;
    int step;
}k;
int n,m,n1,m1,n2,m2;
int a[401][401];
bool vis[401][401];
int dir[2][8]={{1,1,2,2,-1,-1,-2,-2},{2,-2,1,-1,2,-2,1,-1}};
void bfs(node s)
{
	node next,now;
    for(int i=1;i<=n;i++)
    for(int j=1;j<=m;j++)
    {
    	vis[i][j]=0;
	}
	vis[n1][m1]=0;
    queue <node> q;
    now.x=s.x;
    now.y=s.y;
    now.step=s.step;
    //int n2=s.x,m2=s.y;
    q.push(now);
    vis[now.x][now.y]=1;
    while(q.size())
    {
        now=q.front();q.pop();
        if(now.x==n1&&now.y==m1)
        {
            a[n2][m2]=now.step;
            return ;
        }
        for(int i=0;i<8;i++)
        {
            int xx=now.x+dir[0][i];
            int yy=now.y+dir[1][i];
            int step=now.step+1;
            if(xx<=n&&xx>0&&yy<=m&&yy>0&&!vis[xx][yy])
            {
                vis[xx][yy]=1;
                next.x=xx;next.y=yy;
                next.step=step;
                q.push(next);   
            }
        }
       
    }
}
void read(int &x)
{
    char c=getchar();
    int p=1;x=0;
    while(c<'0'||c>'9')
    {
        if(c=='-') p=-1;
        c=getchar();
    }
    while(c>='0'&&c<='9')
    {
        x=x*10+c-'0';
        c=getchar();
    }
    x*=p;
}
int main()
{
//	freopen("jinx.out","w",stdout);
    read(n);read(m);
    read(n1);read(m1);
    for(int i=1;i<=n;i++)
    for(int j=1;j<=m;j++)
     a[i][j]=-1;
    for(int i=1;i<=n;i++)
    for(int j=1;j<=m;j++)
    {
        k.x=i;k.y=j;
        k.step=0;
        n2=i;m2=j;
        bfs(k);
    }
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=m;j++)
         printf("%-5d",a[i][j]);
        cout<<endl;
    }
    return 0;
}

回复

1 条回复,欢迎继续交流。

正在加载回复...