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 Problem Title : Museum Heist Jojo loves art. In his free time, he...
C Programming Language Problem Title : Museum Heist Jojo loves art. In his free time, he usually goes to the museum and admires the artwork there. since Jojo loves are so much, he is planning a heist at the museum. Jojo knows the price of every piece of art and doesn't want to raise suspicion, so he decided to steal the second most expensive art piece. It is guaranteed that there are at least two art pieces with different prices....
C PROGRAMMING LANGUAGE PROBLEM TITLE : ARRAY usually, if people want to input number into an...
C PROGRAMMING LANGUAGE PROBLEM TITLE : ARRAY usually, if people want to input number into an array, they will put it from index 0 until N - 1 using for. But, Bibi is bored to code like that. So, she didin't want to input the number that way. So Bibi challenged you to make a program that will read a sequence (represent index) that she made, then input the number to an array but input it with the same sequence...
C PROGRAMMING LANGUAGE Problem title : Bibi's Array Bibi also has an array containing N elements....
C PROGRAMMING LANGUAGE Problem title : Bibi's Array Bibi also has an array containing N elements. Like Lili, Bibi wants to know the highest frequency (most occurrences) and all elements which have that frequency. Format Input The first line contains an integer T stating the number of test cases. For each test case, the first line contains a single integer N which indicate the number of element in the array. The next line contains N integers Xi (1≤ i ≤...
Programming assignment (100 pts): In the C++ programming language write a program capable of playing Tic-Tac-Toe...
Programming assignment (100 pts): In the C++ programming language write a program capable of playing Tic-Tac-Toe against the user. Your program should use OOP concepts in its design. You can use ASCII art to generate and display the 3x3 playing board. The program should randomly decide who goes first computer or user. Your program should know and inform the user if an illegal move was made (cell already occupied). The program should also announce if one of the players wins...
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...
Write a code in C or C++ programming language that generates the hexadecimal values in Table...
Write a code in C or C++ programming language that generates the hexadecimal values in Table 6-2 in the same format. Table 6-2 Hexadecimal text file specifying the contents of a 4 × 4 multiplier ROM. 00: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 10: 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F 20: 00 02 04 06 08 0A 0C 0E 10...
In the C++ programming language write a program capable of playing Tic-Tac-Toe against the user. Your...
In the C++ programming language write a program capable of playing Tic-Tac-Toe against the user. Your program should use OOP concepts in its design. You can use ASCII art to generate and display the 3x3 playing board. The program should randomly decide who goes first computer or user. Your program should know and inform the user if an illegal move was made (cell already occupied). The program should also announce if one of the players wins or if a draw...
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'; } }
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT