社区讨论

###求解下载下来数据本地测试都对,但是wa声一片啊

P1443马的遍历参与者 3已保存回复 4

讨论操作

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

当前回复
4 条
当前快照
1 份
快照标识符
@mi6ydsmt
此快照首次捕获于
2025/11/20 12:50
4 个月前
此快照最后确认于
2025/11/20 12:50
4 个月前
查看原帖
以下是代码,按照讨论有人说的printf(%-5d)也不行啊
CPP
#include<cstdio>
#include<queue>
using namespace std;
int board[201][201],step[201][201];
int o[8][2]={{-1,-2},{-1,2},{1,-2},{1,2},{-2,-1},{-2,1},{2,-1},{2,1}};
queue<int>h;
queue<int>l;
int n,m,starth,startl;
void bfs(int ,int);
int main()
{
    scanf("%d%d%d%d",&n,&m,&starth,&startl);
    for(int i=1;i<=n;i++)
            for(int j=1;j<=m;j++)
            {
                    board[i][j]=0;
                    step[i][j]=0;
            }//board与step数组初始化 
    bfs(starth,startl);
    printf("\n\n");
    for(int i=1;i<=n;i++)
    {
            for(int j=1;j<=m;j++)
            {
                    if(step[i][j]==0)
                    {
                                     if(i==starth and j==startl)
                                                  printf("0 ");
                                     else
                                         printf("-1 ");
                    }
                    else
                        printf("%-5d ",step[i][j]);
            }
            printf("\n");
    }
    return 0;
}
void bfs(int x,int y)
{
     board[x][y]=1;
     h.push(x);
     l.push(y);
     int q1,q2;
     while(h.size()!=0)
     { 
                      for(int i=0;i<=7;i++)
                      {
                              q1=h.front()+o[i][0];
                              q2=l.front()+o[i][1];
                              if(board[q1][q2]==0 and q1>=1 and q1<=n and q2>=1 and q2<=m)//是否遍历过以及边界条件!! 
                              {
                                                  board[q1][q2]=1;//打标记 
                                                  h.push(q1);
                                                  l.push(q2);
                                                  step[q1][q2]=step[h.front()][l.front()]+1;
                              }
                      }
                      h.pop();
                      l.pop();
     }
     return ;
}

回复

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

正在加载回复...