Question

In: Computer Science

C Programming Language (Code With C Programming Language) Problem Title : Which Pawn? Jojo is playing...

C Programming Language (Code With C Programming Language)

Problem Title : Which Pawn?

Jojo is playing chess himself to practice his abilities. The chess that Jojo played was N × N. When Jojo was practicing, Jojo suddenly saw a position on his chessboard that was so interesting that Jojo tried to put the pieces of Rook, Bishop and Knight in that position. Every time he put a piece, Jojo counts how many other pieces on the chessboard can be captured in one step. After all the pieces are tried, Jojo has a new idea in his game, which is Jojo will put one of the three pieces above in a position that can capture the most pieces but at the least cost. The cost to use Rook, Bishop and Knight is worth 3,2,1 respectively. Help Jojo calculate the minimum cost in each position that Jojo wants to try.

Note: The three pieces cannot capture a piece if it is blocked by another piece, except Knight who has the ability to be able to jump on other piece. Each chessboard row and column will be numbered from 1 to N. Here are the rules for moving pieces.
• Knight can move 1 horizontal step followed by 2 vertical steps or 1 vertical step followed by 2 horizontal steps that make up the letter L with an amount of 8 possible directions.
• Bishop can make diagonal moves to the top left, top right, bottom left and bottom right.
• Rook can move horizontally to the left, horizontal to the right, vertically up and vertically down.

Format Input

In the first line there are 2 integers N, M, where N represents the size of the chessboard and M represents the number of pieces that were on the original chessboard. The next M lines are 2 integers Xi and Yi representing the position of the initial chess pieces. X represents rows in chessboards numbered 1 through N from top to bottom and Y
represents columns in chessboards numbered 1 to N from left to right. In the next line there are integers Q representing the number of positions that Jojo will try. The next Q line is Ai and Bi represents the position that Jojo will try with the three pieces above.

Format Output

Output Q line with 1 integer X which represents the smallest cost that can capture as many other pieces as possible.

Constraints

  • 1 ≤ N ≤ 200
  • 1 ≤ M + Q ≤ 40000
  • 1 ≤ Ai, Bi, Xi, Yi ≤ N

Sample Input & Output (standard input & output)

3 5
1 1
1 2
1 3
2 1
2 3
4
2 2
Case #1: 3
3 1
Case #2: 1
3 2
Case #3: 1
3 3
Case #4: 1

Explanation:

Following is a description of the starting position of each piece on the chessboard, where 1 means there is a piece at that position and 0 means empty.

1 1 1
1 0 1
0 0 0

Here is a list of the pieces that can be captured by the three pieces in the first query with position 2,2.
Knight can capture 0 pieces.
Bishop can capture 2 pieces that are in [1,1] and [1,3].
Rook can capture 3 pieces that are in [1,2], [2,1], and [2,3].
Because Rook can capture the most pieces, Jojo will use Rook for a fee of 3.

Here is a list of the pieces that can be captured by the three pieces in the third query with position 3,2.
Knight can capture 2 pieces that are in [1,1] and [1,3].
Bishop can capture 2 piece that is in [2,1] and [2,3].
Rook can capture 1 piece that is in [1,2].
Because Knight and Bishop can capture the most pieces, Jojo will use Knight because it costs less which is 1.

Solutions

Expert Solution

#include<stdio.h>

//Displacement of the knight's moves
int a[] = {-2,-1,1,2,2,1,-1,-2};
int b[] = {1,2,2,1,-1,-2,-2,-1};


int rook(int board[200][200], int x, int y, int n){
    int ans=0;
    int a=x-1,b=y-1;
    while(a>=0 && b>=0 && a<n && b<n)
        if(board[a++][b]==1){
            ans++;
            break;
        }
    a=x-1,b=y-1;
    while(a>=0 && b>=0 && a<n && b<n)
        if(board[a--][b]==1){
            ans++;
            break;
        }
    a=x-1,b=y-1;
    while(a>=0 && b>=0 && a<n && b<n)
        if(board[a][b++]==1){
            ans++;
            break;
        }
    a=x-1,b=y-1;
    while(a>=0 && b>=0 && a<n && b<n)
        if(board[a][b--]==1){
            ans++;
            break;
        }
    return ans;
}

int knight(int board[200][200], int x, int y, int n){
    int ans=0;
    int i;
    int x_move,y_move;
    for(i = 0; i < 8;i++){
        x_move=x+a[i]-1;
        y_move=y+b[i]-1;
        if(x_move>=0 && x_move<n && y_move>=0 && y_move<n)
            if(board[x_move][y_move]==1)
                ans++;
    }
    return ans;
}

int bishop(int board[200][200], int x, int y, int n){
    int ans=0;
    int a=x-1,b=y-1;
    while(a>=0 && b>=0 && a<n && b<n)
        if(board[a--][b--]==1){
            ans++;
            break;
        }
    a=x-1,b=y-1;
    while(a>=0 && b>=0 && a<n && b<n)
        if(board[a--][b++]==1){
            ans++;
            break;
        }
    a=x-1,b=y-1;
    while(a>=0 && b>=0 && a<n && b<n)
        if(board[a++][b++]==1){
            ans++;
            break;
        }
    a=x-1,b=y-1;
    while(a>=0 && b>=0 && a<n && b<n)
        if(board[a++][b--]==1){
            ans++;
            break;
        }
    return ans;
}

