TLE 求助
  • 板块UVA1309 Sudoku
  • 楼主lzyqwq
  • 当前回复1
  • 已保存回复1
  • 发布时间2022/9/10 12:15
  • 上次更新2023/10/27 12:08:25
查看原帖
TLE 求助
539211
lzyqwq楼主2022/9/10 12:15
#include <bits/stdc++.h>
using namespace std;
char ma[22][22];
int cnt, row[4097], num[1025], ans[1145], mat[22][22];
struct link {
    int x, y, u, d, l, r;
}e[200001];
inline void init() {
    memset(num, 0, sizeof(num));
    memset(row, 0, sizeof(row));
    for (int i = 0; i < 1025; ++i) {
        e[e[i].u = e[i].d = i].l = (e[i].r = i + 1) - 2;
    }
    e[e[1024].r = 0].l = cnt = 1024;
}
inline int grid(int x, int y) {
	if (x < 5) {
		if (y < 5) {
			return 1;
		} else if (y < 9) {
			return 2;
		} else if (y < 13) {
			return 3;
		} else {
			return 4;
		}
	} else if (x < 9) {
		if (y < 5) {
			return 5;
		} else if (y < 9) {
			return 6;
		} else if (y < 13) {
			return 7;
		} else {
			return 8;
		}
	} else if (x < 13) {
		if (y < 5) {
			return 9;
		} else if (y < 9) {
			return 10;
		} else if (y < 13) {
			return 11;
		} else {
			return 12;
		}
	} else {
		if (y < 5) {
			return 13;
		} else if (y < 9) {
			return 14;
		} else if (y < 13) {
			return 15;
		} else {
			return 16;
		}
	}
}
inline int getr(int u, int v, int w) {
    return (u - 1 << 8) + (v - 1 << 4) + w;
}
inline int getc(int u, int v, int w, int id) {
    if (id == 1) {
		return (u - 1 << 4) + w;
	} else if (id == 2) {
		return 256 + (v - 1 << 4) + w;
	} else if (id == 3) {
		return 512 + (grid(u, v) - 1 << 4) + w;
	} else {
		return 768 + (u - 1 << 4) + v;
	}
}
inline void add(int x, int y) {
    e[++cnt].x = x;
    ++num[e[cnt].y = e[cnt].d = y];
    e[y].u = e[e[cnt].u = e[y].u].d = cnt;
    if (row[x]) {
        e[e[cnt].r = row[x]].l = e[e[cnt].l = e[row[x]].l].r = cnt;
    } else {
        e[cnt].l = e[cnt].r = row[x] = cnt;
    }
}
inline void remove(int y) {
    for (int i = e[y].d; i ^ y; i = e[i].d) {
        for (int j = e[i].r; j ^ i; j = e[j].r) {
            e[e[e[j].u].d = e[j].d].u = e[j].u;
            --num[e[j].y];
        }
    }
    e[e[e[y].r].l = e[y].l].r = e[y].r;
}
inline void resume(int y) {
    for (int i = e[y].d; i ^ y; i = e[i].d) {
        for (int j = e[i].r; j ^ i; j = e[j].r) {
            e[e[j].u].d = e[e[j].d].u = j;
            ++num[e[j].y];
        }
    }
    e[e[y].r].l = e[e[y].l].r = y;
}
bool dance(int dep) {
    if (!e[0].r) {
        for (int i = 1, x, y, v; i ^ dep; ++i) {
			x = (ans[i] - 1 >> 8) + 1;
			y = (ans[i] - 1 >> 4) % 16 + 1;
			v = (ans[i] - 1) % 16 + 1;
			mat[x][y] = v;
		}
		return 1;
    }
    int c = e[0].r;
    for (int i = e[0].r; i; i = e[i].r) {
        if (num[i] < num[c]) {
            c = i;
        }
    }
    remove(c);
    for (int i = e[c].d; i ^ c; i = e[i].d) {
        ans[dep] = e[i].x;
        for (int j = e[i].r; j ^ i; j = e[j].r) {
            remove(e[j].y);
        }
        if (dance(dep + 1)) {
            return 1;
        }
        for (int j = e[i].l; j ^ i; j = e[j].l) {
            resume(e[j].y);
        }
    }
    resume(c);
    return 0;
}
int main() {
    while (scanf("%s", ma[1] + 1)) {
        init();
        for (int i = 2; i < 17; ++i) {
            scanf("%s", ma[i] + 1);
        }
        for (int i = 1; i < 17; ++i) {
            for (int j = 1; j < 17; ++j) {
                for (int k = 1; k < 17; ++k) {
                    if ((ma[i][j] ^ '-') && ((ma[i][j] - 'A' + 1) ^ k)) {
                        continue;
                    }
                    for (int l = 1; l < 5; ++l) {
						add(getr(i, j, k), getc(i, j, k, l));
					}
                }
            }
        }
        bool o = dance(1);
        for (int i = 1; i < 17; ++i) {
			for (int j = 1; j < 17; ++j) {
				putchar(mat[i][j] + 'A' - 1);
			}
			puts("");
		}
    }
}

双倍经验 A 了,是不是常数问题

2022/9/10 12:15
加载中...