-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path10189 - Minesweeper
123 lines (116 loc) · 2.67 KB
/
10189 - Minesweeper
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
#include<iostream>
#include<cstring>
using namespace std;
char a[105][105];
int field[105][105];
int dx[] = {1, -1, 0, 0, -1, 1, -1, 1};
int dy[] = {0, 0, 1, -1, 1, 1, -1, -1};
int main() {
int n, m, count = 1;
string s;
while (cin >> n >> m) {
if (n == 0 && m == 0) {
break;
}
for (int i = 0; i < n; i++) {
cin>>s;
for (int j = 0; j < m; j++) {
a[i][j]=s[j];
}
}
memset(field,0,sizeof(field));
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
if(a[i][j]=='*'){
field[i][j]=-1;
}
else{
for(int k=0;k<8;k++){
int x = i+dx[k];
int y = j+dy[k];
if(x>=0 && x<n && y>=0 && y <m && a[x][y]=='*'){
field[i][j]++;
}
}
}
}
}
cout << "Field #" << count << ":" << endl;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (field[i][j] == -1) {
cout << "*";
}
else {
cout << field[i][j];
}
}
cout << endl;
}
count++;
}
}
##########################
#include <iostream>
#include <cstring>
using namespace std;
char a[105][105];
int field[105][105];
bool visited[105][105];
int dx[] = {1, -1, 0, 0, -1, 1, -1, 1};
int dy[] = {0, 0, 1, -1, 1, 1, -1, -1};
int n, m;
void dfs(int i, int j) {
if (i < 0 || i >= n || j < 0 || j >= m || visited[i][j]) {
return;
}
visited[i][j] = true;
field[i][j] = 0;
for (int k = 0; k < 8; k++) {
int x = i + dx[k];
int y = j + dy[k];
if (x >= 0 && x < n && y >= 0 && y < m && a[x][y] == '*') {
field[i][j]++;
}
}
}
int main() {
int count = 1;
string s;
while (cin >> n >> m) {
if (n == 0 && m == 0) {
break;
}
for (int i = 0; i < n; i++) {
cin >> s;
for (int j = 0; j < m; j++) {
a[i][j] = s[j];
visited[i][j] = false; // 初始化訪問矩陣
}
}
memset(field, 0, sizeof(field));
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (!visited[i][j]) {
dfs(i, j);
}
}
}
if (count > 1) {
cout << endl; // 每個場地之間的空行
}
cout << "Field #" << count << ":\n";
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (a[i][j] == '*') {
cout << "*";
} else {
cout << field[i][j];
}
}
cout << endl;
}
count++;
}
return 0;
}