rt,不是这题,是教练的 加强版
(这题只需要水水的 dfs 就过了,加强版要加拓扑排序,我按教练的拓扑排名并且优化了下标,但还是卡不过,最慢的点跑 3 秒多,55 诶嘿嘿嘿教练刚才把时限开到了 5 秒我水过去了真香)
谢谢,估计我的代码常数太大了 qaq
#include <bits/stdc++.h>
using namespace std;
int zqzakioi (int x, int y) {
return (x - 1) * 9 + y;
}
int head[100];
int cnt;
int indeg[100];
int tuopu[100]; // index of tuopu
int Index;
struct node {
int u, v;
} e[100];
void AddEdge (int u, int v) {
e[++cnt].u = v;
e[cnt].v = head[u];
head[u] = cnt;
}
int Box[11][11] = {
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 1, 1, 1, 2, 2, 2, 3, 3, 3},
{0, 1, 1, 1, 2, 2, 2, 3, 3, 3},
{0, 1, 1, 1, 2, 2, 2, 3, 3, 3},
{0, 4, 4, 4, 5, 5, 5, 6, 6, 6},
{0, 4, 4, 4, 5, 5, 5, 6, 6, 6},
{0, 4, 4, 4, 5, 5, 5, 6, 6, 6},
{0, 7, 7, 7, 8, 8, 8, 9, 9, 9},
{0, 7, 7, 7, 8, 8, 8, 9, 9, 9},
{0, 7, 7, 7, 8, 8, 8, 9, 9, 9}
};
struct qaq {
int Col;
int Row;
int Gongge;
int Tuopu;
} sudaku[90];
bool fjrakioi (qaq x, qaq y) {
if (x.Gongge != y.Gongge) return x.Gongge < y.Gongge;
else if (x.Tuopu != y.Tuopu) return x.Tuopu < y.Tuopu;
else if (x.Col != y.Col) return x.Col < y.Col;
else return x.Row < y.Row;
}
bool col[11][11]; // hang
bool row[11][11]; // lie
bool box[11][11]; // gongge
int a[11][11];
bool lvr[11][11]; // left and right
bool uvd[11][11]; // up and down
void dfs (int cur) {
if (cur > 81) {
for (int i = 1; i <= 9; i++) {
for (int j = 1; j <= 9; j++)
printf("%d ", a[i][j]);
puts("");
}
exit(0);
}
int x = sudaku[cur].Col;
int y = sudaku[cur].Row;
int Max = -1;
if (x != 1 && a[x - 1][y] != 0 && uvd[x - 1][y] == '^') Max = max(Max, a[x - 1][y]);
if (x != 9 && a[x + 1][y] != 0 && uvd[x + 1][y] == 'v') Max = max(Max, a[x + 1][y]);
if (y != 1 && a[x][y - 1] != 0 && lvr[x][y - 1] == '<') Max = max(Max, a[x][y - 1]);
if (y != 9 && a[x][y + 1] != 0 && lvr[x][y + 1] == '>') Max = max(Max, a[x][y + 1]);
if (Max == -1) Max = 1;
for (int i = Max; i <= 9; i++) {
if (col[x][i] == true) continue;
if (row[y][i] == true) continue;
if (box[Box[x][y]][i] == true) continue;
if (x > 1 && x - 1 % 3 != 0 && a[x - 1][y] != 0) {
char tmp = uvd[x - 1][y];
if (tmp == '^')
if (a[x - 1][y] > i)
continue;
if (tmp == 'v')
if (a[x - 1][y] < i)
continue;
} // a[x - 1][y] & a[x][y]
if (y > 1 && y - 1 % 3 != 0 && a[x][y - 1] != 0) {
char tmp = lvr[x][y - 1];
if (tmp == '<')
if (a[x][y - 1] > i)
continue;
if (tmp == '>')
if (a[x][y - 1] < i)
continue;
} // a[x][y - 1] & a[x][y]
if (x < 9 && x + 1 % 3 != 0 && a[x + 1][y] != 0) {
char tmp = uvd[x + 1][y];
if (tmp == '^')
if (a[x + 1][y] > i)
continue;
if (tmp == 'v')
if (a[x + 1][y] < i)
continue;
} // a[x + 1][y] & a[x][y]
if (y < 9 && y + 1 % 3 != 0 && a[x][y + 1] != 0) {
char tmp = lvr[x][y + 1];
if (tmp == '<')
if (a[x][y + 1] > i)
continue;
if (tmp == '>')
if (a[x][y + 1] < i)
continue;
} // a[x][y + 1] & a[x][y]
a[x][y] = i;
col[x][i] = true;
row[y][i] = true;
box[Box[x][y]][i] = true;
dfs(cur + 1);
col[x][i] = false;
row[y][i] = false;
box[Box[x][y]][i] = false;
a[x][y] = 0;
}
}
int main () {
for (int i = 1; i <= 9; i++) {
for (int j = 1; j <= 9; j++) { // left and right
if (j % 3 == 0) continue;
scanf(" %c", &lvr[i][j]);
}
for (int j = 1; j <= 9; j++) { // up and down
if (i % 3 == 0) continue;
scanf(" %c", &uvd[i][j]);
}
}
for (int i = 1; i <= 9; i++)
for (int j = 1; j <= 9; j++) {
int tmp = zqzakioi(i, j);
if (j % 3 != 0 && lvr[i][j] == '<') AddEdge(tmp, tmp + 1), indeg[tmp + 1]++;
if (j % 3 != 0 && lvr[i][j] == '>') AddEdge(tmp + 1, tmp), indeg[tmp]++;
if (i % 3 != 0 && uvd[i][j] == '^') AddEdge(tmp, tmp + 1), indeg[tmp + 1]++;
if (i % 3 != 0 && uvd[i][j] == 'v') AddEdge(tmp + 1, tmp), indeg[tmp]++;
}
queue <int> q;
for (int i = 1; i <= 81; i++)
if (indeg[i] == 0)
q.push(i);
while (!q.empty()) {
int cur = q.front();
tuopu[++Index] = cur;
q.pop();
for (int p = head[cur]; p; p = e[p].v) {
int tmp = e[p].u;
indeg[tmp]--;
if (indeg[tmp] == 0) q.push(tmp);
}
}
int cnt_c = 1, cnt_r = 0;
for (int i = 1; i <= 81; i++) {
cnt_r++;
if (cnt_r > 9) cnt_c++, cnt_r = 1;
sudaku[i].Col = cnt_c;
sudaku[i].Row = cnt_r;
sudaku[i].Gongge = Box[cnt_c][cnt_r];
int cnt_t;
for (int j = 1; j <= 81; j++)
if (tuopu[j] == i) {
cnt_t = i;
break;
}
sudaku[i].Tuopu = cnt_t;
}
sort(sudaku + 1, sudaku + 82, fjrakioi);
dfs(1);
return 0;
}