#include<bits/stdc++.h>
using namespace std;
vector<vector<int> > create(int n)
{
int c = n / 2;
vector<vector<int> > ans(n, vector<int> (n));
vector<pair<int,int> > moves(n * n + 1, {2, 2});
for(int i = 1; i <= n; i += 2)
moves[i * i] = {-1, 0}, moves[i * i + 1] = {0, 1};
for(int i = 2; i <= n; i += 2)
moves[i * i] = {1, 0}, moves[i * i + 1] = {0, -1};
for(int i = 1; i < n; i ++)
{
int idx = (i * i + (i + 1) * (i + 1) + 1) / 2;
if(i % 2 == 1)
moves[idx] = {1, 0};
else
moves[idx] = {-1, 0};
}
for(int i = 1; i <= n * n; i ++)
if(moves[i].first == 2) moves[i] = moves[i - 1];
int x = c, y = c;
for(int i = 1; i <= n * n; i++)
{
ans[x][y] = i;
x += moves[i].first;
y += moves[i].second;
}
return ans;
}
int main()
{
int n, m, k;
cin >> n >> m >> k;
int ans[n][m];
for(int i = 0; i < n; i ++)
for(int j = 0; j < m ; j ++)
ans[i][j] = 1e8;
while(k--)
{
int x, y, d;
cin >> x >> y >> d;
x--, y--; // how much row and cols do we need
int sz = max(max(x, n - x - 1), max(y, m - y - 1)) * 2 + 1;
vector<vector<int> > grid = create(sz);
if(d)
for(vector<int> &vec : grid)
reverse(vec.begin(), vec.end());
for(int i = 0; i < n; i ++)
for(int j = 0; j < m; j ++)
{
int x1 = i - x + sz / 2, y1 = j - y + sz / 2;
ans[i][j] = min(ans[i][j], grid[x1][y1]);
}
}
for(int i = 0; i < n; i ++)
for(int j = 0; j < m; j ++)
cout << ans[i][j] << " \n"[j == m - 1];
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
57 ms |
572 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |