Skip to content

Commit a79fca4

Browse files
committed
tco 04 semi round 3 200
1 parent 35e7a37 commit a79fca4

File tree

2 files changed

+250
-0
lines changed

2 files changed

+250
-0
lines changed

Unblur.cpp

+98
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
#include <iostream>
2+
#include <sstream>
3+
#include <vector>
4+
#include <algorithm>
5+
#include <set>
6+
#include <map>
7+
#include <queue>
8+
#include <cstring>
9+
#include <climits>
10+
#include <cstdio>
11+
using namespace std;
12+
typedef pair<int,int> pi;
13+
typedef vector<int> vi;
14+
typedef vector<vi> vvi;
15+
typedef vector<string> vs;
16+
typedef vector<vs> vvs;
17+
18+
class Unblur{
19+
public:
20+
vector <string> original(vector <string> blurred) {
21+
int n = blurred.size(), m = blurred[0].size();
22+
vvi M(n, vi(m, 0)), A = M;
23+
vs R(n, string(m, '.'));
24+
25+
for(int i = 0; i < n; i++)
26+
for(int j = 0; j < m; j++)
27+
M[i][j] = blurred[i][j]-'0';
28+
29+
for(int i = 1; i <= n-2; i++)
30+
for(int j = 1; j <= m-2; j++){
31+
int f = 0;
32+
for(int ii = max(0, i-2); ii <= i; ii++)
33+
for(int jj = max(0, j-2); jj <= j; jj++)
34+
f += A[ii][jj];
35+
36+
f = M[i-1][j-1]-f;
37+
if(f == 1){
38+
R[i][j] = '#';
39+
A[i][j] = 1;
40+
}
41+
}
42+
43+
return R;
44+
}
45+
46+
// BEGIN CUT HERE
47+
public:
48+
void run_test(int Case) { if ((Case == -1) || (Case == 0)) test_case_0(); if ((Case == -1) || (Case == 1)) test_case_1(); if ((Case == -1) || (Case == 2)) test_case_2(); if ((Case == -1) || (Case == 3)) test_case_3(); if ((Case == -1) || (Case == 4)) test_case_4(); }
49+
private:
50+
template <typename T> string print_array(const vector<T> &V) { ostringstream os; os << "{ "; for (typename vector<T>::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); }
51+
void verify_case(int Case, const vector <string> &Expected, const vector <string> &Received) { cerr << "Test Case #" << Case << "..."; if (Expected == Received) cerr << "PASSED" << endl; else { cerr << "FAILED" << endl; cerr << "\tExpected: " << print_array(Expected) << endl; cerr << "\tReceived: " << print_array(Received) << endl; } }
52+
void test_case_0() { string Arr0[] = { "1221",
53+
"1221",
54+
"1221" }; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); string Arr1[] = { "....", ".##.", "...." }; vector <string> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); verify_case(0, Arg1, original(Arg0)); }
55+
void test_case_1() { string Arr0[] = { "00000",
56+
"00000",
57+
"00000",
58+
"00000" }; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); string Arr1[] = { ".....", ".....", ".....", "....." }; vector <string> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); verify_case(1, Arg1, original(Arg0)); }
59+
void test_case_2() { string Arr0[] = { "0011212121100",
60+
"0123333333210",
61+
"0123333333210",
62+
"1233333333321",
63+
"1233333333321",
64+
"1233333333321",
65+
"0112121212110" } ; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); string Arr1[] = { ".............", "...#.#.#.#...", "..#.#.#.#.#..", ".............", ".#.#.#.#.#.#.", "..#.#.#.#.#..", "............." }; vector <string> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); verify_case(2, Arg1, original(Arg0)); }
66+
void test_case_3() { string Arr0[] = { "1233321000000000123332100000000000000000000",
67+
"1244422233222332334343323322232112332223321",
68+
"1255523344343443545343434434343233432334432",
69+
"0033303455465775633011445546454355753457753",
70+
"0033303333364543533011433336333364521346542",
71+
"0033303455464532445343545546454355753446542",
72+
"0022202344342200234343434434343233432323221",
73+
"0011101233221100123332223322232112332211111" } ; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); string Arr1[] = { "...........................................", ".#####...........#####.....................", "...#...####.####.#...#.####.###..####.####.", "...#...#..#.#..#.#.....#..#.#..#.#....#..#.", "...#...#..#.####.#.....#..#.#..#.###..####.", "...#...#..#.#....#...#.#..#.#..#.#....#.#..", "...#...####.#....#####.####.###..####.#..#.", "..........................................." }; vector <string> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); verify_case(3, Arg1, original(Arg0)); }
74+
void test_case_4() { string Arr0[] = { "0000123210000",
75+
"0012456542100",
76+
"0135789875310",
77+
"0258988898520",
78+
"1479865689741",
79+
"2589742479852",
80+
"2589742479852",
81+
"1479865689741",
82+
"0258988898520",
83+
"0135789875310",
84+
"0012456542100",
85+
"0000123210000" }
86+
; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); string Arr1[] = { ".............", ".....###.....", "...#######...", "..#########..", "..####.####..", ".####...####.", ".####...####.", "..####.####..", "..#########..", "...#######...", ".....###.....", "............." }; vector <string> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); verify_case(4, Arg1, original(Arg0)); }
87+
88+
// END CUT HERE
89+
90+
};
91+
92+
// BEGIN CUT HERE
93+
int main(){
94+
95+
Unblur ___test;
96+
___test.run_test(-1);
97+
}
98+
// END CUT HERE

