E. Gojou and Matrix Game
time limit per test
4 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
Marin feels exhausted after a long day of cosplay, so Gojou invites her to play a game!
Marin and Gojou take turns to place one of their tokens on an n×nn×n grid with Marin starting first. There are some restrictions and allowances on where to place tokens:
- Apart from the first move, the token placed by a player must be more than Manhattan distance kk away from the previous token placed on the matrix. In other words, if a player places a token at (x1,y1)(x1,y1), then the token placed by the other player in the next move must be in a cell (x2,y2)(x2,y2) satisfying |x2−x1|+|y2−y1|>k|x2−x1|+|y2−y1|>k.
- Apart from the previous restriction, a token can be placed anywhere on the matrix, including cells where tokens were previously placed by any player.
Whenever a player places a token on cell (x,y)(x,y), that player gets vx, yvx, y points. All values of vv on the grid are distinct. You still get points from a cell even if tokens were already placed onto the cell. The game finishes when each player makes 1010010100 moves.
Marin and Gojou will play n2n2 games. For each cell of the grid, there will be exactly one game where Marin places a token on that cell on her first move. Please answer for each game, if Marin and Gojou play optimally (after Marin's first move), who will have more points at the end? Or will the game end in a draw (both players have the same points at the end)?
Input
The first line contains two integers nn, kk (3≤n≤20003≤n≤2000, 1≤k≤n−21≤k≤n−2). Note that under these constraints it is always possible to make a move.
The following nn lines contains nn integers each. The jj-th integer in the ii-th line is vi,jvi,j (1≤vi,j≤n21≤vi,j≤n2). All elements in vv are distinct.
Output
You should print nn lines. In the ii-th line, print nn characters, where the jj-th character is the result of the game in which Marin places her first token in the cell (i,j)(i,j). Print 'M' if Marin wins, 'G' if Gojou wins, and 'D' if the game ends in a draw. Do not print spaces between the characters in one line.
Example
input
Copy
3 1 1 2 4 6 8 3 9 5 7
output
Copy
GGG MGG MGG
#include <bits/stdc++.h>
using i64 = long long;
constexpr int inf=1E9;
int main() {
//freopen("in.txt","r",stdin);
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int n, k;
std::cin >> n >> k;
std::vector<std::vector<int>> v(n, std::vector<int>(n));
std::vector<std::array<int, 2>> pos(n*n);
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
std::cin>>v[i][j];
v[i][j]--;
pos[v[i][j]]={i,j};
}
}
int pp=-inf,pn=-inf,np=-inf,nn=-inf;
std::vector<std::string> ans(n,std::string(n,'G'));
for(int i=n*n-1;i>=0;i--){
auto [x,y]=pos[i];
if(pp>x+y+k||pn>x-y+k||np>-x+y+k||nn>-x-y+k){continue;}
ans[x][y]='M';
pp=std::max(pp,x+y);
pn=std::max(pn,x-y);
np=std::max(np,-x+y);
nn=std::max(nn,-x-y);
}
for(int i=0;i<n;i++)
std::cout<<ans[i]<<"\n";
return 0;
}
首先,找到肯定赢的位置,然后逐步扩散O(n)
https://codeforces.com/blog/entry/101302
这里还要转换一下距离的比较方式