week14作業B Q老師與十字叉

在這裏插入圖片描述在這裏插入圖片描述Input

9
5 5
..*..
..*..
*****
..*..
..*..
3 4
****
.*..
.*..
4 3
***
*..
*..
*..
5 5
*****
*.*.*
*****
..*.*
..***
1 4
****
5 5
.....
..*..
.***.
..*..
.....
5 3
...
.*.
.*.
***
.*.
3 3
.*.
*.*
.*.
4 4
*.**
....
*.**
*.**

Output

0
0
0
0
0
4
1
1
2

記錄每一行、每一列空白的格子數目,然後遍歷每一個格子,對於格子(x,y),如果這個格子是黑色的,那麼補全x行、y列,形成十字需要 x行的空白格子數+y列的空白格子數。如果這個格子是空白的,那麼說明填補這個格子的時候,是同時填補行和列的,所以需要 x的空白格子數+y列的空白格子數-1。

#include<iostream>
#include<string.h>
using namespace std;
int q;
int n,m;
int x[51000],y[51000];
char **map;
int main()
{
	cin>>q;
	while(q--)
	{
		memset(x,0,sizeof(x));
		memset(y,0,sizeof(y));
		scanf("%d%d",&n,&m);
		map=new char*[n];
		for(int i=0;i<n;i++)
		{
			map[i]=new char[m];
		}
		for(int i=0;i<n;i++)
		{
			for(int j=0;j<m;j++)
			{
				cin>>map[i][j];
				if(map[i][j]=='.')
				{
					x[i]++;
					y[j]++;
				}
			}
		}
		int ans=1e9;
		for(int i=0;i<n;i++)
		{
			for(int j=0;j<m;j++)
			{
				int temp=x[i]+y[j];
				if(map[i][j]=='.')
					temp--;
				if(ans>temp)
					ans=temp;
			}
		}
		cout<<ans<<endl;
	}
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章