constructive algorithms greedy *1300

Please click on ads to support us..

Python Code:

t=int(input())

for x in range(t):
    inp=list(map(int,input().split()))
    n=inp[0]
    m=inp[1]
    wall=[]
    operations=[]

    for y in range(n):
        p=input()
        wall.append(p)
    

    if(wall[0][0]=='1'):
        print(-1)
        continue

    for y in range(n):
        for z in range(m):
            p=[]
            if(wall[y][m-z-1]=='1'):
                if(m-z!=1):
                    operations.append((y+1,m-z-1,y+1,m-z))
    
    for y in range(n):
        if(wall[n-y-1][0]=='1'):
            operations.append((n-y-1,1,n-y,1))
    
    print(len(operations))

    for x in operations:
        print(x[0],x[1],x[2],x[3])

C++ Code:

#include <bits/stdc++.h>
using namespace std;
 
int main() {
    int t;
  cin>>t;
  while(t--){
      long long n , m;
      cin>>n>>m;
      vector<string > v(n);
      for(int i=0;i<n;i++){
          cin>>v[i];
      }
      
      if(v[0][0]=='1'){
          cout<<-1<<endl;
          continue;
      }
      vector<vector<int >> ans;
      for(int i=n-1;i >= 0;i--){
          for(int j=m-1;j >= 0;j--){
              if(v[i][j]=='1'){
                  vector<int> temp;
                  if(j!=0){
                      temp.push_back(i+1);
                      temp.push_back(j);
                      temp.push_back(i+1);
                      temp.push_back(j+1);
                      
                  }
                  else{
                      temp.push_back(i);
                      temp.push_back(j+1);
                      temp.push_back(i+1);
                      temp.push_back(j+1);
                  }
                  ans.push_back(temp);
              }
          }
      }
      long long s = ans.size();
      cout<<s<<endl;
      for(long long i = 0;i<s;i++){
          for(int j =0;j<4;j++){
              cout<<ans[i][j]<<" ";
          }
          cout<<endl;
      }
      
      
      
  }
}


Comments

Submit
0 Comments
More Questions

1038B - Non-Coprime Partition
43A - Football
50A - Domino piling
479A - Expression
1480A - Yet Another String Game
1216C - White Sheet
1648A - Weird Sum
427A - Police Recruits
535A - Tavas and Nafas
581A - Vasya the Hipster
1537B - Bad Boy
1406B - Maximum Product
507B - Amr and Pins
379A - New Year Candles
1154A - Restoring Three Numbers
750A - New Year and Hurry
705A - Hulk
492B - Vanya and Lanterns
1374C - Move Brackets
1476A - K-divisible Sum
1333A - Little Artem
432D - Prefixes and Suffixes
486A - Calculating Function
1373B - 01 Game
1187A - Stickers and Toys
313B - Ilya and Queries
579A - Raising Bacteria
723A - The New Year Meeting Friends
302A - Eugeny and Array
1638B - Odd Swap Sort