Dado un tablero N * N que inicialmente está vacío y una secuencia de consultas, cada consulta consta de dos números enteros X e Y donde se pinta la celda (X, Y) . La tarea es imprimir el número de la consulta después de lo cual habrá un cuadrado de 3 * 3 en el tablero con todas las celdas pintadas.
Si no hay tal cuadrado después de procesar todas las consultas, imprima -1 .
Ejemplos:
Entrada: N = 4, q = {{1, 1}, {1, 2}, {1, 3}, {2, 2}, {2, 3}, {1, 4}, {2, 4} , {3, 4}, {3, 2}, {3, 3}, {4, 1}}
Salida: 10
Después del décimo movimiento, existe un cuadrado de 3X3, de (1, 1) a (3, 3 ) (indexación basada en 1).
Entrada: N = 3, q = {(1, 1), {1, 2}, {1, 3}}
Salida: -1
Enfoque: Una observación importante aquí es que cada vez que coloreamos un cuadrado, puede ser parte del cuadrado requerido de 9 maneras diferentes (cualquier celda del cuadrado de 3 * 3). Para cada posibilidad, verifique si la celda actual es parte de cualquier cuadrado donde están pintadas las 9 celdas. Si la condición se cumple, imprima el número de consultas procesadas hasta el momento; de lo contrario, imprima -1 después de que se hayan procesado todas las consultas.
A continuación se muestra la implementación del enfoque anterior:
CPP
// C++ implementation of the approach #include <bits/stdc++.h> 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<vector<bool> >& 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<pair<int, int> > moves) { int x, y; vector<vector<bool> > 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<pair<int, int> > 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.
Javascript
<script> // Javascript implementation of the approach // Function that returns true // if the coordinate is inside the grid function valid(x, y, 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 function count(n, cx, cy, board) { var ct = 0; // Iterate through 3 rows for (var x = cx; x <= cx + 2; x++) // Iterate through 3 columns for (var 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 function minimumMoveSquare(n, m, moves) { var x, y; var board = Array.from(Array(n+1), ()=>Array(n+1).fill(false)); for (var i = 0; i < moves.length; i++) { 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 (var cx = x - 2; cx <= x; cx++) for (var cy = y - 2; cy <= y; cy++) if (count(n, cx, cy, board) == 9) return i + 1; } return -1; } // Driver code var n = 3; var moves = [ [ 1, 1 ], [ 1, 2 ], [ 1, 3 ] ]; var m = moves.length; document.write( minimumMoveSquare(n, m, moves)); // This code is contributed by famously. </script>
-1
Publicación traducida automáticamente
Artículo escrito por Abdullah Aslam y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA