Answers for "python to java convert online"

0

python to java convert online

#include <bits/stdc++.h>
using namespace std;
 
int main()
{
  int m,ans=INT_MAX,I=-1,J=-1;
  cin>>m;
  vector<vector<int>> mat(m,vector<int>());
  cin.ignore();
  for(int i=0;i<m;i++)
  {
    string s;
    getline(cin,s);
    istringstream ss(s);
    while(ss)
    {
      string w;
      ss>>w;
      if(w=="") break;
      mat[i].push_back(stoi(w));
    }
  }
  int n=mat[0].size();
  vector<vector<int>> Ans1(m,vector<int>(n,1));
  vector<vector<int>> Ans2(m,vector<int>(n,1));
  vector<vector<int>> Ans3(m,vector<int>(n,1));
  vector<vector<int>> Ans4(m,vector<int>(n,1));
 
  for(int i=0;i<m-1;i++)
  for(int j=0;j<mat[i].size();j++)
  if(mat[i][j]==mat[i+1][j])
    Ans1[i+1][j]=Ans1[i][j]+1;
  
  for(int i=0;i<m;i++)
  for(int j=0;j<mat[i].size() -1;j++)
  if(mat[i][j]==mat[i][j+1])
    Ans2[i][j+1]=Ans2[i][j]+1;
  
  for(int i=0;i<m-1;i++)
  for(int j=0;j<mat[i].size()-1;j++)
  if(mat[i][j]==mat[i+1][j+1])
    Ans3[i+1][j+1]=Ans3[i][j]+1;
 
  for(int i=0;i<m-1;i++)
  for(int j=1;j<mat[i].size();j++)
  if(mat[i][j]==mat[i+1][j-1])
    Ans4[i+1][j-1]=Ans4[i][j]+1;
  
  for(int i=0;i<m;i++)
  for(int j=0;j<mat[i].size();j++)
  {
    if(Ans1[i][j]>=4 && ans>mat[i][j])
    {ans=mat[i][j];I=i;J=j;}
    if(Ans2[i][j]>=4 && ans>mat[i][j])
    {ans=mat[i][j];I=i;J=j;}
    if(Ans3[i][j]>=4 && ans>mat[i][j])
    {ans=mat[i][j];I=i;J=j;}
    if(Ans3[i][j]>=4 && ans>mat[i][j])
    {ans=mat[i][j];I=i;J=j;}
  }
  if(I==-1)cout<<-1;
  else cout<<ans<<endl;
  
}
Posted by: Guest on April-18-2021

Code answers related to "python to java convert online"

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language