TJU Problem 1090 City hall

注:对于每一横行的数据读取,一定小心不要用int型,而应该是char型或string型。

原题:

1090.   City hall


Time Limit: 1.0 Seconds   Memory Limit: 65536K
Total Runs: 4874   Accepted Runs: 2395



Because of its age, the City Hall has suffered damage to one of
its walls. A matrix with M rows and N columns represents the
encoded image of that wall, where 1 represents an intact wall and 0 represents a
damaged wall (like in Figure-1).

1110000111

1100001111

1000000011

1111101111

1110000111

Figure-1

To repair the wall, the workers will place some blocks vertically into the damaged area. They can use blocks with a fixed width of 1 and different heights of {1, 2, ..., M}.

For a given image of the City Hall‘s wall, your task is to determine how many blocks of different heights are needed to fill in the damaged area of the wall, and to use the least amount of blocks.

Input

There is only one test case. The case starts with a line containing two integers M and N (1 ≤ M, N ≤ 200). Each of the following M lines contains a string with length of N, which consists of "1" s and/or "0" s. These M lines represent the wall.

Output

You should output how many blocks of different heights are needed. Use separate lines of the following format:

k Ck

where k∈{1,2, ..., M} means the height of the block, and Ck means the amount of blocks of height k that are needed. You should not output the lines where Ck = 0. The order of lines is in the ascending order of k.

Sample Input

5 10
1110000111
1100001111
1000000011
1111101111
1110000111

Sample Output

1 7
2 1
3 2
5 1

Source: Asia - Beijing
2004 Practice

 

源代码:

 1 #include <iostream>
 2 using namespace std;
 3
 4 char board[205][205];
 5 int x, y, m, sum = 0;
 6 int book[205];
 7
 8 int find(int x, int y)    {
 9     if (board[x][y] == ‘1‘)    {
10         return sum;
11     }
12     else if (board[x][y] == ‘0‘)    {
13         sum++;
14         board[x][y] = ‘1‘;
15         find (x+1, y);
16     }
17 }
18
19 int main()    {
20     int n;    cin >> m >> n;
21     for (int i = 0; i < m + 1; i++)    book[i] = 0;
22     for (int i = 0; i < 205; i++)
23         for (int j = 0; j < 205; j++)
24             board[i][j] = ‘1‘;
25     for (int i = 0; i < m; i++)
26         for (int j = 0; j < n; j++)
27             cin >> board[i][j];
28
29     for (int i = 0; i < m; i++)    {
30         for (int j = 0; j < n; j++)    {
31             if (board[i][j] != ‘1‘)    {
32                 int res = find(i, j);
33                 sum = 0;
34                 //cout << res << endl;
35                 book[res]++;
36             }
37         }
38     }
39
40
41     for (int i = 1; i <= m; i++) {
42         if (book[i] != 0)    {
43             int a = book[i];
44             cout << i << " " << a << endl;
45         }
46     }
47
48     return 0;
49 }
时间: 2024-08-07 04:32:53

TJU Problem 1090 City hall的相关文章

天大 ACM 1090. City hall

此题的关键就在你是如何选择来计算需要加进去的砖块,是从小的height开始还是从大的height开始.本题是新建一个数组用来存储从最大的(最大的height)砖头开始的砖头数.代码中“for(int k=m;k>=1;k--)” 然后通过下面中的  str[iii][j]='1'; 将其补上:进而再计算次最大的height的砖头数.以此类推,得出结论. #include<iostream> #include<cstring> using namespace std; cons

TJU Problem 1065 Factorial

注意数据范围,十位数以上就可以考虑long long 了,断点调试也十分重要. 原题: 1065.   Factorial Time Limit: 1.0 Seconds   Memory Limit: 65536KTotal Runs: 6067   Accepted Runs: 2679 The most important part of a GSM network is so called Base Transceiver Station (BTS). These transceiver

TJU Problem 2520 Quicksum

注意: for (int i = 1; i <= aaa.length(); i++) 其中是“ i <= ",注意等号. 原题: 2520.   Quicksum Time Limit: 0.5 Seconds   Memory Limit: 65536KTotal Runs: 2964   Accepted Runs: 1970 A checksum is an algorithm that scans a packet of data and returns a single

TJU Problem 1015 Gridland

最重要的是找规律. 下面是引用 http://blog.sina.com.cn/s/blog_4dc813b20100snyv.html 的讲解: 1 做这题时,千万不要被那个图给吓着了,其实这题就是道简单的数学题. 2 3 首先看当m或n中有一个为2的情况,显然,只需要算周长就OK了.即(m+n-2)*2,考虑到至少其中一个为2,所以答案为2 *m或2*n,亦即m*n.注意这里保证了其中一个数位偶数. 4 当m,n≥3时,考虑至少其中一个为偶数的情况,显然,这种情况很简单,可以得出,结果为m*

TJU Problem 2857 Digit Sorting

原题: 2857.   Digit Sorting Time Limit: 1.0 Seconds   Memory Limit: 65536KTotal Runs: 3234   Accepted Runs: 1704 Several players play a game. Each player chooses a certain number, writes it down (in decimal notation, without leading zeroes) and sorts t

TJU Problem 2101 Bullseye

注意代码中: result1 << " to " << result2 << ", PLAYER 1 WINS."<< endl; 和 result1 << " to " << result2 << ", PLAYER 1 WINS. "<< endl; 虽然只在WINS后只差一个空格,但会导致PE. 原题: 2101.   Bul

TJU Problem 1100 Pi

注: 1. 对于double计算,一定要小心,必要时把与double计算相关的所有都变成double型. 2. for (int i = 0; i < N; i++)         //N 不可写为N - 1,否则当N为1时无法进行: 原题: 1100.   Pi Time Limit: 1.0 Seconds   Memory Limit: 65536KTotal Runs: 5683   Accepted Runs: 2317 Professor Robert A. J. Matthews

TJU Problem 2548 Celebrity jeopardy

下次不要被长题目吓到,其实不一定难. 先看输入输出,再揣测题意. 原文: 2548.   Celebrity jeopardy Time Limit: 1.0 Seconds   Memory Limit: 65536KTotal Runs: 1306   Accepted Runs: 898 It's hard to construct a problem that's so easy that everyone will get it, yet still difficult enough

【水】TJU Problem 1805 Electrical Outlets

没看懂题,直接看了INPUT,原来真的可以猜出来. 原题: 1805.   Electrical Outlets Time Limit: 1.0 Seconds   Memory Limit: 65536KTotal Runs: 3321   Accepted Runs: 2575 Roy has just moved into a new apartment. Well, actually the apartment itself is not very new, even dating ba