fork download
  1. /*
  2.   Cred : SunnyYeahBoi
  3.   It's my last chance (⌐■_■)
  4.   Problem :
  5. */
  6.  
  7. #include<bits/stdc++.h>
  8.  
  9. using namespace std;
  10.  
  11. #define int long long
  12. #define double long double
  13. #define endl "\n"
  14. #define NAME "a"
  15.  
  16. const int MAXN = 1e3 + 5;
  17. const int inf = 1e18;
  18. const int MOD = 1e9 + 7;
  19.  
  20. void FileInput(){
  21. if(fopen(NAME".inp" , "r") == NULL)
  22. freopen(NAME".inp" , "w" , stdout);
  23. freopen(NAME".inp" , "r" , stdin);
  24. freopen(NAME".out" , "w" , stdout);
  25. }
  26.  
  27. const int dx[] = {1 , -1 , 0 , 0};
  28. const int dy[] = {0 , 0 , 1 , -1};
  29.  
  30. int n , m , k;
  31. int a[MAXN][MAXN];
  32. pair<int , int> start; // một ô ngẫu nhiên
  33.  
  34. bool keep[MAXN][MAXN];
  35.  
  36. void solve(){
  37. cin >> n >> m >> k;
  38.  
  39. int count = 0;
  40. for(int i = 1 ; i <= n ; i++){
  41. for(int j = 1 ; j <= m ; j++){
  42. cin >> a[i][j];
  43. if(a[i][j] == 1){
  44. start = {i , j};
  45. count++;
  46. }
  47. }
  48. }
  49.  
  50. count = count - k;
  51. if(count < 0){
  52. cout << "rotdoituyentinh" << endl;
  53. return;
  54. }
  55.  
  56.  
  57. queue<pair<int , int>> q;
  58. q.push(start);
  59. keep[start.first][start.second] = true;
  60. count--;
  61.  
  62. while(!q.empty()){
  63. pair<int , int> u = q.front();
  64. q.pop();
  65.  
  66. for(int dir = 0 ; dir < 4 ; dir++){
  67. int i = u.first + dx[dir] , j = u.second + dy[dir];
  68. if(count > 0 && a[i][j] == 1 && keep[i][j] == false){
  69. keep[i][j] = true;
  70. count--;
  71. q.push({i , j});
  72. }
  73. }
  74. }
  75.  
  76. for(int i = 1 ; i <= n ; i++){
  77. for(int j = 1 ; j <= m ; j++){
  78. if(a[i][j] == 1 && keep[i][j] == false)
  79. cout << i << " " << j << endl;
  80. }
  81. }
  82. }
  83.  
  84. int32_t main(){
  85. // FileInput();
  86. ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
  87. int t = 1;
  88. // cin >> t;
  89. while(t--)
  90. solve();
  91. return 0;
  92. }
Success #stdin #stdout 0.01s 5288KB
stdin
Standard input is empty
stdout
Standard output is empty