AcWing 3402. 等差数列--用scanf进行输入
原题链接
中等
作者:
我呼吸了
,
2022-08-06 10:51:06
,
所有人可见
,
阅读 10
#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;
}