社区讨论

(正面下跪)30pts求条!

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

讨论操作

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

当前回复
2 条
当前快照
1 份
快照标识符
@mhqm3dd1
此快照首次捕获于
2025/11/09 02:22
4 个月前
此快照最后确认于
2025/11/16 14:04
4 个月前
查看原帖
CPP
#include<bits/stdc++.h>
using namespace std;
struct cord{
    int x,y,s;
};
int n,m,x,y;
queue<cord> bfs;
long long int ans[420][420];

int mov[8][2] = {
    {1,2},{1,-2},{2,1},{2,-1},
    {-1,2},{-1,-2},{-2,-1},{-2,1}
};
bool isValid(int i,int j){
    if (i>0&&j>0&&i<=n&&j<=m)return true;return false;
}
int main(){
    cin >> n >> m >>x >> y;
    bfs.push((cord){1,1,0});
    cord now ;
    while (!bfs.empty()){
        now = bfs.front();
        bfs.pop();
        // printf("NowBFS: x=%d y=%d s=%d\n",now.x,now.y,now.s);
        for (int i = 0;i<8;i++){
            int tx = now.x+mov[i][0];
            int ty = now.y+mov[i][1];
            if (isValid(tx,ty)&&!ans[tx][ty]){
            	// printf("|--Validated tx = %d ty = %d \n",tx,ty);
                bfs.push((cord){tx,ty,now.s+1});
                ans[tx][ty] = now.s+1;
            } else {
            	// printf("|-----------------------Not tx = %d ty = %d  ans[tx][ty] = %d isValid? %d\n",tx,ty,ans[tx][ty],isValid(tx,ty));
			}
            
        }
    }
    for(int i = 1;i<=n;i++){
        for (int j = 1;j<=m;j++){
            int ansp = ans[i][j];
            if (1==j&&i==1){
                cout << 0 << ' ';continue;
            }
            printf("%d ",ansp==0?-1:ansp);
            
        }
        cout << endl;
    }
    return 0;
}
可能无法及时回复,明天上午才会回来,致歉(下跪)

回复

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

正在加载回复...