bfs深度优先搜索好题

中文题意:找出火箭炮一次可以摧毁敌人的最大数目,火箭炮的移动有八个方向可以移动,以及找出一个二维矩阵中敌人的部队的数目
Oliver and the battle
Attempted by:  1369
/
Accuracy:  67%
/
Maximum Score:  20
/
 
15 Votes
Tag(s):
 

Algorithms, BFS, DFS, Easy

PROBLEM
EDITORIAL
MY SUBMISSIONS
ANALYTICS

Colonel Oliver and his battalion were in midst of a battle against an extraterrestrial species Zomni. The Colonel has to win the battle at any cost to save all of humanity. He was short of soldiers, however, he had a very powerful weapon bazooka which could be used only once during the war.

The Zomni army was divided into small troops scattered at different locations (which can be contained in a N x M battle ground). The bazooka, once launched on some troop is capable of killing all the Zomnis of that troop at a time but cannot harm any Zomni soldier in other troops.

The war is coming to an end and the Colonel is about to lose. He has only one bazooka but something he knows for sure is that if he is able to kill the maximum possible soldiers with this bazooka, he and his soldiers definitely have a chance of winning the battle.

So, the Colonel seeks your help in finding out the maximum number of Zomnis he can kill in one strike of the bazooka and also the total number of Zomni troops gathered against them so that he can divide his battalion efficiently (the troop killed by the bazooka should also be included).

Two Zomni soldiers belong to the same troop if they are at adjacent positions in the battle ground. Therefore, any soldier who is not at some boundary of the battle ground can have a maximum of 8 adjacent soldiers.

INPUT:
First line contains single integer T, the number of test cases. First line of each test case contains two space separated integers N and M (size of the battle ground), followed by N lines containing M integers 0 or 1 where 0 means an empty space and 1 means a Zomni soldier.

OUTPUT:
For every test case, output two space separated integers X and Y where X is the number of Zomni troops and Y is the maximum number of Zomnis that can be killed by a single strike of the bazooka.

CONTSTRAINTS:
1 ≤ T ≤ 10
1 ≤ N,M ≤ 1000

Problem statement in native language: http://hck.re/om3VGD

SAMPLE INPUT
 
2
4 6
0 0 0 1 1 0 
1 1 0 0 0 0 
0 1 0 0 0 0 
0 0 1 0 0 0 
6 4
1 1 1 1 
0 0 0 0 
0 1 0 0 
1 0 1 0 
1 0 0 0 
1 0 0 0 
SAMPLE OUTPUT
 
2 4
2 5
Explanation

For the first test case

there are two enemy troops first having positions 1,4 and 1,5 second having positions 2, 1 2, 2 3, 2 4, 3 Therefore the bomb should be exploded on the second troop killing all the 4 enemy soldiers.

#include<iostream>
using namespace std;
int a[1000][1000];
bool visited[1000][1000];
int n, m, cnt;
void bfs(int x, int y)
{
	if ((x < 0 || x >= n) || (y < 0 || y >= m))
	{
		return;
	}
	if (visited[x][y])return;
	if (a[x][y] == 0)return;
	visited[x][y] = true;
	cnt++;
	bfs(x, y + 1);//1
	bfs(x, y - 1);//2
	bfs(x + 1, y + 1);//3
	bfs(x - 1, y + 1);//4
	bfs(x + 1, y - 1);//5
	bfs(x - 1, y - 1);//6
	bfs(x + 1, y);//7
	bfs(x - 1, y);//8
}

int main()
{ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    
	int t;
	cin >> t;
	while (t--)
	{
		cin >> n >> m;
		for (int i = 0; i < n; ++i)
		{
			for (int j = 0; j < m; ++j)
			{
				visited[i][j] = false;
			}
		}
		for (int i = 0; i < n; ++i)
		{
			for (int j = 0; j < m; ++j)
			{
				cin >> a[i][j];
			}
		}
		int max = 0;
		int ans = 0;
		for (int i = 0; i < n; ++i)
			for (int j = 0; j < m; ++j)
			{
				cnt = 0;
				bfs(i, j);
				if (cnt != 0)
				{
					ans++;
					if (max < cnt)max = cnt;
				}
			}

		cout << ans << " " << max << endl;
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值