Java Algorithm Blue Bridge Cup lattice position

Source: Internet
Author: User

Problem Descriptionenter three natural number n,i,j (1<=i<=n,1<=j<=n), output in a n*n lattice of the board, with the lattice (i,j) peers, the same column, the same diagonal all the lattice position. Input FormatEnter a total of three lines, enter the natural number n,i,j. which guarantees n<=24 and 1<=i<=n,1<=j<=n. output Formatoutput total four rows. The first behavior is the position of all the squares with the lattice (I,J), the second behavior is the position of all the squares of the same column as the lattice (I,J), the third behavior from the top left to the lower right diagonal of the lattice position, and the fourth behavior from the bottom left to the upper right diagonal of the lattice position. Sample Input4
2
3Sample Output( 2,1) (2,2) (2,3) (2,4)
( 1,3) (2,3) (3,3) (4,3)
(2,3) ( 3,4 )
( 4,1) (3,2) (2,3) (1,4)input and Output sample interpretationn=4,i=2,j=3 represents the grid in the second row and third column of the chessboard, such as:
1th column 2nd Column 3rd Column 4th Column
Line 1th
(2,3) Line 2nd
Line 3rd
Line 4th

( 2,1) (2,2) (2,3) (2,4) {The position of the lattice on the same line}
( 1,3) (2,3) (3,3) (4,3) {The position of the lattice on the same column}
(2,3) ( 3,4) {The position of the lattice on top left to bottom right diagonal}
( 4,1) (3,2) (2,3) (1,4) {The position of the lattice on the top left-to-right diagonal}Code::

Package Blue Bridge cup Java algorithm training;

Import Java.util.Scanner;


public class _3 lattice position {
public static void Main (string[] args) {
Scanner Scanner = new Scanner (system.in);
int n,i,j;
N = Scanner.nextint ();
i = Scanner.nextint ();
J = scanner.nextint ();
//Peer
for (int j2 = 1, J2 <= N; j2++) {
for (int k = 1; k <= N; k++) {
if (J2 = = i) System.out.print ( "(" +j2+ "," +k+ ")");
}
}
System.out.println ();
The same column
for (int j2 = 1, J2 <= N; j2++) {
for (int k = 1; k <= N; k++) {
if (k = = j) System.out.print ("(" +j "+k+");
}
}
System.out.println ();
The position of the lattice from top left to bottom right diagonal
for (int j2 = 1; j2 <= N; j2++) {
for (int k = 1; k <= N; k++) {
if ((k-j2) = = (J-i) | | (j2-k) = = (I-j)) System.out.print ("(" +j2+ "," +k+ ");
}
}
System.out.println ();
//The position of the lattice from bottom left to upper right diagonal
for (int j2 = N; J2 >= 1; j2--) {
for (int k = n; K >= 1; k--) {
if ((k+j2) = = (J+i)) System.out.print ("(" +j2+ "," +k+ ")");
}
}

}
}

Java algorithm Blue Bridge cup grid position

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.