fork(2) download
  1. /*
  2. Author: Ritik Patel
  3. */
  4.  
  5. //&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& LIBRARIES &&&&&&&&&&&&&&&&&&&&&&&&&&&
  6.  
  7. #include <bits/stdc++.h>
  8. using namespace std;
  9.  
  10. //&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& DEFINES &&&&&&&&&&&&&&&&&&&&&&&&&&&
  11.  
  12. // #define int long long int
  13. // #define ll long long int
  14. #define all(i) i.begin(), i.end()
  15. #define SZ(a) (int)a.size()
  16.  
  17. //&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& CODE &&&&&&&&&&&&&&&&&&&&&&&&&&&
  18.  
  19. const int dx4[4] = {0, 0, 1, -1};
  20. const int dy4[4] = {-1, 1, 0, 0};
  21.  
  22. int n;
  23. vector<vector<int>> g, vis;
  24. vector<vector<pair<int, int>>> groups;
  25. int sze = 0;
  26. vector<pair<int, int>> curr;
  27.  
  28. bool valid(int i, int j) {
  29. if (i >= 0 and i < n and j >= 0 and j < n) return true;
  30. return false;
  31. }
  32.  
  33. void dfs(int i, int j) {
  34. sze++;
  35. vis[i][j] = 1;
  36. curr.emplace_back(i, j);
  37. for (int k = 0; k < 4; ++k) {
  38. int nx = i + dx4[k];
  39. int ny = j + dy4[k];
  40. if (valid(nx, ny) and g[nx][ny] == 1 and !vis[nx][ny]) {
  41. dfs(nx, ny);
  42. }
  43. }
  44. }
  45.  
  46. void solve() {
  47. cin >> n;
  48. g.assign(n, vector<int>(n));
  49. vis.assign(n, vector<int>(n, 0));
  50. groups.assign(105, {});
  51. for (int i = 0; i < n; ++i) {
  52. for (int j = 0; j < n; ++j) {
  53. cin >> g[i][j];
  54. }
  55. }
  56. for (int i = 0; i < n; ++i) {
  57. for (int j = 0; j < n; ++j) {
  58. if (g[i][j] == 0 or vis[i][j]) continue;
  59. sze = 0; curr.clear();
  60. dfs(i, j);
  61. groups[sze] = curr;
  62. }
  63. }
  64. int p; cin >> p;
  65. if (groups[p].empty()) {
  66. cout << -1 << " "<< -1 << '\n';
  67. return;
  68. }
  69. sort(all(groups[p]));
  70. cout << groups[p][0].first << " " << groups[p][0].second << '\n';
  71. }
  72.  
  73. int32_t main(){
  74. // freopen("input.txt","r",stdin);
  75. // freopen("output.txt","w",stdout);
  76. ios_base::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);
  77. int T = 1;
  78. cin >> T;
  79. for(int i = 1; i <= T; ++i){
  80. // cout << "Case #" << i << ": ";
  81. solve();
  82. }
  83. return 0;
  84. }
  85.  
  86. /*
  87. Sample inp
  88. */
Success #stdin #stdout 0s 4500KB
stdin
Standard input is empty
stdout
-1 -1