मान लीजिए, हमें h * w आयामों का एक ग्रिड दिया गया है। ग्रिड में प्रत्येक सेल को एक विशिष्ट मान दिया गया है। हमें सम मान वाले कक्षों को अधिकतम करना होगा। ऐसा करने के लिए, हम एक सेल का चयन कर सकते हैं जिसे पहले नहीं चुना गया है, और फिर वर्तमान सेल के मान को 1 से घटाएं और किसी अन्य सेल के मान को 1 से बढ़ा दें जो वर्तमान सेल से सटे लंबवत या क्षैतिज रूप से स्थित है। हम संचालन की संख्या और वृद्धि और कमी के संचालन के सेल नंबरों का प्रिंट आउट लेते हैं। आउटपुट नीचे दिए गए प्रारूप में होगा -
-
संचालन की संख्या
-
पहला (सेल की स्थिति में कमी) - (सेल की स्थिति में वृद्धि)
....
-
nth (सेल की स्थिति में कमी) - (सेल की स्थिति में वृद्धि)
इसलिए, यदि इनपुट h =3, w =3, ग्रिड ={{2, 3, 4}, {2, 0, 1}, {1, 2, 3}} जैसा है, तो आउटपुट होगापी>
4 (0, 1) - (0, 2) (2, 0) - (2, 1) (2, 1) - (2, 2) (0, 2) - (1, 2)
कदम
इसे हल करने के लिए, हम इन चरणों का पालन करेंगे -
Define a new array result that contains a tuple
for initialize i := 0, when i < h, update (increase i by 1), do:
tp := 0
for initialize j := 0, when j < w, update (increase j by 1), do:
if tp > 0, then:
insert tuple(i, j - 1, i, j) at the end of result
grid[i, j] := grid[i, j] + tp
if grid[i, j] mod 2 is same as 1 and j < w-1, then:
grid[i, j] := grid[i, j] - 1
tp := 1
Otherwise
tp := 0
tp := 0
for initialize i := 0, when i < h, update (increase i by 1), do:
if tp > 0, then:
insert tuple(i - 1, w - 1, i, w - 1) at the end of result
grid[i, w - 1] := grid[i, w - 1] + tp
if grid[i, w - 1] mod 2 is same as 1, then:
grid[i, w - 1] := grid[i, w - 1] - 1
tp := 1
Otherwise
tp := 0
print(size of result)
for initialize i := 0, when i < size of result, update (increase i by 1), do:
print('(' + first value of result[i] + ',' + second value of result[i] + '- (' + third value of result[i] + ',' + fourth value of result[i]) उदाहरण
आइए बेहतर समझ पाने के लिए निम्नलिखित कार्यान्वयन देखें -
#include <bits/stdc++.h>
using namespace std;
void solve(int h, int w, vector<vector<int>>grid){
vector<tuple<int,int,int,int>> result;
for(int i = 0; i < h; i++){
int tp = 0;
for(int j = 0; j < w; j++){
if(tp > 0){
result.push_back(make_tuple(i, j-1, i, j));
grid[i][j] += tp;
}
if(grid[i][j]%2 == 1 && j < w-1){
grid[i][j] -= 1;
tp = 1;
}
else
tp = 0;
}
}
int tp = 0;
for(int i = 0; i < h; i++){
if(tp > 0){
result.push_back(make_tuple(i-1, w-1, i, w-1));
grid[i][w-1] += tp;
}
if(grid[i][w-1]%2 == 1){
grid[i][w-1] -= 1;
tp = 1;
}
else
tp = 0;
}
cout << (int)result.size() << endl;
for(int i = 0; i < (int)result.size(); i++){
cout << "(" << get<0>(result[i]) << ", " << get<1>(result[i])
<< ")" << " - (" << get<2>(result[i]) << ", " << get<3>(result[i]) << ")";
cout << '\n';
}
}
int main() {
int h = 3, w = 3 ;
vector<vector<int>> grid = {{2, 3, 4}, {2, 0, 1}, {1, 2, 3}};
solve(h, w, grid);
return 0;
} इनपुट
3, 3, {{2, 3, 4}, {2, 0, 1}, {1, 2, 3}} आउटपुट
4 (0, 1) - (0, 2) (2, 0) - (2, 1) (2, 1) - (2, 2) (0, 2) - (1, 2)