City hall

City hall

Time Limit:1000MS Memory Limit:65536K


Description 

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 

#include <iostream>
#include <vector>
using namespace std;
int main(){
 int a,b;
 int res[201]={0};
 bool isCounting = false;
 int intLength = 0;
 int i = 0,j = 0;
 char wall[201][201];
 cin >> a >> b;
 for (i = 1;i <= a ;i++) {
  for (j = 1;j <= b;j++) {
  cin >> wall[i][j];
  }
 }
 for (j = 1;j <= b;j++) {
  for (i = 1;i <= a ;i++) {
  if (wall[i][j] == '0') {
  if(isCounting == false){
  intLength = 1;
  isCounting = true;
  }else{
  intLength++;
  }
  }else{
  if (intLength != 0) {
  res[intLength]++;
  isCounting = false;
  intLength = 0;
  }
  }
  }
  if (intLength != 0) {
  res[intLength]++;
  isCounting = false;
  intLength = 0;
  }
 }

 for (i = 1;i <= 200;i++) {
  if (res[i] != 0) {
  cout << i << " " << res[i] << endl;
  }
 }
 return 0;
}

/*******************************************************/

题没什么难度,纯属练手题,注意一下存储方式,解题会方便很多

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值