/home/toolbox/public_html/solutions/102/10279/a.cpp
1 #include <cstdio>
2 #include <iostream>
3 #include <vector>
4 #include <cstring>
5 #include <cstdlib>
6
7 using namespace std;
8
9 #define DEBUG
10 //#undef DEBUG //uncomment this line to pull out print statements
11 #ifdef DEBUG
12 #define TAB '\t'
13 #define debug(a, end) cout << #a << ": " << a << end
14 #else
15 #define debug(a, end)
16 #endif
17
18 typedef pair<int, int> point;
19 typedef long long int64; //for clarity
20 typedef vector<int> vi; //?
21 typedef vector<point> vp; //?
22 template<class T> void chmin(T &t, T f)
23 {
24 if (t > f) t = f; //change min
25 }
26 template<class T> void chmax(T &t, T f)
27 {
28 if (t < f) t = f; //change max
29 }
30
31 #define UN(v) SORT(v),v.erase(unique(v.begin(),v.end()),v.end())
32 #define SORT(c) sort((c).begin(),(c).end())
33 #define FOR(i,a,b) for (int i=(a); i < (b); i++)
34 #define REP(i,n) FOR(i,0,n)
35 #define CL(a,b) memset(a,b,sizeof(a))
36 #define CL2d(a,b,x,y) memset(a, b, sizeof(a[0][0])*x*y)
37
38 /*global variables*/
39 char graph[12][12];
40 char area[12][12];
41 const char bomb = '*';
42 int n;
43
44 int rangex[] = {1, 1, 0, -1, -1, -1, 0, 1};
45 int rangey[] = {0, -1, -1, -1, 0, 1, 1, 1};
46 /*global variables*/
47
48 void dump()
49 {
50 //dump data
51 for (int i = 1; i < n; ++i)
52 {
53 for (int j = 1; j < n; ++j)
54 {
55 printf("%c", graph[i][j]);
56 }
57 cout << endl;
58 }
59 }
60
61 bool getInput()
62 {
63 //get input
64 scanf("%d ", &n);
65 CL2d(graph, 0, 12, 12); //zero out the map
66 CL2d(area, 0, 12, 12); //zero out the map
67 //debug(width, TAB); debug(height, endl);
68
69 n += 1;
70 for (int i = 1; i < n; ++i)
71 {
72 for (int j = 1; j < n; ++j)
73 {
74 scanf("%c%*[ \n]", &graph[i][j]);
75 if (graph[i][j] == '.')
76 graph[i][j] = 0;
77 }
78 }
79
80 for (int i = 1; i < n; ++i)
81 for (int j = 1; j < n; ++j)
82 scanf("%c%*[ \n]", &area[i][j]);
83 //dump();
84
85 return true;
86 }
87
88 void addonetoneighbors(int x, int y)
89 {
90 //process input
91 for (int i = 0; i < 8; ++i)
92 {
93 if (graph[x+rangex[i]][y+rangey[i]] != bomb)
94 graph[x+rangex[i]][y+rangey[i]]++;
95 }
96 }
97
98 void show_mines()
99 {
100 for (int i = 1; i < n; ++i)
101 {
102 for (int j = 1; j < n; ++j)
103 {
104 if (graph[i][j] == bomb)
105 {
106 area[i][j] = 'x';
107 graph[i][j] = bomb-'0';
108 }
109 }
110 }
111 }
112
113 int main()
114 {
115 int num_cases;
116 scanf("%d\n", &num_cases);
117 bool show_bombs = false;
118 while (num_cases-- > 0)
119 {
120 getInput();
121 for (int i = 1; i < n; ++i)
122 {
123 for (int j = 1; j < n; ++j)
124 {
125 if (graph[i][j] == bomb)
126 addonetoneighbors(i, j);
127 }
128 }
129
130 /*output*/
131 for (int i = 1; i < n; ++i)
132 {
133 for (int j = 1; j < n; ++j)
134 {
135 if (area[i][j] == 'x')
136 {
137 if (graph[i][j] == bomb)
138 {
139 show_bombs = true;
140 show_mines();
141 }
142 }
143 }
144
145 }
146 for (int i = 1; i < n; ++i)
147 {
148 for (int j = 1; j < n; ++j)
149 {
150 if (area[i][j] == 'x')
151 {
152 printf("%c", graph[i][j]+'0');
153 }
154 else
155 printf(".");
156 }
157 if (i+1 != n)
158 cout << endl;
159 }
160
161
162 show_bombs = false;
163 /*output*/
164 if (num_cases != 0)
165 cout << endl << endl;
166 }
167 }
168