问题:
给定一个只有1和0的布尔矩阵。找出连续1s的最长序列的长度。只允许移动是南,东南和东。
样本矩阵:输出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
答案 0 :(得分:0)
这是一个动态编程问题。想象一下,你已经解决了问题,直到第i行和第j列。并且对于每一行&lt; 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]。然而,我描述了问题的方式,即向南,东南,东移动。但是N [] []正在以相反的方式解决问题,即它从右下角开始向西,向西,向北移动。但是,它实际上并不重要;他们都会提供相同的解决方案。 (看看为什么这是一个很好的练习)