问题:
给定一个只有 1 和 0 的 bool 矩阵。找出连续 1 的最长序列的长度。只允许向南、东南和东移动。
样本矩阵:输出 5
10000
01111
00100
00010
我正在尝试解决此问题,但尚未了解问题以考虑可能的解决方案。在剖析和理解问题方面需要帮助。
更新:
请分享正确性。
for i=1 to n+1
N[i][m+1] = 0;
for j=1 to m+1
N[n+1][j] = 0;
for i=n to 1
for j=m to 1
if M[i][j] == 1
N[i][j] = 1 + max(N[i+1][j] , N[i][j+1]);
else
N[i][j] = 0
search max element in matrix, output it.
}
到目前为止已经尝试过
int main()
{
int A[5][5] = {{0,0,0,1,1},{1,1,1,0,1},{0,1,1,1,0},{0,0,1,0,0},{1,1,1,1,1}};
int temp[5][5];
int end_r(0), end_c(0);
for(int i=0; i<;5; i++){
for(int j=0; j<;5; j++){
temp[i][j] = A[i][j];
int top(0), left(0), max(0);
if(i>;0) top = temp[i-1][j];
if(j>;0) left = temp[i][j-1];
if(top>left) max = top; else max=left;
if(temp[i][j] && max) {temp[i][j] = ++max, end_r=i; end_c=j;}
cout<<temp[i][j]<<" ";
}
cout<<endl;
}
int i = end_r, j = end_c, count=temp[i][j];
--count;
while(count){
if((temp[i-1][j]) == count) --i; else --j;
--count;
}
cout<<"Starting Point"<<" "<<i<<" "<<j<<endl;
cout<<"Ending Point"<<" "<<end_r<<" "<<end_c<<endl;
cout<<"Max Length"<<" "<<temp[end_r][end_c];
return 0;
}
解决方案
/*
============================================================================
Author : James Chen
Email : a.james.chen@gmail.com
Description : Find the longest path of 1s available in the matrix
Created Date : 11-July-2013
Last Modified :
============================================================================
*/
#include <iostream>
#include <iomanip>
#include <cassert>
#include <vector>
using namespace std;
void DisplayPath(int* matrix, int rows, int cols, int maxCount)
{
typedef pair<int, int> Pair;
vector<Pair> path;
int prevRow = rows;
int prevCol = cols;
for(int i = rows - 1; i >= 0; --i){
for(int j = cols - 1; j >=0; --j){
if(matrix[ i * cols + j] == maxCount && i <= prevRow && j <= prevCol){
path.push_back(make_pair(i, j));
maxCount --;
prevRow = i;
prevCol = j;
}
if(maxCount == 0){
cout << "The path is " << endl;
for(int i = path.size() - 1; i >= 0; i--){
cout << path.size() - i << "th -- ";
cout << "[ " << path[i].first << ", " << path[i].second;
cout << "] " << endl;
}
return;
}
}
}
}
int FindLongest1Sequences(int* matrix, int rows, int cols)
{
assert(matrix != NULL);
assert(rows > 0);
assert(cols > 0);
int maxCount(0);
int count(0);
for(int i = 0; i < rows; i ++){
for(int j = 0; j < cols; j++){
int a = (i == 0) ? 0 : matrix[(i - 1) * cols + j];
int b = (j == 0) ? 0 : matrix[i * cols + j - 1];
matrix[i * cols + j] = matrix[i * cols + j] ? max(a, b) + 1 : 0;
maxCount = max(maxCount, matrix[i * cols + j]);
}
}
DisplayPath(matrix, rows, cols, maxCount);
return maxCount;
}
void DoTest(int* matrix, int rows, int cols)
{
if(matrix == NULL){
cout << "The matix is null" << endl;
return;
}
if(rows < 1){
cout << "The rows of matix is less than 1" << endl;
return;
}
if(cols < 1){
cout << "The cols of matix is less than 1" << endl;
return;
}
cout << "The matrix is " << endl;
for(int i = 0; i < rows; ++i){
for(int j = 0; j < cols; ++j){
cout << setw(3) << matrix[i * cols + j];
}
cout << endl;
}
int len = FindLongest1Sequences(matrix, rows, cols);
cout << "The longest length is " << len << endl;
cout << "---------------------------------------" << endl;
}
int main(int argc, char* argv[])
{
int matrix[5][5] = {
{0, 0, 0, 1, 1},
{1, 1, 1, 0, 1},
{0, 1, 1, 1, 0},
{0, 0, 1, 0, 0},
{1, 1, 1, 1, 1}
};
DoTest(&matrix[0][0], 5, 5); // Expected return 8
int matrix1[1][1] = {
0
};
DoTest(&matrix1[0][0], 1, 1); // Expected return 0
int matrix2[1][1] = {
1
};
DoTest(&matrix2[0][0], 1, 1); // Expected return 1
int matrix3[5][5] = {
0
};
DoTest(&matrix3[0][0], 5, 5); // Expected return 0
int matrix4[5][5] = {
{1, 1, 1, 1, 1},
{1, 1, 1, 1, 1},
{1, 1, 1, 1, 1},
{1, 1, 1, 1, 1},
{1, 1, 1, 1, 1}
};
DoTest(&matrix4[0][0], 5, 5); // Expected return 9
int matrix5[5][5] = {
{1, 1, 0, 1, 1},
{0, 1, 1, 0, 1},
{1, 0, 0, 0, 0},
{1, 1, 0, 1, 1},
{1, 1, 1, 1, 1}
};
DoTest(&matrix5[0][0], 5, 5); // Expected return 7
return 0;
}
输出
The matrix is
0 0 0 1 1
1 1 1 0 1
0 1 1 1 0
0 0 1 0 0
1 1 1 1 1
The path is
1th -- [ 1, 0]
2th -- [ 1, 1]
3th -- [ 2, 1]
4th -- [ 2, 2]
5th -- [ 3, 2]
6th -- [ 4, 2]
7th -- [ 4, 3]
8th -- [ 4, 4]
The longest length is 8
---------------------------------------
The matrix is
0
The longest length is 0
---------------------------------------
The matrix is
1
The path is
1th -- [ 0, 0]
The longest length is 1
---------------------------------------
The matrix is
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
The longest length is 0
---------------------------------------
The matrix is
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
The path is
1th -- [ 0, 0]
2th -- [ 1, 0]
3th -- [ 2, 0]
4th -- [ 3, 0]
5th -- [ 4, 0]
6th -- [ 4, 1]
7th -- [ 4, 2]
8th -- [ 4, 3]
9th -- [ 4, 4]
The longest length is 9
---------------------------------------
The matrix is
1 1 0 1 1
0 1 1 0 1
1 0 0 0 0
1 1 0 1 1
1 1 1 1 1
The path is
1th -- [ 2, 0]
2th -- [ 3, 0]
3th -- [ 4, 0]
4th -- [ 4, 1]
5th -- [ 4, 2]
6th -- [ 4, 3]
7th -- [ 4, 4]
The longest length is 7
---------------------------------------
Press any key to continue . . .
引用 : https://en.wikipedia.org/wiki/Longest_increasing_subsequence https://sites.google.com/site/spaceofjameschen/annnocements/findthelongestpathof1savailableinthematrix--goldmansachs
最佳答案
这是一个动态规划问题。想象一下,您已经解决了直到第 i 行和第 j 列的问题。对于 < i 的每一行,以及 i 行中小于 j 的每一列,答案都存储在 DP[][] 中。
现在,您应该将其视为归纳法。 DP[i][j] 的最佳可能答案可以来自这三个地方(如果它们存在 - 意味着它们是有效索引并且 M[i][j] == 1):
DP[i - 1][j] (a south move from there to i, j)
DP[i - 1][j - 1] (a south east move from there)
DP[i][j - 1] (an east move from there)
您输入的代码(第一个代码)试图对此进行模拟,但实际上并没有为此测试用例提供适当的解决方案(因为它未能捕获东南移动):
100
010
001
事实上,你的第一个代码需要是这样的:
N[i][j] = 1 + max(N[i+1][j] , N[i][j+1], N[i + 1][j + 1]);
注意 N[i][j] 就像我描述的 DP[i][j]。但是,我按照问题的方式描述了 DP,即向南、向东南、向东移动。但是 N[][] 正在以相反的方式解决问题,即它从右下角开始向西、西北、北移动。然而,这其实并不重要;他们都将提供相同的解决方案。 (这是一个很好的练习,看看为什么)
关于string - 找出矩阵中最长序列的长度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38290127/