-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathminesweeper.cpp
More file actions
90 lines (74 loc) · 3.02 KB
/
minesweeper.cpp
File metadata and controls
90 lines (74 loc) · 3.02 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
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
void printDbg( const std::vector< std::string>& p_grid ) {
static int turn{1};
std::cerr << "----- Turn " << turn++ << " -----\n";
for ( const auto& line : p_grid ) { std::cerr << "\t" << line << "\n"; }
std::cerr << "------------------\n";
}
int main()
{
int h, w, nb, nb_int{0}, nb_mine{0}, turn{0};
int dx[] = { -1, -1, -1, 0, 0, 1, 1, 1 };
int dy[] = { -1, 0, 1, -1, 1, -1, 0, 1 };
std::cin >> h >> w >> nb; std::cin.ignore();
std::vector< std::string > grid{h};
std::cerr << h << " " << w << " " << nb << "\n";
for ( int i = 0; i < h; i++) {
getline( std::cin, grid[i] );
nb_int += std::count( std::begin(grid[i]), std::end(grid[i]), '?' );
}
while ( nb_mine < nb ) {
if ( nb_int == nb - nb_mine ) {
for ( int i = 0; i < h; ++i )
std::replace( std::begin(grid[i]), std::end(grid[i]), '?', 'X' );
nb_mine = nb;
nb_int = 0;
}
if ( nb_mine != nb ) {
for ( int i = 0; i < h; ++i ) {
for ( int j = 0; j < w; ++j ) {
if ( grid[i][j] >= '1' && grid[i][j] <= '8' )
{
int nb_m{0}, nb_i{0};
for ( int d = 0; d < 8; ++d ) {
int x = j + dx[d];
int y = i + dy[d];
if ( x >= 0 && x < w && y >= 0 && y < h ) {
if ( grid[y][x] == '?' ) ++nb_i;
else if ( grid[y][x] == 'X' ) ++nb_m;
}
}
if ( nb_m == grid[i][j] - '0' ) {
for ( int d = 0; d < 8; ++d ) {
int x = j + dx[d];
int y = i + dy[d];
if ( x >= 0 && x < w && y >= 0 && y < h && grid[y][x] == '?' ) {
grid[y][x] = '.';
--nb_int;
}
}
}
else if ( nb_m + nb_i == grid[i][j] - '0' ) {
for ( int d = 0; d < 8; ++d ) {
int x = j + dx[d];
int y = i + dy[d];
if ( x >= 0 && x < w && y >= 0 && y < h && grid[y][x] == '?' ) {
grid[y][x] = 'X';
--nb_int;
++nb_mine;
}
}
}
}
}
}
}
printDbg(grid);
}
for ( int col = 0; col < w; ++col )
for ( int line = 0; line < h; ++line )
if ( grid[line][col] == 'X' ) std::cout << col << " " << line << std::endl;
}