简体   繁体   English

如果知道1的位置,则找出被1包围的零的数量

[英]Find number of zeros surrounded by ones if you know positions of ones

I am working on one programming challenge in which I have to find number of zeros surrounded by ones. 我正在应对一个编程挑战,其中我必须找到被1包围的零。

I have given: 我给了:
Number of row and columns r and c 行数和列数rc
Number of positions of ones n 个的位数n
n positions ij where i is index of row and j is index of column n位置ij ,其中i是行的索引, j是列的索引

For example if I have 例如,如果我有

011110
010001
010001
001110
000000

then I return 6 . 然后我返回6
There are 3 test input sets. 有3个测试输入集。 In the first two sets r, c <= 1000 . 在前两组r, c <= 1000 I managed to pass the first two sets by using DFS to cout number of zeros which are not surrounded by ones (starting from borders). 我设法通过使用DFS传递前两个集合来找出不被1包围的零(从边界开始)。 Hence number of zeros z = r * c - k - n where k is number of zeros which are not surrounded by ones. 因此,零的数目z = r * c - k - n ,其中k是不被1包围的零的数目。

But in the third case r, c <= 10^18 which doesn't even fit to memory if I create two dimensional vector on beginning. 但是在第三种情况下r, c <= 10^18如果我在开始时创建二维矢量r, c <= 10^18甚至不适合内存。 I also noticed that n is relative small in all sets ( n <= 10^6 ). 我还注意到,在所有集合中n都相对较小( n <= 10^6 )。

My question is how to solve this problem for all test sets? 我的问题是如何为所有测试集解决此问题?

Counting Rooms While Knowing Where Walls Are here you can find an result of counting the rooms wich are surrounded by walls (in your case "1") you have to change the code, that it counts the number of "0"s. 在知道墙壁哪里的同时对房间进行计数您会发现对墙壁包围的房间(在您的情况下为“ 1”)进行计数的结果,您必须更改代码,即对“ 0”的数量进行计数。 But the idea is the same. 但是想法是一样的。 Making it via bfs or dfs. 通过bfs或dfs进行制作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM