Finish the problem we started in class, using the method of brute force.
Sample input:
3, 1, A, 3, C, 8, A
3, 1, A, 6, C, 8, B
3, 1, B, 6, B, 9, C
2, 1, C, 5, B
2, 3, B, 7, A
Sample output:
ABCBCACAB
ACBBACCBA
BCACABABC
CABABCBCA
CABBCAABC
Test input:
4, 1, A, 2, B, 8, A, 9, B
3, 1, A, 2, B, 9, A
3, 3, C, 6, B, 7, C
2, 7, A, 6, C
2, 1, C, 6, A
Test output:
ABCBCACAB
ABCCABBCA
BACACBCBA
CBABACACB
CABBCAABC
Java code we started:
import java.util.*; class Main { public static boolean check(int[][] grid) { for(int[] i : grid) { if(i[0]+i[1]+i[2]!=3 || i[0]*i[1]*i[2]!=0) { return false; } } for(int i=0; i<3; i++) { if(grid[0][i]+grid[1][i]+grid[2][i]!=3 || grid[0][i]*grid[1][i]*grid[2][i]!=0) { return false; } } return true; } public static int toInt(String s){ if(s.equals("A")) return 0; else if(s.equals("B")) return 1; else return 2; } public static void main(String[] args) { Scanner in = new Scanner(System.in); for(int i=0; i<5; i++){ int[][] grid = new int[3][3]; String[] inputs = in.nextLine().split(", "); for(int j=1; j<inputs.length; j+=2){ int c = Integer.parseInt(inputs[j])-1; grid[c/3][c%3] = toInt(inputs[j+1]); } } } }
Python code we started:
def toInt(letter): if letter=="A": return 0 elif letter=="B": return 1 else: return 2 def check(grid): for t in grid: if t[0]+t[1]+t[2]!=3 or t[0]*t[1]*t[2]!=0: return False for t in range(0,3): if grid[0][t]+grid[1][t]+grid[2][t]!=3 or grid[0][t]*grid[1][t]*grid[2][t]!=0: return False return True for p in range(0,5): grid = [[0,0,0],[0,0,0],[0,0,0]] inputs = input().split(", ") for u in range(1,len(inputs),2): c = int(inputs[u])-1 grid[c//3][c%3] = toInt(inputs[u+1])
It only works for some of the test input but I didn't know how to fix it:
def toInt(char):
if char == 'A':
return 0
elif char == 'B':
return 1
elif char == 'C':
return 2
def toString(char):
if char == 0:
return 'A'
elif char == 1:
return 'B'
elif char == 2:
return 'C'
letter_int = [0, 1, 2]
starting = input().split(', ')
grid = [[4, 4, 4], [4, 4, 4], [4, 4, 4]]
for i in range(1, len(starting), 2):
c = int(starting[i]) - 1
grid[c//3][c % 3] = toInt(starting[i + 1])
copy = [row[:] for row in grid]
def assign(copy, row, col):
unavail = [0, 0, 0]
avail = -1
r = 0
c = 0
letter_index = 0
for c in range(0, 3):
if copy[row][c] != 4:
letter_index = copy[row][c]
unavail[letter_index] = 1
for r in range (0, 3):
if copy[r][col] != 4:
letter_index = copy[r][col]
unavail[letter_index] = 1
for letter_index in range (0, 3):
if unavail[letter_index] == 0:
avail = letter_int[letter_index]
break
return avail
def once(board):
for row in board:
if row[0] + row[1] + row[2] != 3 or row[0]*row[1]*row[2] != 0:
return False
for col in range(0, 3):
if board[0][col] + board[1][col] + board[2][col] != 3 or board[0][col] * board[1][col] * board[2][col] != 0:
return False
return True
for s in range(0, 1):
for row in range(0, 3):
for col in range (0, 3):
if copy[row][col] == 4:
copy[row][col] = assign(copy, row, col)
no_dup = once(copy)
if no_dup == True:
letter_grid = ''
for row in copy:
for char in row:
letter_grid += toString(char)
print(letter_grid)