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 }
原文地址:https://www.cnblogs.com/QingHuan/p/4289680.html