#define _CRT_SECURE_NO_WARNINGS #pragma comment(linker, "/stack:16777216") #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; #define FOR(i, a, b) for(int i=(a);i<(b);i++) #define RFOR(i, b, a) for(int i=(b)-1;i>=(a);--i) #define FILL(A,value) memset(A,value,sizeof(A)) #define ALL(V) V.begin(), V.end() #define SZ(V) (int)V.size() #define PB push_back #define MP make_pair #define Pi 3.14159265358979 #define x0 ikjnrmthklmnt #define y0 lkrjhkltr #define y1 ewrgrg typedef long long Int; typedef unsigned long long UInt; typedef vector VI; typedef pair PII; const int INF = 1000000000; const int MAX = 20007; const int MAX2 = 1000000; const int MAXD = 20; const int BASE = 1000000000; const int MOD = 1000000007; int a[207][207]; int val[207][207]; int cnt[207][207]; int n , r , k; bool ok ( int x , int y) { return x >= 0 && y >= 0 && x < n && y < n; } int c[57]; Int best = -1; vector Res; set S; Int solve(int a , int b) { int K = k; FILL(cnt , 0); FOR(i,0,n) { FOR(j,0,n) { S.insert(MP(-a * val[i][j] + b * cnt[i][j] , i * 256 + j)); } } vector res; Int score = 0; while (S.size()) { pair p = *S.begin(); S.erase(S.begin()); int x = p.second / 256; int y = p.second % 256; if (K < cnt[x][y]) continue; res.push_back(MP(x, y)); K -= cnt[x][y]; score += val[x][y]; cnt[x][y] = INF; FOR(dx,-1,2) { FOR(dy,-1,2) { if (dx == 0 && dy == 0) continue; int xx = x; int yy = y; FOR(it,0,r) { xx += dx; yy += dy; if (!ok(xx , yy)) break; if (cnt[xx][yy] == INF) continue; S.erase(MP(-a * val[xx][yy] + b * cnt[xx][yy] , xx * 256 + yy)); ++cnt[xx][yy]; S.insert(MP(-a * val[xx][yy] + b * cnt[xx][yy] , xx * 256 + yy)); } } } } if (score > best) { best = score; Res = res; } return score; } vector V; void solve2() { int K = k; FILL(cnt , 0); random_shuffle(ALL(V)); vector res; Int score = 0; FOR(i,0,V.size()) { int x = V[i].first; int y = V[i].second; //cout << cnt[x][y] << ' ' << p.first.first << ' ' << p.first.second << endl; if (K < cnt[x][y]) continue; res.push_back(MP(x, y)); K -= cnt[x][y]; score += val[x][y]; cnt[x][y] = INF; FOR(dx,-1,2) { FOR(dy,-1,2) { if (dx == 0 && dy == 0) continue; int xx = x; int yy = y; FOR(it,0,r) { xx += dx; yy += dy; if (!ok(xx , yy)) break; if (cnt[xx][yy] == INF) continue; ++cnt[xx][yy]; } } } } if (score > best) { best = score; Res = res; } } Int med = 0; void solve3() { int K = k; FILL(cnt , 0); vector > V; FOR(i,0,n) { FOR(j,0,n) { V.push_back(MP(val[i][j] + rand() % med , MP(i,j))); } } sort(ALL(V)); reverse(ALL(V)); vector res; Int score = 0; FOR(i,0,V.size()) { int x = V[i].second.first; int y = V[i].second.second; //cout << cnt[x][y] << ' ' << p.first.first << ' ' << p.first.second << endl; if (K < cnt[x][y]) continue; res.push_back(MP(x, y)); K -= cnt[x][y]; score += val[x][y]; cnt[x][y] = INF; FOR(dx,-1,2) { FOR(dy,-1,2) { if (dx == 0 && dy == 0) continue; int xx = x; int yy = y; FOR(it,0,r) { xx += dx; yy += dy; if (!ok(xx , yy)) break; if (cnt[xx][yy] == INF) continue; ++cnt[xx][yy]; } } } } if (score > best) { best = score; Res = res; } } int Rand() { return abs(rand() + (rand() << 16)); } int main() { //freopen("in.txt" , "r" , stdin); //freopen("out.txt" , "w" , stdout); freopen("queens.in" , "r" , stdin); freopen("queens.out" , "w" , stdout); cin >> n >> r >> k; FOR(i,0,n) { FOR(j,0,n) { V.push_back(MP(i, j)); } } FOR(i,0,n) { FOR(j,0,n) { cin >> a[i][j]; //a[i][j] = 50; c[a[i][j]]++; } } FOR(i,0,n) { FOR(j,0,n) { val[i][j] = 4 * a[i][j]; FILL(c,0); c[a[i][j]] = 4; FOR(dx,-1,2) FOR(dy,-1,2) { if (dx == 0 && dy == 0) continue; int x = i; int y = j; FOR(it,0,r) { x += dx; y += dy; if (!ok(x, y)) break; val[i][j] += a[x][y]; c[a[x][y]]++; } } int mult = 0; FOR(i,0,57) { mult = max(mult , c[i]); } val[i][j] *= mult; } } FOR(i,0,n) { FOR(j,0,n) { med += val[i][j]; } } med /= n * n; if (n <= 10) { while (1.0 * clock() / CLOCKS_PER_SEC < 4.6) { solve2(); } } solve(0,1); solve(1,0); int l = 1; vector > A; while(l < 500000) { A.push_back(MP(solve(1 , l) , l) ); l *= 5; } sort(ALL(A)); reverse(ALL(A)); if (n <= 20) { while (1.0 * clock() / CLOCKS_PER_SEC < 2) { solve3(); } } while (1.0 * clock() / CLOCKS_PER_SEC < 4.5) { solve(1 , Rand() % (2 * A[0].second)); } //cout << best << endl; //cout << Res.size() << endl; FOR(i,0,Res.size()) { cout << Res[i].first + 1 << ' ' << Res[i].second + 1 << endl; } return 0; }