使用分而治之对矩阵中的元素进行计数
Count of an element in a matrix using Divide & Conquer
我开始学习如何实现分而治之算法,但我在这个练习中遇到了一些麻烦。
我写了一个算法,但不幸的是它返回了一个 0 值。我需要使用D&C计算一个数字在矩阵中找到多少次。
我的想法是将矩阵分成 4 个子矩阵,直到所有坐标相等,这意味着有一个元素(可以是我的数字,也可能不是)。
我的缩写:
lr - left row | starting with
lc- left column | left lower corner
rr - right row | starting with
rc- right column | right upper corner
当我编译时,它运行良好但返回 0(因为我认为是因为我的 stop 语句)。所以,我的错误出在solve()函数中。
我的代码:
#include<fstream>
#include<conio.h>
#include<iostream>
using namespace std;
ifstream fin("in.txt");
#define debug cerr<<"OK";
int n,a[100][100];
int solve(int lr,int lc, int rr,int rc,int x,int &contor)
{
int cnt=0;
if(lr < rr || lc > rc)
{
if(cnt<=0)
return 0;
return cnt;
}
if(lr == lc && rr == rc)
if(a[lr][lc] == x)
cnt++;
else;
else
{
int l=(lr+rr)/2;
int c=(lc+rc)/2;
if(l && c)
{
int cnt1=solve(lr-l,lc,rr,rc-c,x,contor); // coordinates for upper left square
int cnt2=solve(lr-l,lc+c,rr,rc,x,contor); // -||- for upper right square
int cnt3=solve(lr,lc,rr+l,rc-c,x,contor); // -||- for low left square
int cnt4=solve(lr,lc+c,rr,rc-c,x,contor); // -||- for low right square
contor=cnt1+cnt2+cnt3+cnt4;
}
}
}
int main()
{
fin>>n;
int i,j;
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
fin>>a[i][j];
int contor=0;
solve(n,1,1,n,3,contor); // i'm searching for 3
cout<<contor;
_getch();
fin.close();
return 0;
}
也许你可以告诉我问题在哪里或直接解决它,它会更快,我可以更容易理解。
Input:
4
1 3 3 4
5 6 7 8
1 2 3 4
4 3 2 1
这是更正的源代码。我添加了一些评论,以便您可以关注它。此外,我切换到正确大小的动态数组。
#include<fstream>
#include<conio.h>
#include<iostream>
using namespace std;
int countOccurences(int** values, int min1, int min2, int max1, int max2, int searchValue)
{
if (min1 > max1 || min2 > max2)
return 0; //invalid area
if (min1 == max1 && min2 == max2)
{
//the current area is a single cell
if (values[min1][min2] == searchValue)
return 1; //we have found 1 occurence
else
return 0; //we have found nothing
}
else
{
//divide the current range
int center1 = (min1 + max1) / 2;
int center2 = (min2 + max2) / 2;
int occurencesInSubMatrices = 0;
//accumulate the occurences in the according variable
occurencesInSubMatrices += countOccurences(values, min1, min2, center1, center2, searchValue); // coordinates for upper left square
occurencesInSubMatrices += countOccurences(values, center1 + 1, min2, max1, center2, searchValue); // -||- for upper right square
occurencesInSubMatrices += countOccurences(values, min1, center2 + 1, center1, max2, searchValue); // -||- for low left square
occurencesInSubMatrices += countOccurences(values, center1 + 1, center2 + 1, max1, max2, searchValue); // -||- for low right square
return occurencesInSubMatrices;
}
}
int main()
{
ifstream fin("in.txt");
int n;
fin >> n;
//create a 2d array of appropriate size
int** values = new int*[n];
for (int i = 0; i < n; i++)
{
//allocate memory for the i-th row
values[i] = new int[n];
for (int j = 0; j < n; j++)
fin >> values[i][j];
}
int count = countOccurences(values, 0, 0, n - 1, n - 1, 3); // i'm searching for 3
cout << count;
_getch();
fin.close();
return 0;
}
请注意,这是一个纯粹的教育示例。在现实世界中,没有人会计算这样的发生。
递归不会更新 cnt...
if(l && c)
{
solve(lr-l,lc,rr,rc-c,x); // coordinates for upper left square
solve(lr-l,lc+c,rr,rc,x); // -||- for upper right square
solve(lr,lc,rr+l,rc-c,x); // -||- for low left square
solve(lr,lc+c,rr,rc-c,x); // -||- for low right square
}
相关文章:
- C++ 分而治之的算法问题
- 从 std::vector 中选择一个元素,而不是给定元素
- 使用分而治之的最大总和子数组,为什么要以不同的方式使用 for 循环,答案是错误的?
- 如何将这种递归解决方案转换为分而治之?
- 最接近的一对点 用向量分而治之
- 通过分而治算法计算数组的最大数量
- 通过分而治之查找子字符串
- 分而治之合并排序不起作用
- 分而治之数组算法++
- 分而治之和分叉和加入的区别
- 用于查找多数元素的分而治之算法
- 斐波那契 - 分而治之算法
- 为什么这个计数器以这种方式增加,而不是在这个分而治之算法中一个接一个地增加
- C++ 递归排序通过分而治之和迭代排序
- 使用分而治之搜索字符串中'xy'的第一个索引
- 使用分而治之的二进制到十进制转换
- 分而治之真的能战胜增加的内存分配吗
- 分而治之的动态规划
- 使用分而治之对矩阵中的元素进行计数
- 分而治之-C/C++库函数和运算符是最理想的函数和运算符吗