Maze Love CodeChef Solution

Problem -Maze Love CodeChef Solution

This website is dedicated for CodeChef solution where we will publish right solution of all your favourite CodeChef problems along with detailed explanatory of different competitive programming concepts and languages.

Maze Love CodeChef Solution in C++14

#include <bits/stdc++.h>
using namespace std;

const int inf = 1e9;

int gcd(int a, int b) {
  while(b) {
    int r = a % b;
    a = b;
    b = r;
  }
  return a;
}

int main() {
  ios_base::sync_with_stdio(false);
  int t;
  cin >> t;
  assert(1 <= t and t <= 100000);
  while(t--) {
    int n, m, a, b, c, d, P;
    cin >> n >> m;
    // North, South, East, West
    cin >> a >> b >> c >> d;
    cin >> P;
    assert(1 <= n and n <= 1000);
    assert(1 <= m and m <= 1000);
    assert(1 <= a and a <= 1000);
    assert(1 <= b and b <= 1000);
    assert(1 <= c and c <= 1000);
    assert(1 <= d and d <= 1000);
    assert(1 <= P and P <= 1000000);
    // N - S = m, E - W = n
    // Na + Sb + Ec + Wd = P
    int A = a + b;
    int B = c + d;
    int C = P + m * b + n * d;
    // N * A + E * B = C
    // Constraints:
    // N >= m, E >= n
    int g = gcd(A, B);
    if (C % g != 0) {
      cout << "-1" << "\n";
      continue;
    }
    int ans = inf;
    int till = C / A;
    for(int N = m; N <= till; ++N) {
      int E = (C - N * A);
      if (E % B != 0) {
        continue;
      }
      E /= B;
      if (E < n) {
        break;
      }
      int S = N - m, W = E - n;
      ans = min(ans, N + S + E + W);
    }
    if (ans == inf) {
      ans = -1;
    }
    else {
      assert(ans >= n + m);
    }
    cout << ans << "\n";
  }
  return 0;
}

Maze Love CodeChef Solution in PYTH 3

# cook your dish here
# cook your dish here
t=int(input())
for i in range(t):
    x,y,n,s,e,w,p=map(int,input().split())
    wa=x*e+y*n
    c=x+y
    p-=wa
    if p<0:
        print(-1)
    elif p==0:
        print(x+y)
    else:
        li=[n+s,e+w]
        li.sort(reverse=True)
        j=0
        while p%li[0]!=0:
            p-=li[1]
            j+=2
            if p==0:
                print(x+y+j)
            if p<0:
                print(-1)
                break
        if p>0:
            j+=2*p//li[0]
            print(x+y+j)

Maze Love CodeChef Solution in C

#include <stdio.h>
int min(int a,int b){return (a<b)?a:b;}
int main()
{
    int i,T,X,Y,N,S,E,W,P;
    scanf("%d",&T);
    for(i=1;i<=T;i++)
    {
        scanf("%d%d%d%d%d%d%d",&X,&Y,&N,&S,&E,&W,&P);
        int a,b,temp;
        int res=P-X*E-Y*N;
        int c1=N+S,c2=E+W;
        int ans=-1;
        int first=1;
        for(a=0,temp=0;temp<=res;a++,temp+=c1)
        {
            int temp2=res-temp;
            if(temp2%c2==0)
            {
                b=temp2/c2;
                if(first){first=0;ans=a+b;}
                else ans=min(ans,a+b);
            }
        }
        if(ans==-1) printf("%d\n",ans);
        else printf("%d\n",2*ans+X+Y);
    }
    return 0;
} 

Maze Love CodeChef Solution in JAVA

import java.io.*;
import java.util.*;

public class Main {

	public static class InputReader {
		public BufferedReader reader;
		public StringTokenizer tokenizer;

		public InputReader() {
			reader = new BufferedReader(new InputStreamReader(System.in), 32765);
			tokenizer = null;
		}

