-
Notifications
You must be signed in to change notification settings - Fork 0
/
new.cpp
209 lines (158 loc) · 5.69 KB
/
new.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#include <bits/stdc++.h>
using namespace std;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int m, n, k;
cin >> m >> n >> k;
queue <pair<int, int> > turtle_positions;
vector<vector<bool> > counted(m, vector<bool>(n, false));
set<pair<int, int> > turtle_set;
vector<vector<int> > grid(m, vector<int>(n));
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
cin >> grid[i][j];
if(grid[i][j]){
pair <int, int> pos(i, j);
turtle_positions.push(pos);
}
}
}
long int c_turtles = 0;
while(!turtle_positions.empty()){
pair <int, int> pos = turtle_positions.front();
turtle_positions.pop();
int top_left_i = pos.first-1, top_left_j = pos.second-1; bool top_left_flag = true;
int top_right_i = pos.first-1, top_right_j = pos.second+1; bool top_right_flag = true;
int bottom_left_i = pos.first+1, bottom_left_j = pos.second-1; bool bottom_left_flag = true;
int bottom_right_i = pos.first+1, bottom_right_j = pos.second+1; bool bottom_right_flag = true;
for(int temp_k = 1; temp_k <= k && (top_left_flag || top_right_flag || bottom_left_flag || bottom_right_flag); temp_k++){
// if(i<=m-1 && j<=n-1){
// if(grid[i][j] && !counted[i][j]){
// c_turtles++;
// counted[i][j] = true;
// }
// }
// else
// break;
if(top_left_i >= 0 && top_left_j >= 0 && top_left_flag){
if(grid[top_left_i][top_left_j] && !counted[top_left_i][top_left_j]){
c_turtles++;
counted[top_left_i][top_left_j] = true;
if(!counted[pos.first][pos.second]){
counted[pos.first][pos.second] = true;
c_turtles++;}
}
top_left_i--, top_left_j--;
}
else
top_left_flag = false;
if(top_right_i >= 0 && top_right_j <= n-1 && top_right_flag){
if(grid[top_right_i][top_right_j] && !counted[top_right_i][top_right_j]){
c_turtles++;
counted[top_right_i][top_right_j] = true;
if(!counted[pos.first][pos.second]){
counted[pos.first][pos.second] = true;
c_turtles++;}
}
top_right_i--, top_right_j++;
}
else
top_right_flag = false;
if(bottom_left_i <= m-1 && bottom_left_j >= 0 && bottom_left_flag){
if(grid[bottom_left_i][bottom_left_j] && !counted[bottom_left_i][bottom_left_j]){
c_turtles++;
counted[bottom_left_i][bottom_left_j] = true;
if(!counted[pos.first][pos.second]){
counted[pos.first][pos.second] = true;
c_turtles++;}
}
bottom_left_i++, bottom_left_j--;
}
else
bottom_left_flag = false;
if(bottom_right_i <= m-1 && bottom_right_j <= n-1 && bottom_right_flag){
if(grid[bottom_right_i][bottom_right_j] && !counted[bottom_right_i][bottom_right_j]){
c_turtles++;
counted[bottom_right_i][bottom_right_j] = true;
if(!counted[pos.first][pos.second]){
counted[pos.first][pos.second] = true;
c_turtles++;}
}
bottom_right_i++, bottom_right_j++;
}
else
bottom_right_flag = false;
}
}
cout << c_turtles << "\n";
return 0;
}
/*
struct Node {
char key;
Node *left, *right;
Node(char ch) {
this.key = ch;
this.left = this.right = NULL;
}
}
class BSTOfCharacter {
private:
Node *root = NULL;
public:
//Constructor:
BSTOfCharacter(): {}
BSTOfCharacter(char ch): root(ch) {}
bool insert(char ch, Node* current) {
if (root == nullptr) {
this->root = new Node(ch);
} else {
this->Insert(ch, this->root);
}
}
void Insert(int val, TreeNode*& node) {
if(val <= node->data){
if(node->left == nullptr){
// Make a new node as the left child of this node
node->left = new Node(val);
} else{
// Recursively call Insert() on this node's left child
this->Insert(val, node->left);
}
} else{
if(node->right == nullptr){
// Make a new node as the right child of this node
node->right = new Node(val);
} else{
// Recursively call Insert() on this node's right child
this->Insert(val, node->right);
}
}
}
void printTree() {
if (root == NULL) {
return;
} else {
stack <Node*> s(root);
vector<Node*> node;
//Loop untill the stack is empty
while(!s.empty()) {
top = s.top();
if (top->left == NULL && top->right == NULL) {
nodes.push_back(top);
s.pop();
}
if (top->right)
s.push(s->right)
if (top->left)
s.push(s->left)
}
for (auto node : nodes) {
cout << node->key << " ";
}
}
}
};
*/