Coding Exercise – Timus Online Judge – C++ solution – 1197. Lonesome Knight


1197 Coding Exercise - Timus Online Judge - C++ solution - 1197. Lonesome Knight algorithms beginner brute force c / c++ code implementation programming languages timus

This puzzle is from Timus Online Judge and you can submit your solution here.

The first line of the input is an integer containing the number of total tests, followed by the locations, one each line. The location is represented by two characters, the first is a letter from ‘a’ to ‘h’ and the second is a digit from 1 to 8.

You are required to output the number of positions being attached. You have to convert the first coordinate to integer and check if any of the 8 attacking positions are still on board (in range). Bruteforce implementation is a piece of cake.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>
 
using namespace std;
 
int pos(char x)
{
    if ((x >= '0') && (x <= '9'))   
    {       
        return ((int)(x - '0'));    
    }   
    return ((int)(x - 'a' + 1)); 
} 
 
int test(int x, int y) 
{   
    if ((x > 0) && (x <= 8) && (y > 0) && (y <= 8))     
    {       
        return (1);     
    }   
    return (0); 
} 
 
int loc(char x, char y) 
{   
    int xx = pos(x);    
    int yy = pos(y);    
    return (        
        test(xx - 2, yy + 1) +      
        test(xx + 2, yy + 1) +      
        test(xx - 2, yy - 1) +      
        test(xx + 2, yy - 1) +      
        test(xx - 1, yy + 2) +      
        test(xx + 1, yy + 2) +      
        test(xx - 1, yy - 2) +      
        test(xx + 1, yy - 2)    
    ); 
} 
 
int main() {    
    int n;  cin >> n;
    char x, y;
    for (int i = 0; i < n; i ++)    
    {       
        cin >> x >> y;
        cout << loc(x, y) << endl;
    }
    return (0);
}
#include <iostream>

using namespace std;

int pos(char x)
{
	if ((x >= '0') && (x <= '9')) 	
	{ 		
		return ((int)(x - '0')); 	
	} 	
	return ((int)(x - 'a' + 1)); 
} 

int test(int x, int y) 
{ 	
	if ((x > 0) && (x <= 8) && (y > 0) && (y <= 8)) 	
	{ 		
		return (1); 	
	} 	
	return (0); 
} 

int loc(char x, char y) 
{ 	
	int xx = pos(x); 	
	int yy = pos(y); 	
	return ( 		
		test(xx - 2, yy + 1) + 		
		test(xx + 2, yy + 1) + 		
		test(xx - 2, yy - 1) + 		
		test(xx + 2, yy - 1) + 		
		test(xx - 1, yy + 2) + 		
		test(xx + 1, yy + 2) + 		
		test(xx - 1, yy - 2) + 		
		test(xx + 1, yy - 2) 	
	); 
} 

int main() { 	
	int n; 	cin >> n;
	char x, y;
	for (int i = 0; i < n; i ++) 	
	{ 		
		cin >> x >> y;
		cout << loc(x, y) << endl;
	}
	return (0);
}

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
a WordPress rating system
369 words
Last Post: Coding Exercise - Timus Online Judge - 1880. Psych Up's Eigenvalues - C++ solution
Next Post: C Programming in 6502 - CC65 Compiler Does Not Support Float Type

The Permanent URL is: Coding Exercise – Timus Online Judge – C++ solution – 1197. Lonesome Knight

Leave a Reply