📌  相关文章
📜  最小移动次数,之后存在3X3彩色正方形

📅  最后修改于: 2021-04-29 02:50:22             🧑  作者: Mango

给定一个最初为空的N * N板并执行一系列查询,每个查询都由两个整数XY组成,其中绘制了单元格(X,Y)。任务是打印查询的编号,此后在板上将显示一个3 * 3的正方形,并绘制了所有单元格。
如果在处理完所有查询后没有这样的正方形,则打印-1
例子:

方法:这里的一个重要观察结果是,每次我们为一个正方形着色时,它都可以以9种不同方式(3 * 3正方形的任何单元)成为所需正方形的一部分。对于每种可能性,请检查当前单元格是否是所有9个单元格都已绘制的正方形的一部分。如果满足条件,则打印到目前为止已处理的查询数,否则在处理完所有查询后打印-1。
下面是上述方法的实现:

CPP
// C++ implementation of the approach
 
#include 
using namespace std;
 
// Function that returns true
// if the coordinate is inside the grid
bool valid(int x, int y, int n)
{
    if (x < 1 || y < 1 || x > n || y > n)
        return false;
    return true;
}
 
// Function that returns the count of squares
// that are coloured in the 3X3 square
// with (cx, cy) being the top left corner
int count(int n, int cx, int cy,
          vector >& board)
{
    int ct = 0;
 
    // Iterate through 3 rows
    for (int x = cx; x <= cx + 2; x++)
 
        // Iterate through 3 columns
        for (int y = cy; y <= cy + 2; y++)
 
            // Check if the current square
            // is valid and coloured
            if (valid(x, y, n))
                ct += board[x][y];
    return ct;
}
 
// Function that returns the query
// number after which the grid will
// have a 3X3 coloured square
int minimumMoveSquare(int n, int m,
                      vector > moves)
{
    int x, y;
    vector > board(n + 1);
 
    // Initialize all squares to be uncoloured
    for (int i = 1; i <= n; i++)
        board[i].resize(n + 1, false);
    for (int i = 0; i < moves.size(); i++) {
        x = moves[i].first;
        y = moves[i].second;
 
        // Mark the current square as coloured
        board[x][y] = true;
 
        // Check if any of the 3X3 squares
        // which contains the current square
        // is fully coloured
        for (int cx = x - 2; cx <= x; cx++)
            for (int cy = y - 2; cy <= y; cy++)
                if (count(n, cx, cy, board) == 9)
                    return i + 1;
    }
 
    return -1;
}
 
// Driver code
int main()
{
    int n = 3;
    vector > moves = { { 1, 1 },
                                      { 1, 2 },
                                      { 1, 3 } };
    int m = moves.size();
 
    cout << minimumMoveSquare(n, m, moves);
 
    return 0;
}


Python3
# Python3 implementation of the approach
 
# Function that returns True
# if the coordinate is inside the grid
def valid(x, y, n):
 
    if (x < 1 or y < 1 or x > n or y > n):
        return False;
    return True;
 
 
# Function that returns the count of squares
# that are coloured in the 3X3 square
# with (cx, cy) being the top left corner
def count(n, cx, cy, board):
 
    ct = 0;
 
    # Iterate through 3 rows
    for x in range(cx, cx + 3):
 
        # Iterate through 3 columns
        for y in range(cy + 3):
 
            # Check if the current square
            # is valid and coloured
            if (valid(x, y, n)):
                ct += board[x][y];
    return ct;
 
# Function that returns the query
# number after which the grid will
# have a 3X3 coloured square
def minimumMoveSquare(n, m, moves):
 
    x = 0
    y = 0
    board=[[] for i in range(n + 1)]
 
    # Initialize all squares to be uncoloured
    for i in range(1, n + 1):
        board[i] = [False for i in range(n + 1)]
 
    for  i in range(len(moves)):
     
        x = moves[i][0];
        y = moves[i][1];
 
        # Mark the current square as coloured
        board[x][y] = True;
 
        # Check if any of the 3X3 squares
        # which contains the current square
        # is fully coloured
        for cx in range(x - 2, x + 1 ):
            for cy in range(y - 2, y + 1):   
                if (count(n, cx, cy, board) == 9):
                    return i + 1;
    return -1;
 
# Driver code
if __name__=='__main__':
 
    n = 3;
    moves = [[ 1, 1 ],[ 1, 2 ], [ 1, 3 ]]
    m = len(moves)
 
    print(minimumMoveSquare(n, m, moves))
 
    # This code is contributed by rutvik_56.


输出:
-1