Official

B - Futon Editorial by evima


You can count count the number of all possible positions where the mattress can be placed without duplicates by performing the operations “+1 if the current cell and the cell in the right are both empty” and “+1 if the current cell and the cell below are both empty” for all cells.

Be careful of out-of-bounds array access. You have to be careful not to perform “+1 if the current cell and the cell below are both empty” for the cells in the bottom row, since there are no cells below. However, the cells in the bottom row may still satisfy “+1 if the current cell and the cell in the right are both empty.”

Sample code in C++

#include<stdio.h>
char in[110][110];
int main(){
	int a,b;scanf("%d%d",&a,&b);
	for(int i=0;i<a;i++){
		scanf("%s",in[i]);
	}
	int ret=0;
	for(int i=0;i<a;i++)for(int j=0;j<b;j++){
		if(i+1<a&&in[i][j]==in[i+1][j]&&in[i][j]=='.')ret++;
		if(j+1<b&&in[i][j]==in[i][j+1]&&in[i][j]=='.')ret++;
	}
	printf("%d\n",ret);
}

posted:
last update: