Sudoku Killer |
| Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) |
| Total Submission(s): 1293 Accepted Submission(s): 424 |
|
Problem Description
Since the first several Sino-World Championships from March 10 to 11, 2006, the number of this game is increasingly loved and paid attention to.
It is said that at the 2008 Beijing Olympics, the number is alone as a separate project, and the champion will have a huge prize --- HDU free seven-day tour Extra Lcy checking and chance to take a photo with HDU ACM Team. So before the world, the servant is successively trained for the prizes day and night. Of course, it also includes the beginner Linle, but he is too stupid and there is not much patience, can only do the most basic number of autism, but he still wants to get those prizes, can you help him? As long as you tell him about the answer, you don't have to teach him how to do it. The rule of the number of games is this: in a 9x9 square, you need to fill in the number 1-9 to the space, and make the checkered each line and each column. They all contain 1-9 nine numbers. At the same time, it is also necessary to divide the thick line in the space into 9 3x3 squares, also contain 1-9 nine numbers. For example, there is such a question, you can take a closer look at each line, each column, and each 3X3 checker contains 1-9 nine numbers. Example:
answer:
|
|
Input
This topic contains multiple sets of tests, separated from one space between each group. Each group of tests will give you a 9 * 9 matrix, and the two elements adjacent to the same line are separated by a space. Among them, 1-9 represents the number of filledments in this location, question mark (?) Means you need to fill the number.
|
|
Output
For each group test, then output its solution, and the two numbers adjacent to the same line are separated by a space. There is an empty line between the two groups. For each set of test data, it is guaranteed to have only one solution. |
|
Sample Input
7 1 2 ? 6 ? 3 5 8 ? 6 5 2 ? 7 1 ? 4 ? ? 8 5 1 3 6 7 2 9 2 4 ? 5 6 ? 3 7 5 ? 6 ? ? ? 2 4 1 1 ? 3 7 2 ? 9 ? 5 ? ? 1 9 7 5 4 8 6 6 ? 7 8 3 ? 5 1 9 8 5 9 ? 4 ? ? 2 3 |
|
Sample Output
7 1 2 4 6 9 3 5 8 3 6 5 2 8 7 1 9 4 4 9 8 5 1 3 6 7 2 9 2 4 1 5 6 8 3 7 5 7 6 3 9 8 2 4 1 1 8 3 7 2 4 9 6 5 2 3 1 9 7 5 4 8 6 6 4 7 8 3 2 5 1 9 8 5 9 6 4 1 7 2 3 |
|
Author
linle
|
|
Source
ACM Summer Training Team Exercise (3)
|
|
Recommend
LL
|
This question is to input and output, it is easy to go wrong.
I just started to use% C, every time I have to get Char () to go to space and enter;
Later, I used to use% s with% s.
This output is the output space between the two groups, and the last set of data does not have to output.
Because there is no direct return after finding, the results are not right, and I will find it wrong for a long time.
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
char map[10][10];
int x[100];
int y[100];
int vx[10][10];
int vy[10][10];
int vz[10][10];
int cnt;
bool flag;
void dfs(int deep)
{
if (flag)return;
if (deep == cnt) { flag = true; return; }
int i = deep;
for (int j = 1; j <= 9; j++)
{
IF (flag) return; // less
int temp = 0;
IF (vx [x [i]] [j] == 1) Continue; // is not full
IF (Vy [Y]] [J] == 1) Continue; // Not satisfied
IF (x [i]> = 0 && x [i] <3) // does not satisfy 3 * 3
{
if (y[i] >= 0 && y[i] < 3)
{
temp = 0;
if(vz[0][j] == 1)continue;
}
else if (y[i] >= 3 && y[i] < 6)
{
temp = 1;
if (vz[1][j] == 1)continue;
}
else if (y[i] >= 6 && y[i]< 9)
{
temp = 2;
if (vz[2][j] == 1)continue;
}
}
else if (x[i] >= 3 && x[i] < 6)
{
if (y[i] >= 0 && y[i] < 3)
{
temp = 3;
if (vz[3][j] == 1)continue;
}
else if (y[i] >= 3 && y[i] < 6)
{
temp = 4;
if (vz[4][j] == 1)continue;
}
else if (y[i] >= 6 && y[i]< 9)
{
temp = 5;
if (vz[5][j] == 1)continue;
}
}
else if (x[i] >= 6 && x[i] < 9)
{
if (y[i] >= 0 && y[i]< 3)
{
temp = 6;
if (vz[6][j] == 1)continue;
}
else if (y[i] >= 3 && y[i] < 6)
{
temp = 7;
if (vz[7][j] == 1)continue;
}
else if (y[i] >= 6 && y[i]< 9)
{
temp = 8;
if (vz[8][j] == 1)continue;
}
}
map[x[i]][y[i]] = j+'0';
vx[x[i]][j] = 1;
vy[y[i]][j] = 1;
vz[temp][j] = 1;
dfs(deep + 1);
vx[x[i]][j] = 0;
vy[y[i]][j] = 0;
vz[temp][j] = 0;
}
}
int main()
{
int l = 0;
char str[10];
while (scanf("%s", &str) != EOF)
{
map[0][0] = str[0];
for (int i = 0; i < 9; i++)
{
for (int j = 0; j < 9; j++)
{
if (i == 0 && j == 0)continue;
scanf("%s", &str);
map[i][j] = str[0];
}
}
cnt = 0;
memset(vx, 0, sizeof(vx));
memset(vy, 0, sizeof(vy));
memset(vz, 0, sizeof(vz));
for (int i = 0; i < 9; i++)
{
for (int j = 0; j < 9; j++)
{
if (map[i][j] == '?') { x[cnt] = i; y[cnt++] = j; }
else
{
int temp = map[i][j] - '0';
vx[i][temp] = 1;
vy[j][temp] = 1;
if (i >= 0 && i < 3)
{
if (j >= 0 && j < 3)
{
vz[0][temp] = 1;
}
else if (j >= 3 && j < 6)
{
vz[1][temp] = 1;
}
else if (j >= 6 && j< 9)
{
vz[2][temp] = 1;
}
}
else if (i >= 3 && i < 6)
{
if (j >= 0 && j < 3)
{
vz[3][temp] = 1;
}
else if (j >= 3 && j < 6)
{
vz[4][temp] = 1;
}
else if (j >= 6 && j< 9)
{
vz[5][temp] = 1;
}
}
else if (i >= 6 && i < 9)
{
if (j >= 0 && j < 3)
{
vz[6][temp] = 1;
}
else if (j >= 3 && j < 6)
{
vz[7][temp] = 1;
}
else if (j >= 6 && j< 9)
{
vz[8][temp] = 1;
}
}
}
}
}
flag = false;
if (l)printf("\n");
dfs(0);
l++;
for (int i = 0; i < 9; i++)
{
for (int j = 0; j < 8; j++)
{
printf("%c ", map[i][j]);
}
printf("%c\n", map[i][8]);
}
}
return 0;
}
Problem Address: http: //acm.hdu.edu.cn/showproblem.php pid = 1426? A topic Dfs can be solved. The idea is not difficult, as long as a trial on a number line. But strangely, every time I tried ...
Problem Description Since the first Sudoku World Championships from March 10 to 11, 2006, the game of Sudoku has been loved and valued more and more by people. It is said that in the 2008 Beijing Olym...
Deep search questions, for each'?' box, determine in turn whether filling in 1~9 satisfies the same number in the row, column, and small nine squares. Keep going....
Sudoku rule is this: in a grid of 9x9, you need to fill in the spaces among the numbers 1-9, and the squares of each row and each column contains nine figures 1-9. But also to ensure, with a thick lin...
Title description Since the first Sudoku World Championships from March 10 to 11, 2006, the game of Sudoku has become more and more popular. It is said that in the 2008 Beijing Olympic Games, Sudoku w...
Luogu 1784 Sudoku: The first question is to give you a 9x9 unfilled Sudoku (not filled with 0), asking you to fill in the numbers of these 0s to meet the number. Unique characteristics. parsing: In fa...
Topic connection:http://acm.hdu.edu.cn/showproblem.php?pid=1426 Problem Description Since the first Sudoku World Championships from March 10 to 11, 2006, the game of Sudoku has become more and more po...
Click to open the link Sudoku Killer Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 9634 Acc...
Sudoku Killer Link to the original question https://vjudge.net/contest/345248#problem/P is to write a program to calculate Sudoku Sudoku requires that the ranks and the numbers in each small nine-squa...
Sudoku Killer Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 1289 Accepted Submission(s): 422 Problem Description Since the first several Sino-Wo...