QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#707852#4376. Dragon slayerhejinming983282#AC ✓146ms3828kbC++233.1kb2024-11-03 17:50:272024-11-03 17:50:33

Judging History

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

  • [2024-11-03 17:50:33]
  • 评测
  • 测评结果:AC
  • 用时:146ms
  • 内存:3828kb
  • [2024-11-03 17:50:27]
  • 提交

answer

// Author : hejinming2012
#include <bits/stdc++.h>
#define int long long
#define endl '\n'
#define dbg(x) cout << #x " = " << (x) << endl
#define quickio ios::sync_with_stdio(false);
#define quickin cin.tie(0);
#define quickout cout.tie(0);

using namespace std;
inline int read() {
    int now = 0, nev = 1; char c = getchar();
    while(c < '0' || c > '9') { if(c == '-') nev = -1; c = getchar(); }
    while(c >= '0' && c <= '9') { now = (now << 1) + (now << 3) + (c & 15); c = getchar(); }
    return now * nev;
}
void write(int x) {
    if(x < 0) putchar('-'), x = -x;
    if(x > 9) write(x / 10);
    putchar(x % 10 + '0');
}
int n, m, k, f[(1 << 15) + 5];
struct node {
    int X1, Y1, X2, Y2;
} e[20];
int gl[20][20], gd[20][20];
bool vis[20][20];
int xs, ys, xt, yt;
const int dx[] = {-1, 1, 0, 0};
const int dy[] = {0, 0, -1, 1};
int dfs(int x, int y, int ex, int ey) {
    vis[x][y] = 1; int res = 0;
    if(x == ex && y == ey) return 1;
    if(x >= 1 && gl[x][y] == 0 && vis[x - 1][y] == 0)
        res |= dfs(x - 1, y, ex, ey);
    if(x + 1 < n && gl[x + 1][y] == 0 && vis[x + 1][y] == 0)
        res |= dfs(x + 1, y, ex, ey);
    if(y >= 1 && gd[x][y] == 0 && vis[x][y - 1] == 0)
        res |= dfs(x, y - 1, ex, ey);
    if(y + 1 < m && gd[x][y + 1] == 0 && vis[x][y + 1] == 0)
        res |= dfs(x, y + 1, ex, ey);
    return res;
}
void deal(int x) {
    memset(gl, 0, sizeof(gl));
    memset(gd, 0, sizeof(gd));
    memset(vis, 0, sizeof(vis));
    for(int i = 0; i < k; i++)
        if(((x >> i) & 1) == 0)
            if(e[i].X1 == e[i].X2)
                for(int j = e[i].Y1; j < e[i].Y2; j++)
                    gl[e[i].X1][j] = 1;
            else if(e[i].Y1 == e[i].Y2)
                for(int j = e[i].X1; j < e[i].X2; j++)
                    gd[j][e[i].Y1] = 1;
}
int count(int x) {
    int num = 0;
    while(x) num += x & 1, x >>= 1;
    return num;
}
signed main() {
    quickio
    quickin
    quickout
    int T = read();
    while(T--) {
        n = read(), m = read(), k = read();
        xs = read(), ys = read();
        xt = read(), yt = read();
        for(int i = 0; i < k; i++) {
            e[i].X1 = read(), e[i].Y1 = read();
            e[i].X2 = read(), e[i].Y2 = read();
            if(e[i].X1 == e[i].X2) {
                int miny = min(e[i].Y1, e[i].Y2);
                int maxy = max(e[i].Y1, e[i].Y2);
                e[i].Y1 = miny, e[i].Y2 = maxy;
            } else if(e[i].Y1 == e[i].Y2) {
                int minx = min(e[i].X1, e[i].X2);
                int maxx = max(e[i].X1, e[i].X2);
                e[i].X1 = minx, e[i].X2 = maxx;
            }
        }
        memset(f, 0, sizeof(f));
        int ans = k;
        for(int i = 0; i < (1 << k); i++) {
            if(f[i]) continue ;
            deal(i), f[i] = dfs(xs, ys, xt, yt);
            if(f[i]) {
                ans = min(ans, count(i));
                for(int j = 0; j < k; j++)
                    f[i | (1 << j)] |= f[i];
            }
        }
        write(ans), putchar(10);
    }
    return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 146ms
memory: 3828kb

input:

10
4 4 4 0 2 3 3
0 2 4 2
1 3 1 0
2 4 2 1
3 1 3 4
3 2 2 0 0 2 1
0 1 3 1
1 0 1 2
3 2 2 0 0 2 1
2 1 2 2
1 0 1 1
15 15 15 3 12 4 1
8 0 8 15
1 11 15 11
1 1 1 15
3 1 3 15
0 10 14 10
14 1 14 14
8 1 8 15
1 5 14 5
0 15 14 15
0 4 14 4
0 2 15 2
11 0 11 15
4 1 4 15
1 11 15 11
1 12 14 12
15 15 15 8 5 14 0
0 12 1...

output:

1
2
0
5
3
5
1
4
1
0

result:

ok 10 lines