		public InputReader(InputStream stream) {
			reader = new BufferedReader(new InputStreamReader(System.in), 32765);
			tokenizer = null;
		}

		public String next() {
			while (tokenizer == null || !tokenizer.hasMoreTokens()) {
				try {
					tokenizer = new StringTokenizer(reader.readLine());
				} catch (IOException e) {
					throw new RuntimeException(e);
				}
			}
			return tokenizer.nextToken();
		}

		public char nextChar() {
			return next().charAt(0);
		}

		public int nextInt() {
			return Integer.parseInt(next());
		}

		public long nextLong() {
			return Long.parseLong(next());
		}

		public double nextDouble() {
			return Double.parseDouble(next());
		}

		public int[] nextIntArr(int n) {
			int[] arr = new int[n];
			for (int i = 0; i < n; i++) {
				arr[i] = this.nextInt();
			}
			return arr;
		}

		public Integer[] nextIntegerArr(int n) {
			Integer[] arr = new Integer[n];
			for (int i = 0; i < n; i++) {
				arr[i] = new Integer(this.nextInt());
			}
			return arr;
		}

		public int[][] next2DIntArr(int n, int m) {
			int[][] arr = new int[n][m];
			for (int i = 0; i < n; i++) {
				for (int j = 0; j < m; j++) {
					arr[i][j] = this.nextInt();
				}
			}
			return arr;
		}

		public int[] nextSortedIntArr(int n) {
			int[] arr = new int[n];
			for (int i = 0; i < n; i++) {
				arr[i] = this.nextInt();
			}
			Arrays.sort(arr);
			return arr;
		}

		public long[] nextLongArr(int n) {
			long[] arr = new long[n];
			for (int i = 0; i < n; i++) {
				arr[i] = this.nextLong();
			}
			return arr;
		}

		public char[] nextCharArr(int n) {
			char[] arr = new char[n];
			for (int i = 0; i < n; i++) {
				arr[i] = this.nextChar();
			}
			return arr;
		}
	}

	public static InputReader scn = new InputReader();
	public static PrintWriter out = new PrintWriter(System.out);

	public static void main(String[] args) {
		// InputStream inputStream = System.in; // Useful when taking input other than
		// console eg file handling // check ctor of inputReader
		// To print in file use this:- out = new PrintWriter("destination of file
		// including extension");
		//System.out.println("GO");

		int t = scn.nextInt();
		while (t-- > 0) {
			int p = scn.nextInt(), q = scn.nextInt();
			int a = scn.nextInt(), b = scn.nextInt(), c = scn.nextInt(), d = scn.nextInt();
			int E = scn.nextInt();

			int A = a + b, B = c + d, C = E + b * q + d * p;

			int g = gcd(A, B);
			if (C % g != 0) {
				out.println(-1);
				continue;
			}

			int ans = Integer.MAX_VALUE;

			for (int i = 0; C - A * i >= 0; i++) {
				if ((C - A * i) % B == 0) {
					if (i >= q && (C - A * i) / B >= p) {
						ans = Math.min(ans, i + (C - A * i) / B);
					}
				}
			}

			if (ans == Integer.MAX_VALUE) {
				out.println(-1);
			} else {
				ans = 2 * ans - (p + q);
				out.println(ans);
			}
		}
		out.close();
	}

	public static int gcd(int a, int b) {
		return b == 0 ? a : gcd(b, a % b);
	}
}

Maze Love CodeChef Solution in PYTH

t=input()
for q in range(t):
    x,y,south,north,east,west,p=map(int,raw_input().split())
    p-=x*east
    p-=y*south
    right=east+west
    top=north+south
    i=0
    found=False
    ans=[]
    while(i*top<=p):
        #print i
        if (p-i*top)%right==0:
            j=(p-i*top)/right
            ans.append(2*(i+j))
            found=True
        i+=1
    if found==False:
        print -1
    else:
        print    min(ans)+x+y