Unblur.html

+152
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
<html><body bgcolor="#000000" text="#ffffff"><table><tr><td colspan="2"><h3>Problem Statement</h3></td></tr><tr><td>&#160;&#160;&#160;&#160;</td><td><p>
2+
A simple way to blur an image is to replace each pixel
3+
with the average of it and its neighboring pixels.
4+
The value of each pixel in the blurred image is computed
5+
by adding the values of the 3x3 region centered at the
6+
corresponding pixel of the original image, and dividing
7+
by 9.
8+
</p>
9+
<p>
10+
When computing the value of pixels on the border, the 3x3 region will
11+
fall partially outside of the original image. Assume that
12+
pixels outside of the original image are black.
13+
</p>
14+
15+
<p>
16+
For example, given the following original image:
17+
</p>
18+
19+
<center>
20+
<img src="http://www.topcoder.com/contest/problem/UnBlur/unblur_fig1a.gif"></img>
21+
</center>
22+
23+
<p>
24+
the algorithm described above results in the following blurred image:
25+
</p>
26+
27+
<center>
28+
<img src="http://www.topcoder.com/contest/problem/UnBlur/unblur_fig1b.gif"></img>
29+
</center>
30+
31+
<p>
32+
Write a method that will, given a blurred image, return the original image.
33+
The original image will contain only black and white pixels.
34+
All pixels on the top and bottom rows and left and right
35+
columns of the original image will be black.
36+
All values of the blurred image will therefore be: 0 (black), 1/9, 2/9,
37+
3/9, 4/9, 5/9, 6/9, 7/9, 8/9, or 9/9 (white).
38+
</p>
39+
<p>
40+
The blurred image will be given as a vector &lt;string&gt;.
41+
Each character in the blurred image will be a character between
42+
'0' and '9', inclusive, giving the value of that pixel multiplied
43+
by nine.
44+
For example, the blurred image above would be given as:
45+
</p>
46+
47+
<pre>
48+
49+
{ &quot;1233321000000000123332100000000000000000000&quot;,
50+
&quot;1244422233222332334343323322232112332223321&quot;,
51+
&quot;1255523344343443545343434434343233432334432&quot;,
52+
&quot;0033303455465775633011445546454355753457753&quot;,
53+
&quot;0033303333364543533011433336333364521346542&quot;,
54+
&quot;0033303455464532445343545546454355753446542&quot;,
55+
&quot;0022202344342200234343434434343233432323221&quot;,
56+
&quot;0011101233221100123332223322232112332211111&quot; }
57+
</pre>
58+
59+
<p>
60+
Return the original image as a vector &lt;string&gt;.
61+
For each pixel in the original image, return a '.'
62+
if it is black and '#' if it is white.
63+
For example, the original image for the example
64+
above would be returned as:
65+
</p>
66+
67+
<pre>
68+
{ &quot;...........................................&quot;,
69+
&quot;.#####...........#####.....................&quot;,
70+
&quot;...#...####.####.#...#.####.###..####.####.&quot;,
71+
&quot;...#...#..#.#..#.#.....#..#.#..#.#....#..#.&quot;,
72+
&quot;...#...#..#.####.#.....#..#.#..#.###..####.&quot;,
73+
&quot;...#...#..#.#....#...#.#..#.#..#.#....#.#..&quot;,
74+
&quot;...#...####.#....#####.####.###..####.#..#.&quot;,
75+
&quot;...........................................&quot; }
76+
</pre>
77+
</td></tr><tr><td colspan="2"><h3>Definition</h3></td></tr><tr><td>&#160;&#160;&#160;&#160;</td><td><table><tr><td>Class:</td><td>Unblur</td></tr><tr><td>Method:</td><td>original</td></tr><tr><td>Parameters:</td><td>vector &lt;string&gt;</td></tr><tr><td>Returns:</td><td>vector &lt;string&gt;</td></tr><tr><td>Method signature:</td><td>vector &lt;string&gt; original(vector &lt;string&gt; blurred)</td></tr><tr><td colspan="2">(be sure your method is public)</td></tr></table></td></tr><tr><td colspan="2"><h3>Limits</h3></td></tr><tr><td>&#160;&#160;&#160;&#160;</td><td><table><tr><td>Time limit (s):</td><td>2.000</td></tr><tr><td>Memory limit (MB):</td><td>64</td></tr></table></td></tr><tr><td colspan="2"><h3>Constraints</h3></td></tr><tr><td align="center" valign="top">-</td><td><b>blurred</b> will contain between 1 and 50 elements, inclusive.</td></tr><tr><td align="center" valign="top">-</td><td>Each element of <b>blurred</b> will have a length between 1 and 50, inclusive, and contain only characters between '0' and '9', inclusive.</td></tr><tr><td align="center" valign="top">-</td><td>Each element of <b>blurred</b> will have the same length.</td></tr><tr><td align="center" valign="top">-</td><td><b>blurred</b> will be the result of blurring a black and white image.</td></tr><tr><td colspan="2"><h3>Examples</h3></td></tr><tr><td align="center" nowrap="true">0)</td><td></td></tr><tr><td>&#160;&#160;&#160;&#160;</td><td><table><tr><td><table><tr><td><pre>{ &quot;1221&quot;,
78+
&quot;1221&quot;,
79+
&quot;1221&quot; }</pre></td></tr></table></td></tr><tr><td><pre>Returns: { &quot;....&quot;, &quot;.##.&quot;, &quot;....&quot; }</pre></td></tr><tr><td><table><tr><td colspan="2"><p>
80+
All pixels in the center two columns are adjacent to two white pixels in the source image. The remaining pixels are adjacent to only one.
81+
</p>
82+
</td></tr></table></td></tr></table></td></tr><tr><td align="center" nowrap="true">1)</td><td></td></tr><tr><td>&#160;&#160;&#160;&#160;</td><td><table><tr><td><table><tr><td><pre>{ &quot;00000&quot;,
83+
&quot;00000&quot;,
84+
&quot;00000&quot;,
85+
&quot;00000&quot; }</pre></td></tr></table></td></tr><tr><td><pre>Returns: { &quot;.....&quot;, &quot;.....&quot;, &quot;.....&quot;, &quot;.....&quot; }</pre></td></tr><tr><td><table><tr><td colspan="2">A solid black image blurs to all zeros.</td></tr></table></td></tr></table></td></tr><tr><td align="center" nowrap="true">2)</td><td></td></tr><tr><td>&#160;&#160;&#160;&#160;</td><td><table><tr><td><table><tr><td><pre>{ &quot;0011212121100&quot;,
86+
&quot;0123333333210&quot;,
87+
&quot;0123333333210&quot;,
88+
&quot;1233333333321&quot;,
89+
&quot;1233333333321&quot;,
90+
&quot;1233333333321&quot;,
91+
&quot;0112121212110&quot; } </pre></td></tr></table></td></tr><tr><td><pre>Returns:
92+
{ &quot;.............&quot;,
93+
&quot;...#.#.#.#...&quot;,
94+
&quot;..#.#.#.#.#..&quot;,
95+
&quot;.............&quot;,
96+
&quot;.#.#.#.#.#.#.&quot;,
97+
&quot;..#.#.#.#.#..&quot;,
98+
&quot;.............&quot; }</pre></td></tr><tr><td><table><tr><td colspan="2"><p>
99+
original:
100+
</p>
101+
<img src="http://graphics.lcs.mit.edu/~legakis/TC/unblur_fig3a.gif"></img>
102+
<p>
103+
blurred:
104+
</p>
105+
<img src="http://graphics.lcs.mit.edu/~legakis/TC/unblur_fig3b.gif"></img>
106+
</td></tr></table></td></tr></table></td></tr><tr><td align="center" nowrap="true">3)</td><td></td></tr><tr><td>&#160;&#160;&#160;&#160;</td><td><table><tr><td><table><tr><td><pre>{ &quot;1233321000000000123332100000000000000000000&quot;,
107+
&quot;1244422233222332334343323322232112332223321&quot;,
108+
&quot;1255523344343443545343434434343233432334432&quot;,
109+
&quot;0033303455465775633011445546454355753457753&quot;,
110+
&quot;0033303333364543533011433336333364521346542&quot;,
111+
&quot;0033303455464532445343545546454355753446542&quot;,
112+
&quot;0022202344342200234343434434343233432323221&quot;,
113+
&quot;0011101233221100123332223322232112332211111&quot; } </pre></td></tr></table></td></tr><tr><td><pre>Returns:
114+
{ &quot;...........................................&quot;,
115+
&quot;.#####...........#####.....................&quot;,
116+
&quot;...#...####.####.#...#.####.###..####.####.&quot;,
117+
&quot;...#...#..#.#..#.#.....#..#.#..#.#....#..#.&quot;,
118+
&quot;...#...#..#.####.#.....#..#.#..#.###..####.&quot;,
119+
&quot;...#...#..#.#....#...#.#..#.#..#.#....#.#..&quot;,
120+
&quot;...#...####.#....#####.####.###..####.#..#.&quot;,
121+
&quot;...........................................&quot; }</pre></td></tr><tr><td><table><tr><td colspan="2">This is the example from the problem statement.</td></tr></table></td></tr></table></td></tr><tr><td align="center" nowrap="true">4)</td><td></td></tr><tr><td>&#160;&#160;&#160;&#160;</td><td><table><tr><td><table><tr><td><pre>{ &quot;0000123210000&quot;,
122+
&quot;0012456542100&quot;,
123+
&quot;0135789875310&quot;,
124+
&quot;0258988898520&quot;,
125+
&quot;1479865689741&quot;,
126+
&quot;2589742479852&quot;,
127+
&quot;2589742479852&quot;,
128+
&quot;1479865689741&quot;,
129+
&quot;0258988898520&quot;,
130+
&quot;0135789875310&quot;,
131+
&quot;0012456542100&quot;,
132+
&quot;0000123210000&quot; }
133+
</pre></td></tr></table></td></tr><tr><td><pre>Returns:
134+
{ &quot;.............&quot;,
135+
&quot;.....###.....&quot;,
136+
&quot;...#######...&quot;,
137+
&quot;..#########..&quot;,
138+
&quot;..####.####..&quot;,
139+
&quot;.####...####.&quot;,
140+
&quot;.####...####.&quot;,
141+
&quot;..####.####..&quot;,
142+
&quot;..#########..&quot;,
143+
&quot;...#######...&quot;,
144+
&quot;.....###.....&quot;,
145+
&quot;.............&quot; }</pre></td></tr><tr><td><table><tr><td colspan="2"><p>
146+
original:
147+
</p>
148+
<img src="http://graphics.lcs.mit.edu/~legakis/TC/unblur_fig4a.gif"></img>
149+
<p>
150+
blurred:
151+
</p>
152+
<img src="http://graphics.lcs.mit.edu/~legakis/TC/unblur_fig4b.gif"></img></td></tr></table></td></tr></table></td></tr></table><p>This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved. </p></body></html>

0 commit comments

Comments
 (0)