Place a king and some obstacles in a chessboard such that the king can reach exactly K distinct cells.
EXPLANATION:
SOLUTIONS:
Setter's Solution
import sys
t=input()
for tt in range(t):
k=input()
ans=[['.' for i in range(8)] for j in range(8)]
ans[7][7]='O'
c=64-k
for i in range(8):
for j in range(8):
if c>0:
ans[i][j]='X'
c-=1
for i in range(8):
print "".join(ans[i])
Tester's Solution
#include <iostream>
#include <stdio.h>
using namespace std;
int main()
{
int i,j;
int t,test;
scanf("%d",&t);
for (test=1;test<=t;test++)
{
int k;
int blocked;
scanf("%d", &k);
blocked = 64 - k;
for (i=1;i<=8;i++)
{
for (j=1;j<=8;j++)
{
if (i == 8 && j == 8)
{
printf("O");
continue;
}
if (blocked > 0)
{
printf("X");
blocked--;
}
else
{
printf(".");
}
}
printf("\n");
}
}
return 0;
}
t=int(input())
for t_cases in range(t):
#n=t_cases
n = int(input())
count = 0
end = n+9
for i in range(0,8):
for j in range(0,8):
if(count in range(n,end)):
print(‘X’,end=’ ‘)
if(j==0 and i==0):
print(‘O’,end=’ ‘)
elif(count=n+9):
print(’.’,end=’ ')
count+=1
print() #print()
Can someone plz explain why for index i&j=0, we put ‘O’ there??
class Codechef
{
public static void main (String[] args) throws java.lang.Exception
{
// your code goes here
Scanner in = new Scanner(System.in);
while(in.hasNext()){
int t = in.nextInt();
while(t-- > 0){
int k = in.nextInt();
ITS YOUR CHOICE MAN!! PLACING AT CORNER JUST GIVES U WAY TO PUT ALL OTHER OBSTACLE IN DEFINED AND CONTROLLED WAY! YOU CAN TRY DIFFERENT CORNER OR EVEN CELL!
Don’t know why but my code produces the same result. I used the king trapping technique where i fixed the king in left corner cell and placed X’s as a wall…
Can someone explain me why???
Solution: https://www.codechef.com/viewsolution/35638964
Output for k=1 is wrong itself. King can move diagonally as well. In one move, a king can move to any adjacent cell which shares a side or corner with its current cell and does not contain an obstacle.
Commenting out a bunch of lines fixes that thing-
#include<bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
using namespace std;
#define setbits(x) __builtin_popcountll(x)
#define mod 1000000007
#define FIO ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define ll long long
#define inf 1e8
#define pre(y,x) fixed<<setprecision(y)<<x
#define debug(x) cerr << "[" << #x << ": " << (x) << "] ";
inline ll gcd(ll a, ll b) {ll r; while (b) {r = a % b; a = b; b = r;} return a;}
inline ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}
int main()
{
FIO;
ll t; cin>>t;
while(t--)
{
int k; cin>>k; --k;
char a[65][65];
int n = 8;
int flag=1;
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
if(i==1&&j==1) a[i][j] = 'O';
else{
// if(a[i-1][j]=='X'&&i>1) flag=0;
// if(i==1)
// {
if(k>0)
a[i][j]='.';
else
a[i][j]='X';
// }
// else if(k>0&&i>1&&(a[i-1][j]=='O'||a[i-1][j]=='.')) a[i][j]='.';
// else if(k<0&&(a[i-1][j]=='O'||a[i-1][j]=='.')&&flag) a[i][j]='X';
// else
// a[i][j]='.';
// if(i==1&&k<0){
// a[i][j]='.';
// }
--k;
}
cout<<a[i][j];
}
cout<<'\n';
}
// cout<<endl;
}
return 0;
}
What if we are given condition to minimize the obstacles. Firstly we would go with the ‘.’ in the chessboard and then exchange them with the 'X’s as per the requirement.
But I am not able to apply it properly.
Can someone help?
Mark all cells as “Unassigned”. Then set the required cells where the king can move as “Assigned”. Now, for each of the “Assigned” cells, set all of its “Unassigned” adjacent cells as ‘X’.
Example for K=10:
Edit: This doesn’t work always. For example at K=55, this approach gives 9 as answer, but it can be done in 8.
Hey everyone, this is my solution in java where you dont have to use 2D array or 2 loops:
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class Codechef
{
public static void main (String[] args) {
// your code goes here
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
int size = 64;
while(t--!=0) {
int a = sc.nextInt();
System.out.print("O");
for(int i=2;i<=a;i++) {
if(i%8==0){
System.out.println(".");
}
else{
System.out.print(".");
}
}
for(int i=a+1;i<=size;i++) {
if(i%8==0){
System.out.println("X");
}
else{
System.out.print("X");
}
}
}
}
}