AcWing
  • 首页
  • 活动
  • 题库
  • 竞赛
  • 商店
  • 应用
  • 文章
    • 题解
    • 分享
    • 问答
  • 吐槽
  • 登录/注册

AcWing 3402. 等差数列--用scanf进行输入    原题链接    中等

作者: 作者的头像   我呼吸了 ,  2022-08-06 10:51:06 ,  所有人可见 ,  阅读 10


2


#include <bits/stdc++.h>

#define x first
#define y second

using namespace std;

typedef pair<int, int> pii;

const int N = 1010, M = 2 * N;

vector<pii> res, p[M];
int n, m, g[N][N], cnt[M];
bool st[M]; //行是1 ~ n, 列是 n + 1 ~  n + m;

int main()
{
    scanf("%d %d", &n, &m);

    for(int i = 1; i <= n; i ++)
    {
        for(int j = 1; j <= m; j ++)
        {
            scanf("%d", &g[i][j]);
            if(g[i][j]) 
            {
                cnt[i] ++, cnt[j + n] ++;
                p[i].push_back({j, g[i][j]});
                p[j + n].push_back({i, g[i][j]});
            }
        }
    }

    queue<int> q;

    for(int i = 1; i <= n; i ++) // 行
    {
        if(cnt[i] >= 2 && cnt[i] < m)
        {
            q.push(i);
            st[i] = true;
        }
    }

    for(int i = n + 1; i <= n + m; i ++) //列
    {
        if(cnt[i] >= 2 && cnt[i] < n)
        {
            q.push(i);
            st[i] = true;
        }
    }

    while(q.size())
    {
        int t = q.front();
        q.pop();

        if(t <= n) //行
        {
            int d = (p[t][1].y - p[t][0].y) / (p[t][1].x - p[t][0].x);
            int a = p[t][1].y - d * p[t][1].x;

            for(int i = 1; i <= m; i ++)
            {
                if(!g[t][i])
                {
                    res.push_back({t, i});
                    g[t][i] = a + i * d;

                    cnt[i + n] ++;
                    p[i + n].push_back({t, g[t][i]});

                    if(cnt[i + n] >= 2 && cnt[i + n] < n && !st[i + n])
                    {
                        q.push(i + n);
                        st[i + n] = true;
                    }
                }
            }
        }
        else //列
        {
            int d = (p[t][1].y - p[t][0].y) / (p[t][1].x - p[t][0].x);
            int a = p[t][1].y - d * p[t][1].x;

            t = t - n;

            for(int i = 1; i <= n; i ++)
            {
                if(!g[i][t])
                {
                    res.push_back({i, t});
                    g[i][t] = a + i * d;

                    cnt[i] ++;
                    p[i].push_back({t, g[i][t]});

                    if(cnt[i] >= 2 && cnt[i] < m && !st[i])
                    {
                        q.push(i);
                        st[i] = true;
                    }
                }
            }
        }
    }

    sort(res.begin(), res.end());

    for(int i = 0; i < res.size(); i ++)
    {
        pii t = res[i];

        printf("%d %d %d\n", t.x, t.y, g[t.x][t.y]);
    }
    return 0;
}

0 评论

你确定删除吗?

© 2018-2022 AcWing 版权所有  |  京ICP备17053197号-1
用户协议  |  常见问题  |  联系我们
AcWing
请输入登录信息
更多登录方式: 微信图标 qq图标
请输入绑定的邮箱地址
请输入注册信息