QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#80924#2943. NeighborsidontreallyknowAC ✓619ms3564kbC++176.4kb2023-02-25 11:12:172023-02-25 11:12:21

Judging History

你现在查看的是最新测评结果

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-02-25 11:12:21]
  • 评测
  • 测评结果:AC
  • 用时:619ms
  • 内存:3564kb
  • [2023-02-25 11:12:17]
  • 提交

answer

#include<bits/stdc++.h>
#include<bits/extc++.h>
using namespace std;
using namespace __gnu_pbds;
template<class T, class S>
ostream& operator << (ostream &o, const pair<T, S> &p) { return o<<'('<<p.first<<", "<<p.second<<')'; }
template<typename TH> void _dbg(const char* sdbg, TH h) { cerr<<sdbg<<"="<<h<<"\n"; }
template<typename TH, typename... TA> void _dbg(const char* sdbg, TH h, TA... t){
    while(*sdbg != ',') { cerr<<*sdbg++; } cerr<<"="<<h<<","; _dbg(sdbg+1, t...);
}
#ifdef DEBUG
#define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
#define debugv(x) {{cerr<<#x<<": "; for(auto i:x) cerr<<i<<" "; cerr<<endl;}}
#define debugr(l,r,x) {{cerr<<#x<<": "; for(int i=l;i<=r;i++) cerr<<x<<" "; cerr<<endl;}}
#else
#define debug(...) (__VA_ARGS__)
#define debugv(x)
#define debugr(l,r,x)
#define cerr while(0) cerr
#endif
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
//#define int long long
typedef __int128 INT;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pii;
#define priority_queue std::priority_queue
#define F first
#define S second
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
#define rep(x,l,r) for(auto x=l; x<r; x++)
template<class T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

vi adj[300];
vi nadj[300];
int idx(int x, int y){
    return (x << 4) + y;
}

pii unidx(int x){
    return pii(x >> 4, x & 15);
}

/**
 * Author: Dion Ong
 * Description: Standard sudoku solver for an N by N grid (1-indexed rows/cols).
 * Need to fill with entries from 1 to N; no repeats in columns, rows, and K by L boxes.
 * Time: Problem specific. Has worked on N ~ 9.
 * Status: Tested on GNY 2019, SPOJ EASUDOKU
 */

template <typename T>
struct sudoku{
    int N=9, K=3, L=3; T blnk=0;
    vector<vector<T>> a;
    set<pair<pii, int>> available;
    vector<pii> val[300];
    inline pair<pii, int> get_elt(int x, int y) {
        return {{score(x, y), -adj[idx(x, y)].size()}, idx(x, y)};
    }
    inline void remove(int x, int y) {
        if(a[x][y]) return;
        available.erase(get_elt(x, y));
    }
    inline void add(int x, int y) {
        if(a[x][y]) return;
        available.insert(get_elt(x, y));
    }
    sudoku(int N, int K, int L, vector<vector<T>> &a): N(N), K(K), L(L), a(a){
        for(int i=1; i<=N; i++){
            for(int j=1; j<=N; j++){
                add(i, j);
            }
        }
    }
    bool find(int &x, int &y){
        if(available.empty())
            return false;
        auto [a, b] = unidx(available.begin()->S);
        x = a;
        y = b;
        return true;
    }
    bool in_col(int x, T v){
        for(int i=1;i<=N;i++)
            if(a[i][x]==v) return true;
        return false;
    }
    bool in_row(int x, T v){
        for(int i=1;i<=N;i++)
            if(a[x][i]==v) return true;
        return false;
    }
    bool neighbor(int x, int y, T v) {
        for (auto cell : adj[idx(x, y)]) {
            auto [x1, y1] = unidx(cell);
            if (a[x1][y1] && abs(a[x1][y1] - v) != 1)
                return false;
        }
        for (auto cell : nadj[idx(x, y)]) {
            auto [x1, y1] = unidx(cell);
            if (a[x1][y1] && abs(a[x1][y1] - v) == 1)
                return false;
        }
        return true;
    }
    bool good(int x, int y, T v){
        if(in_row(x, v) || in_col(y, v)) return false;
        if(!neighbor(x, y, v)) return false;
        return true;
    }
    int score(int x, int y){
        int ret = 0;
        vi done(N+1, 0);
        for (int i=1; i<=N; i++) {
            done[a[x][i]] = true;
            done[a[i][y]] = true;
        }
        for(int i=1; i<=N; i++){
            if (done[i]) continue;
            ret += neighbor(x, y, i);
        }
        return ret;
    }
    bool solve(){
        int x, y;
        if(!find(x, y)) return true;
        for(T i=1;i<=N;i++){
            if(good(x, y, i)){
                for (int i=1; i<=N; i++) {
                    remove(x, i);
                    remove(i, y);
                }
                a[x][y]=i;
                for (int i=1; i<=N; i++) {
                    add(x, i);
                    add(i, y);
                }
                available.erase(get_elt(x, y));
                if(solve()) return true;
                for (int i=1; i<=N; i++) {
                    remove(x, i);
                    remove(i, y);
                }
                a[x][y]=blnk;
                for (int i=1; i<=N; i++) {
                    add(x, i);
                    add(i, y);
                }
            }
        }
        return false;
    }
};

