You are viewing a single comment's thread. Return to all comments →
I worked up a solution in C++ but can someone tell if this is DP or not ??
string hexagonalGrid(string& a, string& b) { int n = a.size(); int a0=0, b0=0; for(int i =0;i<n;i++){ if(a[i]=='0') a0++; if(b[i]=='0') b0++; } if((a0+b0)%2==1) return "NO"; else{ int i=0,j=0; while(i<n && j<n){ while(i<n && a[i]=='1') i++; while(j<n && b[j]=='1') j++; if(i==n || j==n) break; if(i==j){ a[i]='1'; b[j]='1'; i++;j++; if(i==n || j==n) break; } else if(i==j+1){ a[i]='1'; b[j]='1'; i++;j++; if(i==n || j==n) break; } else if(i>j+1){ while(i>j+1){ if(b[j+1]=='1') return "NO"; b[j]='1';b[j+1]='1';j++;j++; while(j<n && b[j]=='1') j++; if(j==n) break; } } else if(j>i){ while(j>i){ if(a[i+1]=='1') return "NO"; a[i]='1';a[i+1]='1';i++;i++; while(i<n && a[i]=='1') i++; if(i==n) break; } } } if(i<n){ while(i<n-1){ while(i<n && a[i]=='1') i++; if(i==n) break; if(i==n-1 || a[i+1]=='1') return "NO"; a[i]='1';a[i+1]='1';i++;i++; } } else if(j<n){ while(j<n-1){ while(j<n && b[j]=='1') j++; if(j==n) break; if(j==n-1 || b[j+1]=='1') return "NO"; b[j]='1';b[j+1]='1';j++;j++; } } } return "YES"; }
Seems like cookies are disabled on this browser, please enable them to open this website
Hexagonal Grid
You are viewing a single comment's thread. Return to all comments →
I worked up a solution in C++ but can someone tell if this is DP or not ??