- 1
Chess
I try to solve this task: Chess rook moves horizontally or vertically. Given two different cells of the chessboard, determine whether a rook can go from the first cell to the second in one move. The program receives the input of four numbers from 1 to 8, each specifying the column and row number, first two - for the first cell, and then the last two - for the second cell. The program should output YES if a rook can go from the first cell to the second in one move, or NO otherwise here you can see the original task: https://snakify.org/de/lessons/if_then_else_conditions/problems/rook_move/ I should use the if, elif, else function for it. I think now for many days and can't find a solution. Can anybody help?
20 ответов
+ 6
if (a-c)*(b-d)==0:print("Yes")
else:print("No")
+ 3
Since a rook can only move horizontally or vertically, for it to be able to reach another square within one move, both squares must be either on the same row or the same column. Therefore, it is sufficient to check if either the two rows or the two columns are equal. If either of the pairs is equal, the rook can perform the move, otherwise it would take two moves.
+ 3
HonFu Alexander Thiem i think we got it
+ 2
Ah, I see, so blocking own or eneny stones in the path and cases like this don't even come in.
+ 1
You take a two-dimensional array and put something in there that signifies the pieces, for example 0 for nothing, 1 for rook.
Then you have to calculate, which fields your rook can access, by looping.
And if the target coordinates from the input are in the collection of available fields, you get a True, otherwise False.
There are a lot of chess codes in Code Playground that you can check for these kind of things.
Otherwise please *try* to solve this task, writing the code yourself, even if it's hard, even if you fail first. Honestly try to figure it out - applying your own hands.
If you get stuck, show us your code.
+ 1
this array thing help me. i try on my own I just wanted to have some tipps :D
+ 1
Yeah you can create a list of 64 variables!! As you can see my code also shows that your use of or was correct
https://code.sololearn.com/chGWresqnGeK/?ref=app
+ 1
if (a-c)*(b-d):print("No")
else:print("Yes")
is shorter isnt it
+ 1
Hmm Yes😉
+ 1
Awright, Oma Falk, then I'll stop googling for golfing technique now. 😏
0
I use snakify to learn python because I can learn very well with this. The only problem is I can't ask questions. So I do it here. Lists are in a later chapter so I also think there should be a solution with out.
0
if I have
a = int(input())
b = int(input())
c = int(input())
d = int(input())
Feld11 = a == 1 and b == 1 or c == 1 and d == 1
Feld12 = a == 1 and b == 2 or c == 1 and d == 2
Feld13 = a == 1 and b == 3 or c == 1 and d == 3
can i create a list with 64 new variables? to have all fields defined?
I am not so sure about my use of "or"
0
Wow, Oma, that simple, hm? 😁👍
0
print("No" if (a-c)*(b-d) else "Yes") is shorter yet. 😉
0
I solved it like this:
https://code.sololearn.com/c8j9Elza7x7l/#py
0
Good
0
import java.io.*;
import java.util.*;
public class Solution {
public static void main(String[] args) {
Scanner scan= new Scanner(System.in);
int a=scan.nextInt(); //first rook line;
int b=scan.nextInt(); //first rook column
int x=scan.nextInt(); //second rook line
int y=scan.nextInt(); //second rook column
if (a==x || b==y){
System.out.println("YES");
}else{
System.out.println("NO");
}
}
}