Maze Love CodeChef Solution in GO

package main

import (
	"bufio"
	"bytes"
	"fmt"
	"os"
)

func main() {
	// hint(105)
	reader := bufio.NewReader(os.Stdin)

	tc := readNum(reader)
	var buf bytes.Buffer
	for tc > 0 {
		tc--

		line := readNNums(reader, 7)

		D := line[:2]
		C := line[2:6]
		P := line[6]

		res := solve(P, C, D)

		buf.WriteString(fmt.Sprintf("%d\n", res))
	}
	fmt.Print(buf.String())
}

func readUint64(bytes []byte, from int, val *uint64) int {
	i := from

	var tmp uint64
	for i < len(bytes) && bytes[i] >= '0' && bytes[i] <= '9' {
		tmp = tmp*10 + uint64(bytes[i]-'0')
		i++
	}
	*val = tmp

	return i
}

func readInt(bytes []byte, from int, val *int) int {
	i := from
	sign := 1
	if bytes[i] == '-' {
		sign = -1
		i++
	}
	tmp := 0
	for i < len(bytes) && bytes[i] >= '0' && bytes[i] <= '9' {
		tmp = tmp*10 + int(bytes[i]-'0')
		i++
	}
	*val = tmp * sign
	return i
}

func readString(reader *bufio.Reader) string {
	s, _ := reader.ReadString('\n')
	for i := 0; i < len(s); i++ {
		if s[i] == '\n' {
			return s[:i]
		}
	}
	return s
}

func readNum(reader *bufio.Reader) (a int) {
	bs, _ := reader.ReadBytes('\n')
	readInt(bs, 0, &a)
	return
}

func readTwoNums(reader *bufio.Reader) (a int, b int) {
	res := readNNums(reader, 2)
	a, b = res[0], res[1]
	return
}

func readThreeNums(reader *bufio.Reader) (a int, b int, c int) {
	res := readNNums(reader, 3)
	a, b, c = res[0], res[1], res[2]
	return
}

func readNNums(reader *bufio.Reader, n int) []int {
	res := make([]int, n)
	x := 0
	bs, _ := reader.ReadBytes('\n')
	for i := 0; i < n; i++ {
		for x < len(bs) && (bs[x] < '0' || bs[x] > '9') && bs[x] != '-' {
			x++
		}
		x = readInt(bs, x, &res[i])
	}
	return res
}

const INF = 1 << 30

func solve(P int, X []int, D []int) int {
	n, m := D[0], D[1]

	a, b, c, d := X[0], X[1], X[2], X[3]
	A := a + b
	B := c + d
	C := P + m*b + n*d

	g := gcd(A, B)

	if C%g > 0 {
		return -1
	}

	ans := INF

	for N := m; N <= C/A; N++ {
		E := C - N*A
		if E%B != 0 {
			continue
		}
		E /= B
		if E < n {
			break
		}
		S := N - m
		W := E - n
		ans = min(ans, N+S+E+W)
	}

	if ans == INF {
		return -1
	}

	return ans
}

func gcd(a, b int) int {
	for b > 0 {
		a, b = b, a%b
	}
	return a
}

func min(a, b int) int {
	if a <= b {
		return a
	}
	return b
}
Maze Love CodeChef Solution Review:

In our experience, we suggest you solve this Maze Love CodeChef Solution and gain some new skills from Professionals completely free and we assure you will be worth it.

If you are stuck anywhere between any coding problem, just visit Queslers to get the Maze Love CodeChef Solution.

Find on CodeChef

Conclusion:

I hope this Maze Love CodeChef Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Coding Solutions.

This Problem is intended for audiences of all experiences who are interested in learning about Programming Language in a business context; there are no prerequisites.

Keep Learning!

More Coding Solutions >>

Cognitive Class Answer

CodeChef Solution

Microsoft Learn

Leave a Reply

Your email address will not be published. Required fields are marked *