Given a 01 matrix M, find the longest line of consecutive one in the matrix. The line could be horizontal, vertical, diagonal or anti-diagonal.

Example:

Input:
[[0,1,1,0],
[0,1,1,0],
[0,0,0,1]]
Output: 3

Hint: The number of elements in the given matrix will not exceed 10,000.

Median的题就是简单一点。

1. DFS没有问题。

2. 怎么保存已经遍历过的点,因为每一个点可能有4个方向,所以可以开一个map记录每一个点的四个方向有没有被经过。

我的思路,Runtime有点慢。

Runtime: 88 ms, faster than 13.53% of C++ online submissions for Longest Line of Consecutive One in Matrix.

#define ALL(x) (x).begin(), (x).end()
#define FOR(i, a, b) for (remove_cv<remove_reference<decltype(b)>::type>::type i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
#include <vector>
#include <unordered_map>
using namespace std;
class Solution {
private:
unordered_map<int, vector<bool>> mp;
int dirs[][] = {{,},{,},{,},{,-}};
int n,m;
public:
int longestLine(vector<vector<int>>& M) {
n = M.size();
m = M[].size();
int ret;
REP(i,n){
REP(j,m){
mp[i*m+j] = {false,false,false,false};
}
}
REP(i,n){
REP(j,m){
if(M[i][j] == ){
dfs(M, i, j, ret);
}
}
}
return ret;
}
bool valid(int x, int y){
return x < n && x >= && y < m && y >= ;
} void dfs(vector<vector<int>>& M, int x, int y, int& ret){
if(x < || y < || x >= n || y >= m) return ;
if(M[x][y] == ) return ;
REP(i, ){
if(mp[x*m+y][i]) continue;
mp[x*m+y][i] = true;
int tmpx = x, tmpy = y, dx = dirs[i][], dy = dirs[i][];
int cnt = ;
while(valid(tmpx+dx,tmpy+dy) && M[tmpx+dx][tmpy+dy] == ){
tmpx += dx;tmpy += dy;
mp[tmpx*m+tmpy][i] = true;
cnt++;
}
tmpx = x, tmpy = y, dx = -dirs[i][], dy = -dirs[i][];
while(valid(tmpx+dx,tmpy+dy) && M[tmpx+dx][tmpy+dy] == ){
tmpx += dx; tmpy += dy;
mp[tmpx*m+tmpy][i] = true;
cnt++;
}
ret = max(ret, cnt);
}
}
};
//[[0,1,1,0],
//[0,1,1,0],
//[0,0,0,1]] int main(){
vector<vector<int>> M {{,,,},{,,,},{,,,}};
return ;
}

另一个思路使用dp,dp的精髓在于前后的状态无关,有点类似马尔可夫链。

从矩阵第一行向下遍历,每一次保存的就是当前点四个方向的最大长度,如果点是1,该方向加1。也是很好的思路。

class Solution {

public:
int longestLine(vector<vector<int>>& M)
{
int r=M.size();
if(r==) return ;
int c=M[].size();
vector<vector<vector<int> > > dp(r,vector<vector<int>> (c,vector<int>(,) ) );
int res=;
for(int i=;i<r;i++)
for(int j=;j<c;j++)
{
if(M[i][j])
{
dp[i][j][]=j?dp[i][j-][]+:;
dp[i][j][]=i?dp[i-][j][]+:;
dp[i][j][]=i&&j?dp[i-][j-][]+:;
dp[i][j][]=i>&&j<c-?dp[i-][j+][]+:;
for(auto x:dp[i][j])
res=max(res,x);
}
} return res;
}
};

LC 562. Longest Line of Consecutive One in Matrix的更多相关文章

  1. LeetCode 562. Longest Line of Consecutive One in Matrix(在矩阵中最长的连续1)$

    Given a 01 matrix M, find the longest line of consecutive one in the matrix. The line could be horiz ...

  2. [LeetCode] Longest Line of Consecutive One in Matrix 矩阵中最长的连续1

    Given a 01 matrix M, find the longest line of consecutive one in the matrix. The line could be horiz ...

  3. Longest Line of Consecutive One in Matrix

    Given a 01 matrix, find the longest line of consecutive 1 in the matrix. The line could be horizonta ...

  4. LC 516. Longest Palindromic Subsequence

    Given a string s, find the longest palindromic subsequence's length in s. You may assume that the ma ...

  5. LC 3. Longest Substring Without Repeating Characters

    题目描述 Given a string, find the length of the longest substring without repeating characters. Example ...

  6. LC 967. Numbers With Same Consecutive Differences

    Return all non-negative integers of length N such that the absolute difference between every two con ...

  7. LC 687. Longest Univalue Path

    Given a binary tree, find the length of the longest path where each node in the path has the same va ...

  8. [LC] 659. Split Array into Consecutive Subsequences

    Given an array nums sorted in ascending order, return true if and only if you can split it into 1 or ...

  9. [LC] 1048. Longest String Chain

    Given a list of words, each word consists of English lowercase letters. Let's say word1 is a predece ...

随机推荐

  1. linux命令详解——top

    简介 TOP是一个动态显示过程,即可以通过用户按键来不断刷新当前状态.如果在前台执行该命令,它将独占前台,直到用户终止该程序为止.比较准确的说,top命令提供了实时的对系统处理器的状态监视.它将显示系 ...

  2. dhcpd.conf配置文件几例

    例1   ddns-update-style interim; ignore client-updates; subnet 192.168.222.0 netmask 255.255.255.0 { ...

  3. 渗透测试平台Vulnreport介绍与使用

    渗透测试平台Vulnreport介绍与使用   在这篇文章中,我们将跟大家讨论一些关于渗透测试方面的内容,并给大家介绍一款名叫Vulnreport的新型开源工具,而这款工具将能够让任何场景下的渗透测试 ...

  4. 八,kubernetes集群存储卷基础。

    目录 存储卷 存储的分类 emptyDir 测试及使用 hostpath实例 Pod测试挂在共享NFS 写测试清单 测试 pv, pvc 创建几个PV 创建测试的Pod 和 PVC 存储卷 pod运行 ...

  5. ELK展示NGINX访问IP地理位置图

    一.设置NGINX日志格式 [root@zabbix_server ~]# vim /etc/nginx/nginx.conf log_format access_json_log '{"@ ...

  6. Selenium3-浏览器与驱动对照

    在学selenium自动化测试时,遇到的第一个大问题便是浏览器版本.浏览器驱动版本与selenium的版本不对应,而无法驱动浏览器进行自动化操作. 收集了网上的一些技术文档,决定也整理一份相对较全面的 ...

  7. python 时间对应计算

    import re import time def parse_time(date): if re.match('刚刚', date): date = time.strftime('%Y-%m-%d ...

  8. NLP/CL 顶会收录

    全文转载自知乎@刘知远老师:初学者如何查阅自然语言处理学术资料(2016修订版). 1. 国际学术组织.学术会议与学术论文 自然语言处理(natural language processing,NLP ...

  9. 原生 JS实现一个简单分页插件

    最近做的一个 PC端的需求,这个需求中有一个小点,页面底部有一块列表区域,这个列表的数据量比较大,需要进行分页控制,切换页码的时候,发送一个 ajax请求,在页面无刷新的情况下,实现列表数据的刷新,所 ...

  10. WPF界面开发技巧大放送!DevExpress WPF格式化日期时间值

    DevExpress广泛应用于ECM企业内容管理. 成本管控.进程监督.生产调度,在企业/政务信息化管理中占据一席重要之地.通过DevExpress WPF Controls,您能创建有着强大互动功能 ...