Little Monkey Programming C++ | Little Monkey’s Chess Game

Learn C++ from a young age! Record the questions in the learning process of Xueersi Monkey programming and record every moment. Any infringement will be deleted immediately, thank you for your support!

Attached is a summary post: Little Monkey Programming C++ | Summary-CSDN Blog


[Title description]

The little monkey and his friends are playing a game. Initially,  n × m chess pieces  are placed on  an n × m chessboard  . Each chess piece has a certain value. The  i -th (1≤ in ) row  j ( 1≤ jm ) The value of the chess piece in the column is  vi , j .

The rules of this game are: randomly select two square areas on the chessboard (  the upper left corner grid of  the k- th (1≤ k ≤2) square is in the xk-th  row and  yk -th  column, and the side length is  lenk ). Players on both sides are required to use the shortest Calculate the total value in these two square areas within the time required. Whoever calculates the correct result first will win this game.

Now the little monkey wants to know that under the rules he set, there will be a total of  Q  rounds of the game, and what is the correct result of each game, so that it can be used to check whether the results calculated by the players on both sides are correct.

【enter】

The first line contains two integers  n and m .

The next  n  lines, each line contains  m  integers  vi ,1, vi ,2,..., vi , m .

The next line contains an integer  q .

Next are  the q  lines, each line contains 6 integers  x 1, y 1, len 1, x 2, y 2, len 2.

【Output】

There are t lines in total   , each line has an integer representing the answer.

【Input sample】

2 2
1 2
3 4
3
1 1 1 2 2 1
1 2 1 2 1 1
2 1 1 2 2 1

【Output sample】

5
5
7

[Detailed code explanation]

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 810, M = 810;
int n, m, q;
LL s[N][M];
int main()
{
    cin >> n >> m;
    for (int i=1; i<=n; i++) {
        for (int j=1; j<=m; j++) {
            cin >> s[i][j];
            s[i][j] += s[i][j-1];
        }
    }
    cin >> q;
    while (q--)
    {
        int x1, y1, len1, x2, y2, len2;
        cin >> x1 >> y1 >> len1 >> x2 >> y2 >> len2;
        int x3 = x1 + len1 - 1, y3 = y1 + len1 - 1;
        int x4 = x2 + len2 - 1, y4 = y2 + len2 - 1;
        LL res = 0;
        for (int i=x1; i<=x3; i++) {
            res += s[i][y3] - s[i][y1-1];
        }
        for (int i=x2; i<=x4; i++) {
            res += s[i][y4] - s[i][y2-1];
        }
        if (!(x3<x2 || x4<x1 || y3<y2 || y4<y1)) {
            int x[] = {x1, x2, x3, x4};
            int y[] = {y1, y2, y3, y4};
            sort(x, x+4);
            sort(y, y+4);
            for (int i=x[1]; i<=x[2]; i++) {
                res -= s[i][y[2]] - s[i][y[1]-1];
            }
        }
        cout << res << endl;
    }
    return 0;
}

【operation result】

3 4
1 2 3 4
-1 -2 -3 -4
6 7 -6 -7
3
1 1 2 2 3 2
-20
1 1 3 2 2 1
7
1 1 2 2 2 2
-2

Guess you like

Origin blog.csdn.net/guolianggsta/article/details/133895969