1 条题解

  • 0
    @ 2026-10-23 18:34:30

    P76013 中心标记 — 题解

    专题: 模拟与枚举
    适用题目: 新生 OJ 本地题包中的 P76013,不要直接用于外站同名题。

    解题思路

    扫描矩阵找到唯一的 1,它到中心点的最短步数就是行距离与列距离之和。

    复杂度

    时间 O(25)O(25),额外空间 O(1)O(1)。

    易错点

    行列按 1 到 5 编号,中心坐标是 (3,3)。

    C11 参考程序

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <ctype.h>
    #include <limits.h>
    
    int main(void) {
        int x, ans = 0;
        for (int i = 1; i <= 5; i++) for (int j = 1; j <= 5; j++) {
            scanf("%d", &x);
            if (x == 1) ans = abs(i-3)+abs(j-3);
        }
        printf("%d\n", ans);
        return 0;
    }
    

    C++17 参考程序

    #include <bits/stdc++.h>
    using namespace std;
    using ll = long long;
    #define nl '\n'
    
    int main() {
        ios::sync_with_stdio(false);
        cin.tie(nullptr);
        int x,ans=0;
        for(int i=1;i<=5;i++)for(int j=1;j<=5;j++){
            cin>>x;
            if(x)ans=abs(i-3)+abs(j-3);
        }
        cout<<ans<<nl;
        return 0;
    }
    

    题解建议设置为训练结束后开放。普通判题无法验证是否实际使用指定函数或 STL。

    • 1

    信息

    ID
    1782
    时间
    2000ms
    内存
    256MiB
    难度
    1
    标签
    提交数
    1
    已通过
    1
    上传者