int main() {
    int n,m;
    int board[200][200]={0};
    int x,y;
    int q;
    int r_ans,k_ans,b_ans;
    int ans;
    scanf("%d%d",&n,&m);
    while(m-- > 0){
        scanf("%d%d",&x,&y);
        board[--x][--y]=1;
    }
    scanf("%d",&q);
    while(q-- > 0){
        scanf("%d%d",&x,&y);
        r_ans=rook(board,x,y,n);
        k_ans=knight(board,x,y,n);
        b_ans=bishop(board,x,y,n);
        
        //Here I've done some basic manipulation to find the correct answer;
         r_ans=r_ans*10+1;
         k_ans=k_ans*10+3;
         b_ans=b_ans*10+2;
         
        ans=(r_ans > b_ans && r_ans > k_ans)?r_ans:(b_ans > k_ans)?b_ans:k_ans;
        
        //Redoing the manipulation and getting the answer
        ans=ans%10;
        if(ans==3)
            ans=1;
        else if(ans==1)
            ans=3;
        printf("%d\n",ans);
    }
    return 0;
    
}


Related Solutions

C Programming Language Problem Title : Take Three Jojo just graduated and moved up to grade...
C Programming Language Problem Title : Take Three Jojo just graduated and moved up to grade 4. Today is his first day in 4th grade. Unfortunately, the lessons are held online because of the pandemic. So that the quality of learning remains good, Jojo's teacher gives a hard task for 4th grader. After the 4th graders finished their first task which is prime factorization. Jojo's teacher set up a game for the stundets. The game is very simple. Given N...
C Programming Language Problem Title : 4th Grade Jojo just graduated and moved up to grade...
C Programming Language Problem Title : 4th Grade Jojo just graduated and moved up to grade 4. Today is his first day in 4th grade. Unfortunately, the lessons are held online because of pandemic. So that the quality of learning remains good, Jojo’s teacher gives a hard task for 4th grader. The first task is to find the prime factorization of a number. Prime number is a natural number greater than 1 that is not a product of two smaller...
C Programming Language Title : Making wave In Physics, Mathematics, and related fields, a wave is...
C Programming Language Title : Making wave In Physics, Mathematics, and related fields, a wave is a disturbance of one of more fields such that the field values oscillate repeatedly about a stable equilibrium value. Waves are usually represented using mathematical functions of the form F (x, t), where x = position and t = time. Your task is to write a program that will visualize a given wave for exactly N seconds. You do not need to worry about...
Rewrite the C PROGRAMMING LANGUAGE CODE in terms of only dereferencing (*) and pointer addition (+)...
Rewrite the C PROGRAMMING LANGUAGE CODE in terms of only dereferencing (*) and pointer addition (+) AND extend the code so that allocated memory is freed properly. Thank you struct foo { int a; char b; }; int main(void) { struct foo* arr[5]; int x; for(x = 0; x < 5; x++) { arr[x] = malloc(sizeof(struct foo)); arr[x]->a = 0; arr[x]->b = 'b'; } }
Programming language: C++   suggested software: Code::Blocks Develop an algorithm and write a C++ program that computes...
Programming language: C++   suggested software: Code::Blocks Develop an algorithm and write a C++ program that computes the final score of a baseball game. Use a loop to read the number of runs scored by both teams during each of nine innings. Display the final score afterward. Submit your design, code, and execution result via file, if possible
Code in C-language programming description about convert binary number to decimal number.
Code in C-language programming description about convert binary number to decimal number.
Code in C++ programming language description about read and write data to memory example.
Code in C++ programming language description about read and write data to memory example.
Be sure to use only C for the Programming Language in this problem. Before we start...
Be sure to use only C for the Programming Language in this problem. Before we start this, it is imperative that you understand the words “define”, “declare” and “initialize” in context of programming. It's going to help you a lot when following the guidelines below. Let's begin! Define two different structures at the top of your program. be sure to define each structure with exactly three members (each member has to be a different datatype). You may set them up...
C Programming language problem I need to write a program which uses several threads (like 8...
C Programming language problem I need to write a program which uses several threads (like 8 threads for example) to sum up a number. The following program is using 1 thread but I need several threads to compile it. There was a hint saying that using multiple separate for loop and combining them will make a multi-threaded program. #include <stdio.h> #include <stdlib.h> #include <pthread.h> int sum; // this data is shared by the threads void *runner(void *param); // threads call...
Complete the following exercises using C programming language. Take screenshots of the code and its output...
Complete the following exercises using C programming language. Take screenshots of the code and its output where specified and paste them into in a well-labeled Word document for submission. Scenario Assume you are the CIO of an organization with three different IT department locations with separate costs. You want a program to perform simple IT expenditure calculations. Your IT expenditure target is $35,000 per site. Site expenditures: Site 1 – $35,000. Site 2 – $37,500. Site 3 – $42,500. Exercise...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT