Hexagonal Grid – HackerRank Solution Java , Python 3, Python 2 , C , C++, Best and Optimal Solutions , All you need.
Solutions of Algorithms Data Structures Hard HackerRank:
Here are all the Solutions of Hard , Advanced , Expert Algorithms of Data Structure of Hacker Rank , Leave a comment for similar posts
C++ Hexagonal Grid HackerRank Solution
#include <cstdio>
#include <cstring>
#include <string>
using namespace std;
char s[2][13];
int n;
bool can(char s[2][13],int x,int y) {
//printf("x = %d y = %d\n%s\n%s\n",x,y,s[0],s[1]);
if (y >= n) {
return can(s, x + 1, 0);
}
if (x > 1) {
return true;
}
if (s[x][y] == '1') {
return can(s, x, y + 1);
}
if ((y + 1 < n) && (s[x][y + 1] == '0')) {
s[x][y] = s[x][y + 1] = '1';
if (can(s, x, y + 1)) {
return true;
}
s[x][y] = s[x][y + 1] = '0';
}
if (x == 0) {
if (s[1][y] == '0') {
s[0][y] = s[1][y] = '1';
if (can(s, x , y + 1)) {
return true;
}
s[0][y] = s[1][y] = '0';
}
if ((y) && (s[1][y - 1] == '0')) {
s[0][y] = s[1][y - 1] = '1';
if (can(s, x, y + 1)) {
return true;
}
s[0][y] = s[1][y - 1] = '0';
}
}
return false;
}
int main() {
int z;
for (scanf("%d",&z);z;--z) {
scanf("%d%s%s",&n,s[0],s[1]);
int sum = 0;
for (int i = 0; i < n; ++i) {
sum += s[0][i] - '1';
sum += s[1][i] - '1';
}
puts((((sum & 1) == 0) && can(s, 0, 0))?"YES":"NO");
}
return 0;
}
Java Hexagonal Grid HackerRank Solution
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.InputMismatchException;
public class Solution {
static InputStream is;
static PrintWriter out;
static String INPUT = "";
static void solve()
{
for(int T = ni();T >= 1;T--){
int n = ni();
char[] a = ns(n);
char[] b = ns(n);
boolean[] ok = new boolean[1<<2*n];
int ini = 0;
for(int i = 0;i < n;i++){
if(a[i] == '1')ini |= 1<<2*i;
if(b[i] == '1')ini |= 1<<2*i+1;
}
if(Integer.bitCount(ini) % 2 == 0){
ok[ini] = true;
for(int i = ini;i < (1<<2*n)-1;i++){
if(ok[i]){
int j = Integer.numberOfTrailingZeros(~i);
if(j+1 < 2*n && i<<31-(j+1)>=0){
ok[i|1<<j|1<<j+1] = true;
}
if(j+2 < 2*n && i<<31-(j+2)>=0){
ok[i|1<<j|1<<j+2] = true;
}
}
}
}
out.println(ok[(1<<2*n)-1] ? "YES" : "NO");
}
}
public static void main(String[] args) throws Exception
{
long S = System.currentTimeMillis();
is = INPUT.isEmpty() ? System.in : new ByteArrayInputStream(INPUT.getBytes());
out = new PrintWriter(System.out);
solve();
out.flush();
long G = System.currentTimeMillis();
tr(G-S+"ms");
}
private static boolean eof()
{
if(lenbuf == -1)return true;
int lptr = ptrbuf;
while(lptr < lenbuf)if(!isSpaceChar(inbuf[lptr++]))return false;
try {
is.mark(1000);
while(true){
int b = is.read();
if(b == -1){
is.reset();
return true;
}else if(!isSpaceChar(b)){
is.reset();
return false;
}
}
} catch (IOException e) {
return true;
}
}
private static byte[] inbuf = new byte[1024];
static int lenbuf = 0, ptrbuf = 0;
private static int readByte()
{
if(lenbuf == -1)throw new InputMismatchException();
if(ptrbuf >= lenbuf){
ptrbuf = 0;
try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); }
if(lenbuf <= 0)return -1;
}
return inbuf[ptrbuf++];
}
private static boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); }
private static int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; }
private static double nd() { return Double.parseDouble(ns()); }
private static char nc() { return (char)skip(); }
private static String ns()
{
int b = skip();
StringBuilder sb = new StringBuilder();
while(!(isSpaceChar(b))){ // when nextLine, (isSpaceChar(b) && b != ' ')
sb.appendCodePoint(b);
b = readByte();
}
return sb.toString();
}
private static char[] ns(int n)
{
char[] buf = new char[n];
int b = skip(), p = 0;
while(p < n && !(isSpaceChar(b))){
buf[p++] = (char)b;
b = readByte();
}
return n == p ? buf : Arrays.copyOf(buf, p);
}
private static char[][] nm(int n, int m)
{
char[][] map = new char[n][];
for(int i = 0;i < n;i++)map[i] = ns(m);
return map;
}
private static int[] na(int n)
{
int[] a = new int[n];
for(int i = 0;i < n;i++)a[i] = ni();
return a;
}
private static int ni()
{
int num = 0, b;
boolean minus = false;
while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));
if(b == '-'){
minus = true;
b = readByte();
}
while(true){
if(b >= '0' && b <= '9'){
num = num * 10 + (b - '0');
}else{
return minus ? -num : num;
}
b = readByte();
}
}
private static long nl()
{
long num = 0;
int b;
boolean minus = false;
while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));
if(b == '-'){
minus = true;
b = readByte();
}
while(true){
if(b >= '0' && b <= '9'){
num = num * 10 + (b - '0');
}else{
return minus ? -num : num;
}
b = readByte();
}
}
private static void tr(Object... o) { if(INPUT.length() != 0)System.out.println(Arrays.deepToString(o)); }
}
Python 3 Hexagonal Grid HackerRank Solution
def nextIndex(index):
return [index[0]+index[1], (index[1]+1)%2]
def onBoard(index, board):
return index[0] < len(board[0])
for t in range(int(input())):
n = int(input())
line1 = input()
line2 = input()
board = [line1,line2]
index = [0, 0]
sol = "YES"
while(index[0]<n):
if board[index[1]][index[0]] == "1":
index = nextIndex(index)
continue
index1 = nextIndex(index)
if not onBoard(index1, board):
#print("line 19 index",index1)
sol = "NO"
break
if board[index1[1]][index1[0]] == "0":
#board[index1[1]][index1[0]] = "1"
#board[index[1]][index[0]] = "1"
index = nextIndex(index1)
continue
index2 = nextIndex(index1)
if not onBoard(index2, board):
#print("line 29 index",index2)
sol = "NO"
break
if board[index2[1]][index2[0]] == "0":
#board[index2[1]][index2[0]] = "1"
#board[index[1]][index[0]] = "1"
index = nextIndex(index2)
continue
sol = "NO"
#print("line 37",index, index1, index2)
break
print(sol)
Python 2 Hexagonal Grid HackerRank Solution
import copy
import sys
t = int(sys.stdin.readline())
def sat(f, rs, n):
if len(rs) == 0:
return True
x, y = rs.pop(0)
for dx, dy in ((0, 1), (1, 0), (1, -1)):
x2 = x + dx
y2 = y + dy
if x2 == n or y2 < 0 or y2 > 1 or f[y2][x2]:
continue
f0 = copy.deepcopy(f)
f0[y][x] = True
f0[y2][x2] = True
rs0 = filter(lambda x: x[0] != x2 or x[1] != y2, rs)
if sat(f0, rs0, n):
return True
return False
for tix in range(t):
n = int(sys.stdin.readline())
l1 = sys.stdin.readline()
l2 = sys.stdin.readline()
f = []
l1 = filter(lambda x: x == '0' or x == '1', l1)
l2 = filter(lambda x: x == '0' or x == '1', l2)
f.append(map(lambda x: True if x == '1' else False, l1))
f.append(map(lambda x: True if x == '1' else False, l2))
coords = [(x, y) for x in range(n) for y in (0, 1)]
rs = filter(lambda x: not f[x[1]][x[0]], coords)
print 'YES' if sat(f, rs, n) else 'NO'
C Hexagonal Grid HackerRank Solution
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
/*void func(int a[2][10],int n,int i,int j)
{
if(i<n && j <2)
{
if(a[i][j]==0)
{
*/
int main() {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
//long long int n1,n2,var;
int t,i,j,n,flag,temp;
char n1[2][20];
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
j=n;
scanf("%s %s",n1[0],n1[1]);
int a[2][10];
for(i=0;i<2;i++)
{
for(j=0;j<n;j++)
{
if(n1[i][j]=='1')
a[i][j]=1;
else
a[i][j]=0;
}
}
for(i=0;i<n-1;i++)
{
if(a[1][i]==0)
{
if(a[0][i]==0)
{
/*if(a[0][i-1]==0 && i>0)
{
a[0][i-1]=1;
a[0][i]=1;
i--;
}
else
{*/
a[0][i]=1;
a[1][i]=1;
//}
}
else if(a[0][i+1]==0)
{
a[0][i+1]=1;
a[1][i]=1;
}
else if(a[1][i+1]==0)
{
a[1][i+1]=1;
a[1][i]=1;
}
}
else
{
if(a[0][i]==0)
{
if(a[0][i+1]==0)
{
a[0][i]=1;
a[0][i+1]=1;
}
}
}
}
i=n-1;
if(a[1][i]==0)
{
if(a[0][i]==0)
{
a[0][i]=1;
a[1][i]=1;
}
}
for(i=0;i<n-1;i++)
{
if(a[0][i]==0 && a[0][i+1]==0)
{
a[0][i]=1;
a[0][i+1]=1;
}
}
/*for(i=0;i<2;i++)
{
for(j=0;j<n;j++)
{
printf("%d ",a[i][j]);
}
printf("\n");
}*/
flag=0;
for(i=0;i<2;i++)
{
for(j=0;j<n;j++)
{
if(a[i][j]==0)
{
flag=1;
printf("NO\n");
break;
}
}
if(flag==1)
break;
}
if(flag==0)
printf("YES\n");
}
return 0;
}
Warmup
Implementation
Strings
Sorting
Search
Graph Theory
Greedy
Dynamic Programming
Constructive Algorithms
Bit Manipulation
Recursion
Game Theory
NP Complete
Debugging
Leave a comment below