task 4 code
task 4 code
# Check rows
for r in range(N):
row_vals = [int(x) for x in grid[r] if x != 'X']
if len(row_vals) != len(set(row_vals)):
return False
# Check columns
for c in range(N):
col_vals = [int(grid[i][c]) for i in range(N) if grid[i][c] != 'X']
if len(col_vals) != len(set(col_vals)):
return False
def find_empty(grid):
for i in range(N):
for j in range(N):
if grid[i][j] == 'X':
return (i, j)
return None
return True
find = find_empty(grid)
if not find:
return True
else:
row, col = find
if solve_sudoku(grid, N):
return True
grid[row][col] = 'X'
return False
Args:
grid: The shifted Sudoku grid.
N: The size of the Sudoku grid.
S: The number of shifts.
Returns:
A list of lists, where each inner list represents a combination of shifts.
Each shift is a tuple: ('R', row_index, shift_value) or ('C', col_index,
shift_value)
"""
all_possible_shifts =
generate_shifts(grid,, S, N, all_possible_shifts)
return all_possible_shifts
# Main function
def main():
num_cases = int(input())
for case_num in range(1, num_cases + 1):
N, S = map(int, input().split())
grid =
for _ in range(N):
row = input().split()
grid.append(row)
all_shifts = find_all_shifts(grid, N, S)
print(f"Case #{case_num