Life2Coding
URI ONLINE JUDGE SOLUTION : 1206 – Challenge of St. Petersburg (INTERMEDIATE PROBLEM)

Problem Summary:

Problem Number: 1206
Problem Name: Challenge of St. Petersburg
Author’s Name: By Marcio T. I. Oshiro  Brazil
Timelimit: 1
Problem Category: AD-HOC
Problem Source: https://www.urionlinejudge.com.br/judge/en/problems/view/1206

Some Talks about Contest Programming:

An incredible method to enhance your abilities when figuring out how to code is by solving coding problems. Solving different kinds of challenges and riddles can enable you to improve as a problem solver, take in the complexities of a programming dialect, get ready for prospective job interviews, learn new algorithms and more.

An online judge is an online platform to test programs in focused programming challenges. They are likewise used to practice for such challenges. A considerable amount of these platforms also arrange their own programming contests.

10 Steps to Solve Any Problems:

  1. Read the problem completely at least two or three times (or however many makes you feel comfortable)
  2. Identify the subject, the problem belongs to. Is it a sorting or pattern matching problem? Can I use graph theory? Is it related to number theory? etc.
  3. Try to solve the problem manually by considering 3 or 4 sample data sets.
  4. After concentrate on optimizing the manual steps. Try to make it as simple as possible.
  5. Write to write pseudo-code and comments besides the code from the manual steps. One thing you can do is to check after every function is written. Use a good IDE with a debugger, if possible. Don’t need to think much about the syntax. Just focus on the logic and steps.
  6. Replace the comments or pseudo-code with real code. Always check if the values and code are behaving as expected before moving to the new line of pseudo-code.
  7. Then optimize the real code.
  8. Take care of boundary conditions as well.
  9. Get feedback from your teammates, professors, and other developers and also ask your question on Stack Overflow if possible. Try to learn from others’ guidelines and what they are handling those problems. A problem may be solved in several ways. So, don’t get disappointed if you can’t think like an expert. You need to stick to the problem and you will gradually become better and quicker in solving problems like others.
  10. Practice, Practice, and Practice.

N.B: Try to follow the above steps always. If you still can’t get the problem solved, take a look at the solution below. Don’t just copy paste the code. It will kill your creativity. Try to enjoy contest programming and develop your skills.

Solution:

#include <stdio.h>

char mesa[8][8];

bool ACIMA(int x, int y){

	if (y == 0)
		return false;
	
	int i;

	for (i = y-1; i >= 0; i--) {							
		switch (mesa[x][i]) {
			case 0:
				break;
			case 'R':
				return true;
			case 'T':
				return true;
			case 'B':
				return false;
			case 'P':
				return false;
			case 'W':
				if (i == y-1)
					return true;
				else
					return false;
		}
	}
	
	return false;
}

bool ABAIXO(int x, int y){

	if (y == 7)
		return false;
	
	int i;

	for (i = y+1; i <= 7; i++) {							
		switch (mesa[x][i]) {
			case 0:
				break;
			case 'R':
				return true;
			case 'T':
				return true;
			case 'B':
				return false;
			case 'P':
				return false;
			case 'W':
				if (i == y+1)
					return true;
				else
					return false;
		}
	}
	
	return false;
}

bool DIREITA(int x, int y){

	if (x == 7)
		return false;
	
	int i;

	for (i = x+1; i <= 7; i++) {							
		switch (mesa[i][y]) {
			case 0:
				break;
			case 'R':
				return true;
			case 'T':
				return true;
			case 'B':
				return false;
			case 'P':
				return false;
			case 'W':
				if (i == x+1)
					return true;
				else
					return false;
		}
	}
	
	return false;
}

bool ESQUERDA(int x, int y){

	if (x == 0)
		return false;
	
	int i;

	for (i = x-1; i >= 0; i--) {							
		switch (mesa[i][y]) {
			case 0:
				break;
			case 'R':
				return true;
			case 'T':
				return true;
			case 'B':
				return false;
			case 'P':
				return false;
			case 'W':
				if (i == y-1)
					return true;
				else
					return false;
		}
	}
	
	return false;
}