signed main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    int n, k;
    cin >> n >> k;
    vector v(n+1, vector(n+1, 0));
    for (int i = 1; i < 2*n; i++){
        int iv = (i+1)/2;
        if (i % 2){
            for (int j = 1; j < n; j++) {
                char c;
                cin >> c;
                if (c == '1'){
                    adj[idx(iv, j)].push_back(idx(iv, j+1));
                    adj[idx(iv, j+1)].push_back(idx(iv, j));
                }
            }
        }
        else {
            for (int j = 1; j <= n; j++) {
                char c;
                cin >> c;
                if (c == '1') {
                    adj[idx(iv, j)].push_back(idx(iv+1, j));
                    adj[idx(iv+1, j)].push_back(idx(iv, j));
                }
            }
        }
    }
    auto inside = [&](int x, int y){
        return 0 < x && 0 < y && x <= n && y <= n;
    };
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            vector<pii> dirs = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
            for (auto [dx, dy] : dirs){
                int cx = i+dx, cy = j+dy;
                if (inside(cx, cy) && find(all(adj[idx(i, j)]), idx(cx, cy)) == adj[idx(i, j)].end()){
                    nadj[idx(i, j)].push_back(idx(cx, cy));
                }
            }
        }
    }
    while(k--){
        int x, y, s;
        cin >> x >> y >> s;
        v[x][y] = s;
    }
    sudoku solver(n, 1, 1, v);
    solver.solve();
    for (int i=1; i<=n; i++){
        for(int j=1; j<=n; j++){
            cout << solver.a[i][j] << " ";
        }
        cout << "\n";
    }
}

详细

Test #1:

score: 100
Accepted
time: 2ms
memory: 3472kb

input:

7 6
100000
0111010
100000
0000000
001001
0010010
000000
0000000
000000
0000000
001000
0001010
000001
2 7 7
6 1 2
3 3 2
7 6 2
5 3 6
4 7 6

output:

4 3 5 7 1 6 2 
1 2 4 6 3 5 7 
7 5 2 1 6 3 4 
3 7 1 5 2 4 6 
5 1 6 2 4 7 3 
2 6 3 4 7 1 5 
6 4 7 3 5 2 1 

result:

ok 7 lines

Test #2:

score: 0
Accepted
time: 2ms
memory: 3524kb

input:

7 6
100011
0010000
000001
1101010
010010
0111100
010000
0001000
011011
0001000
010101
0100000
000010
4 3 2
4 5 7
1 2 6
5 4 5
2 4 2
5 1 1

output:

5 6 4 7 1 2 3 
3 1 5 2 4 6 7 
4 2 1 3 6 7 5 
6 3 2 4 7 5 1 
1 7 6 5 2 3 4 
7 4 3 6 5 1 2 
2 5 7 1 3 4 6 

result:

ok 7 lines

Test #3:

score: 0
Accepted
time: 3ms
memory: 3556kb

input:

8 9
0001000
10110100
1000000
00001110
0001001
10100011
0000101
00000100
0101001
10000000
0000000
00100101
0010001
00100000
0000010
3 5 3
4 1 7
6 8 6
4 5 1
1 4 7
1 3 5
1 5 6
3 6 6
6 2 8

output:

4 2 5 7 6 8 1 3 
5 6 4 8 2 7 3 1 
8 1 7 2 3 6 4 5 
7 3 8 6 1 2 5 4 
2 5 6 3 4 1 7 8 
1 8 3 5 7 4 2 6 
6 4 2 1 5 3 8 7 
3 7 1 4 8 5 6 2 

result:

ok 8 lines

Test #4:

score: 0
Accepted
time: 2ms
memory: 3512kb

input:

9 9
00000100
010001001
01010010
010000010
00100101
100000010
10100010
010000100
01000001
100000000
00000000
010100000
10010100
000010100
00101000
110100000
00001000
3 5 6
4 2 1
7 1 4
2 2 6
6 9 1
5 7 4
3 1 1
4 3 5
9 4 4

output:

3 5 2 7 1 9 8 6 4 
9 6 7 3 4 8 1 2 5 
1 7 9 8 6 4 5 3 2 
2 1 5 6 9 7 3 4 8 
8 2 3 9 5 1 4 7 6 
7 4 6 2 8 3 9 5 1 
4 3 8 1 2 5 6 9 7 
6 8 4 5 3 2 7 1 9 
5 9 1 4 7 6 2 8 3 

result:

ok 9 lines

Test #5:

score: 0
Accepted
time: 4ms
memory: 3512kb

input:

10 11
010000000
1000000000
010101011
0101000001
001001000
0100100000
101000000
0000000001
000001000
0000101010
000000000
0000000001
100000000
0001000000
000001001
1000110000
100000011
1100000011
111010101
5 8 3
9 1 7
3 8 4
3 4 3
10 6 9
4 9 2
6 3 4
2 3 5
1 2 2
8 4 9
2 5 1

output:

