Назад к лучшим решениям Status: AC, problem ZSUD, contest ZEL07. By klopyrev (Konstantin Lopyrev), 2007-02-20 00:30:53.
import java.io.*;
public class Main
{
private static int[] maze;
private static boolean found;
private static boolean[] [] col;
private static boolean[] [] row;
private static boolean[] [] box;
private static boolean[] empty;
private static final int[] c = {0, 1, 2, 3, 4, 5, 6, 7, 8, 0, 1, 2, 3, 4, 5, 6, 7, 8, 0, 1, 2, 3, 4, 5, 6, 7, 8, 0, 1, 2, 3, 4, 5, 6, 7, 8, 0, 1, 2, 3, 4, 5, 6, 7, 8, 0, 1, 2, 3, 4, 5, 6, 7, 8, 0, 1, 2, 3, 4, 5, 6, 7, 8, 0, 1, 2, 3, 4, 5, 6, 7, 8, 0, 1, 2, 3, 4, 5, 6, 7, 8};
private static final int[] r = {0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 8, 8, 8};
private static final int[] b = {0, 0, 0, 1, 1, 1, 2, 2, 2, 0, 0, 0, 1, 1, 1, 2, 2, 2, 0, 0, 0, 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 5, 3, 3, 3, 4, 4, 4, 5, 5, 5, 3, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, 6, 7, 7, 7, 8, 8, 8, 6, 6, 6, 7, 7, 7, 8, 8, 8, 6, 6, 6, 7, 7, 7, 8, 8, 8};
private static long time;
public static void solve (int cur)
{
if (cur == maze.length)
{
found = true;
return;
}
if (!empty [cur])
{
solve (cur + 1);
return;
}
for (int i = 1 ; i <= 9 && !found ; i++)
{
if (!row [r [cur]] [i - 1] && !col [c [cur]] [i - 1] && !box [b [cur]] [i - 1])
{
row [r [cur]] [i - 1] = true;
col [c [cur]] [i - 1] = true;
box [b [cur]] [i - 1] = true;
maze [cur] = i;
solve (cur + 1);
row [r [cur]] [i - 1] = false;
col [c [cur]] [i - 1] = false;
box [b [cur]] [i - 1] = false;
}
}
}
{
time =
System.
currentTimeMillis ();
int T =
Integer.
parseInt (in.
readLine ());
for (int cases = 0 ; cases < T ; cases++)
{
char[] temp = in.readLine ().toCharArray ();
col = new boolean [9] [9];
row = new boolean [9] [9];
box = new boolean [9] [9];
maze = new int [temp.length];
empty = new boolean [temp.length];
int unfilled = 0;
for (int i = 0 ; i < maze.length ; i++)
{
if (temp [i] != '.')
{
maze [i] = temp [i] - '0';
col [c [i]] [maze [i] - 1] = true;
row [r [i]] [maze [i] - 1] = true;
box [b [i]] [maze [i] - 1] = true;
}
else
{
maze [i] = 0;
empty [i] = true;
unfilled++;
}
}
boolean change = true;
while (unfilled != 0 && change)
{
change = false;
int[] [] placeRows = new int [9] [9];
int[] [] rowLoc = new int [9] [9];
int[] [] placeCols = new int [9] [9];
int[] [] colLoc = new int [9] [9];
int[] [] placeBoxes = new int [9] [9];
int[] [] boxA = new int [9] [9];
int[] [] boxB = new int [9] [9];
for (int i = 0 ; i < 81 ; i++)
{
if (empty [i])
{
int number = -1;
boolean valid = true;
for (int j = 1 ; j <= 9 ; j++)
{
if (!row [r [i]] [j - 1] && !col [c [i]] [j - 1] && !box [b [i]] [j - 1])
{
placeRows [r [i]] [j - 1]++;
rowLoc [r [i]] [j - 1] = c [i];
placeCols [c [i]] [j - 1]++;
colLoc [c [i]] [j - 1] = r [i];
placeBoxes [b [i]] [j - 1]++;
boxA [b [i]] [j - 1] = r [i];
boxB [b [i]] [j - 1] = c [i];
if (number == -1)
number = j;
else
valid = false;
}
}
if (valid)
{
row [r [i]] [number - 1] = true;
col [c [i]] [number - 1] = true;
box [b [i]] [number - 1] = true;
maze [i] = number;
empty [i] = false;
change = true;
unfilled--;
}
}
}
if (unfilled != 0)
{
for (int i = 0 ; i < 9 ; i++)
{
for (int j = 0 ; j < 9 ; j++)
{
if (placeRows [i] [j] == 1)
{
boolean done = false;
int cols = rowLoc [i] [j];
int place = i * 9 + cols;
if (!row [r [place]] [j] && !col [c [place]] [j] && !box [b [place]] [j] && empty [place])
{
done = true;
row [r [place]] [j] = true;
col [c [place]] [j] = true;
box [b [place]] [j] = true;
maze [place] = j + 1;
change = true;
empty [place] = false;
unfilled--;
}
}
}
}
}
if (unfilled != 0)
{
for (int i = 0 ; i < 9 ; i++)
{
for (int j = 0 ; j < 9 ; j++)
{
if (placeCols [i] [j] == 1)
{
boolean done = false;
int rows = colLoc [i] [j];
int place = rows * 9 + i;
if (!row [r [place]] [j] && !col [c [place]] [j] && !box [b [place]] [j] && empty [place])
{
done = true;
row [r [place]] [j] = true;
col [c [place]] [j] = true;
box [b [place]] [j] = true;
maze [place] = j + 1;
change = true;
empty [place] = false;
unfilled--;
}
}
}
}
}
if (unfilled != 0)
{
for (int i = 0 ; i < 9 ; i++)
{
for (int j = 0 ; j < 9 ; j++)
{
if (placeBoxes [i] [j] == 1)
{
boolean done = false;
int rows = boxA [i] [j];
int cols = boxB [i] [j];
int place = rows * 9 + cols;
if (!row [r [place]] [j] && !col [c [place]] [j] && !box [b [place]] [j] && empty [place])
{
done = true;
row [r [place]] [j] = true;
col [c [place]] [j] = true;
box [b [place]] [j] = true;
maze [place] = j + 1;
change = true;
empty [place] = false;
unfilled--;
}
}
}
}
}
}
found = false;
if (unfilled == 0)
found = true;
if (!found)
solve (0);
for (int i = 0 ; i < maze.length ; i++)
output += maze [i];
}
}
}
-