forked from akshitagit/JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbombermanGame.cpp
More file actions
147 lines (125 loc) · 3.47 KB
/
bombermanGame.cpp
File metadata and controls
147 lines (125 loc) · 3.47 KB
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
/**
* Problem link: https://www.hackerrank.com/challenges/bomber-man/problem
*
* Problem statement is too big to place in this file. Please read it on the link given above.
*/
#include <iostream>
#include <vector>
#include <limits>
#include <algorithm>
using namespace std;
void changeAdjacentCell(vector<vector<char>> &board, int row, int col) {
vector< vector<char> > arr(row + 2, vector<char>(col + 2));
arr = board;
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
if (arr[i + 1][j + 1] == '1') {
board[i + 1][j + 1] = '.';
board[i + 1][j] = '.';
board[i + 1][j + 2] = '.';
board[i][j + 1] = '.';
board[i + 2][j + 1] = '.';
}
}
}
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
if (board[i + 1][j + 1] != '.') {
board[i + 1][j + 1] = '2';
}
}
}
}
void state1(vector<vector<char>> &board, int row, int col) {
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
if (board[i + 1][j + 1] == 'O')
board[i + 1][j + 1] = '2';
else
board[i + 1][j + 1] = '.';
}
}
}
void state2(vector<vector<char>> &board, int row, int col) {
state1(board, row, col);
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
if (board[i + 1][j + 1] == '2')
board[i + 1][j + 1] = '1';
else
board[i + 1][j + 1] = '3';
}
}
}
void state3(vector<vector<char>> &board, int row, int col) {
state2(board, row, col);
changeAdjacentCell(board, row, col);
}
void state4(vector<vector<char>> &board, int row, int col) {
state3(board, row, col);
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
if (board[i + 1][j + 1] == '.')
board[i + 1][j + 1] = '3';
else if (board[i + 1][j + 1] == '2')
board[i + 1][j + 1] = '1';
}
}
}
void state5(vector<vector<char>> &board, int row, int col) {
state4(board, row, col);
changeAdjacentCell(board, row, col);
}
void state6(vector<vector<char>> &board, int row, int col) {
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
if (board[i + 1][j + 1] == '2')
board[i + 1][j + 1] = '1';
else if (board[i + 1][j + 1] == '.')
board[i + 1][j + 1] = '3';
}
}
}
int main() {
int row, col, n;
cin >> row >> col >> n;
vector<vector<char>> board(row + 2, vector<char>(col + 2));
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
cin >> board[i + 1][j + 1];
}
}
if (n == 1) {
state1(board, row, col);
} else if (n == 2) {
state2(board, row, col);
} else if (n % 4 == 3) {
state3(board, row, col);
} else if (n % 4 == 0) {
state4(board, row, col);
} else if (n % 4 == 1) {
state5(board, row, col);
} else if (n % 4 == 2) {
state6(board, row, col);
}
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
if (board[i + 1][j + 1] == '.')
cout << '.';
else
cout << 'O';
}
cout << endl;
}
return 0;
}
/*
Sample test case
6 7 3
.......
...O...
....O..
.......
OO.....
OO.....
*/