bool DIAG_1(int x, int y){

	if (x == 0 || y == 0)
		return false;
	
	int i,j;

	for (i = x-1,j = y-1; i >= 0 && j >= 0; i--,j--) {							
		switch (mesa[i][j]) {
			case 0:
				break;
			case 'R':
				return true;
			case 'T':
				return false;
			case 'B':
				return true;
			case 'P':
				return false;
			case 'W':
				if (j == y-1)
					return true;
				else
					return false;
		}
	}
	
	return false;
}

bool DIAG_2(int x, int y){

	if (x == 7 || y == 0)
		return false;
	
	int i,j;

	for (i = x+1,j = y-1; i <= 7 && j >= 0; i++,j--) {
		switch (mesa[i][j]) {
			case 0:
				break;
			case 'R':
				return true;
			case 'T':
				return false;
			case 'B':
				return true;
			case 'P':
				return false;
			case 'W':
				if (j == y-1)
					return true;
				else
					return false;
		}
	}
	
	return false;
}

bool DIAG_3(int x, int y){

	if (x == 0 || y == 7)
		return false;
	
	int i,j;

	for (i = x-1,j = y+1; i >= 0 && j <= 7; i--,j++) {
		switch (mesa[i][j]) {
			case 0:
				break;
			case 'R':
				return true;
			case 'T':
				return false;
			case 'B':
				return true;
			case 'P':
				if (j == y+1)
					return true;
				else
					return false;
			case 'W':
				if (j == y+1)
					return true;
				else
					return false;
		}
	}
	
	return false;
}

bool DIAG_4(int x, int y){

	if (x == 7 || y == 7)
		return false;
	
	int i,j;

	for (i = x+1,j = y+1; i <= 7 && j <= 7; i++,j++) {
		switch (mesa[i][j]) {
			case 0:
				break;
			case 'R':
				return true;
			case 'T':
				return false;
			case 'B':
				return true;
			case 'P':
				if (j == y+1)
					return true;
				else
					return false;
			case 'W':
				if (j == y+1)
					return true;
				else
					return false;
		}
	}
	
	return false;
}

int main() {
	int N, rei_x, rei_y;
	int i,j;
	char str[3];
	bool XM = false;
	int flag = 0;

	while (scanf("%d",&N) != EOF) {
	    flag = 0;
		XM = false;
		
		for (i = 0; i < 8; i++){
			for (j = 0; j < 8; j++){
				mesa[i][j] = 0;
			}
		}
		
		for (i = 0; i < N; i++) {
			scanf("%s",&str);
			mesa[str[1]-'a'][str[2]-'1'] = str[0];
		}

		scanf("%s",&str);
		rei_x = str[1]-'a';
		rei_y = str[2]-'1';
		
		for (i = rei_x-1; i <= rei_x+1; i++) {
		    if(flag == 1) break;
			if (i >= 0 && i <= 7) {
				for (j = rei_y-1; j <= rei_y+1; j++) {
					if (j >= 0 && j <= 7){
						if (ACIMA(i,j) == true) XM = true;
						else if (ABAIXO(i,j) == true) XM = true;
						else if (DIREITA(i,j) == true) XM = true;
						else if (ESQUERDA(i,j) == true) XM = true;
						else if (DIAG_1(i,j) == true) XM = true;
						else if (DIAG_2(i,j) == true) XM = true;
						else if (DIAG_3(i,j) == true) XM = true;
						else if (DIAG_4(i,j) == true) XM = true;
						else {
							XM = false;
							flag = 1;
							break;
						}
					}
				}
			}
		}
		if (XM)
			printf("SIMn");
		else
			printf("NAOn")
		
	}
}

N.B.: Code is Collected from Different Sources

life2coding_icon [] URI ONLINE JUDGE SOLUTION : 1206 - Challenge of St. Petersburg (INTERMEDIATE PROBLEM)

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.