9 2 3 8 4 6 10 7 1 5 
10 6 5 2 1 4 3 9 8 7 
1 5 2 3 9 7 8 4 10 6 
3 4 7 6 8 1 5 10 2 9 
5 1 9 4 2 8 7 3 6 10 
2 9 4 7 3 10 6 8 5 1 
4 3 8 10 7 5 1 6 9 2 
6 10 1 9 5 3 4 2 7 8 
7 8 10 1 6 2 9 5 4 3 
8 7 6 5 10 9 2 1 3 4 

result:

ok 10 lines

Test #6:

score: 0
Accepted
time: 619ms
memory: 3460kb

input:

11 12
0000001101
00000001000
0011000000
00110000000
0010000010
00000110000
0000100000
00000001100
0000000000
01010000010
0000010000
00001010000
0000000101
00000010010
0100100000
00100000101
1000010100
00100001010
1001000000
00010001000
0000000000
8 7 7
8 9 1
10 11 9
6 5 10
9 2 8
1 10 8
4 6 6
2 4 10
...

output:

2 9 11 1 3 10 6 5 4 8 7 
5 7 9 10 11 2 4 6 8 3 1 
1 3 8 9 4 7 2 10 5 6 11 
4 10 1 5 7 6 3 8 11 9 2 
9 4 6 3 5 11 1 7 10 2 8 
3 5 2 4 10 8 9 11 7 1 6 
6 1 7 2 9 5 8 4 3 11 10 
11 6 5 8 2 3 7 9 1 10 4 
7 8 4 11 6 9 10 1 2 5 3 
10 11 3 7 8 1 5 2 6 4 9 
8 2 10 6 1 4 11 3 9 7 5 

result:

ok 11 lines

Test #7:

score: 0
Accepted
time: 2ms
memory: 3508kb

input:

4 3
000
0001
001
0010
010
0100
100
1 2 1
2 2 4
1 4 2

output:

3 1 4 2 
1 4 2 3 
4 2 3 1 
2 3 1 4 

result:

ok 4 lines

Test #8:

score: 0
Accepted
time: 0ms
memory: 3508kb

input:

5 3
1111
00110
1010
10010
0000
00110
0101
01001
0000
5 2 2
2 4 3
2 5 5

output:

5 4 3 2 1 
2 1 4 3 5 
3 5 1 4 2 
1 3 2 5 4 
4 2 5 1 3 

result:

ok 5 lines

Test #9:

score: 0
Accepted
time: 0ms
memory: 3412kb

input:

6 5
10000
000100
10110
110100
10000
010011
01000
001000
00100
001110
00101
6 5 2
5 6 5
3 6 2
4 3 2
5 4 4

output:

1 2 5 3 6 4 
4 5 1 2 3 6 
3 4 6 1 5 2 
5 3 2 6 4 1 
2 6 3 4 1 5 
6 1 4 5 2 3 

result:

ok 6 lines

Test #10:

score: 0
Accepted
time: 2ms
memory: 3524kb

input:

7 6
011101
0001001
000000
0000010
000010
0000100
001001
0010001
001000
0001000
100000
0000001
100100
3 7 1
4 4 1
7 3 4
2 1 4
3 3 6
6 2 3

output:

1 4 5 6 7 3 2 
4 6 1 7 2 5 3 
7 2 6 3 5 4 1 
3 5 2 1 4 7 6 
5 1 3 4 6 2 7 
2 3 7 5 1 6 4 
6 7 4 2 3 1 5 

result:

ok 7 lines

Test #11:

score: 0
Accepted
time: 2ms
memory: 3480kb

input:

8 9
0011000
00000010
1000000
01011000
0110000
10000000
1000010
00000010
1010101
10110010
0010000
00000001
0000000
11001010
0000000
5 2 1
8 8 1
7 2 2
4 5 5
7 6 1
3 3 3
3 4 2
6 3 6
8 7 5

output:

1 7 2 3 4 6 8 5 
4 5 8 1 6 2 7 3 
8 4 3 2 7 5 1 6 
7 6 1 8 5 3 4 2 
2 1 5 6 8 7 3 4 
3 8 6 5 1 4 2 7 
5 2 4 7 3 1 6 8 
6 3 7 4 2 8 5 1 

result:

ok 8 lines

Test #12:

score: 0
Accepted
time: 1ms
memory: 3564kb

input:

9 10
00010000
001000000
00010000
000001100
00001001
000100000
00100101
001000111
01001001
100000000
10000001
000100010
00110010
000000001
01010000
000000001
00000000
9 8 9
4 1 4
4 6 7
3 1 1
3 6 3
7 1 2
8 2 5
8 4 2
4 3 5
2 9 1

output:

9 4 8 1 2 6 3 7 5 
5 2 7 9 8 4 6 3 1 
1 6 2 7 4 3 5 8 9 
4 1 5 6 9 7 8 2 3 
7 3 4 8 6 5 9 1 2 
6 7 9 3 1 8 2 5 4 
2 9 3 4 5 1 7 6 8 
8 5 6 2 3 9 1 4 7 
3 8 1 5 7 2 4 9 6 

result:

ok 9 lines