QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#56721 | #4376. Dragon slayer | qinjianbin | AC ✓ | 250ms | 3856kb | C++ | 7.0kb | 2022-10-21 10:17:08 | 2022-10-21 10:17:11 |
Judging History
answer
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<algorithm>
#include<cctype>
#include<ctime>
#include<queue>
#include<stack>
#include<deque>
#include<map>
#include<unordered_map>
#include<set>
#include<unordered_set>
#include<bitset>
using namespace std;
//STL库常用(vector, map, set, pair)
#define pii pair<int,int>
#define pdd pair<double,double>
#define F first
#define S second
//常量
#define PI (acos(-1.0))
#define INF 0x3f3f3f3f
//必加
#ifdef TanJI
#include<cassert>
#define dbg(...) do {cerr << "\033[33;1m" << #__VA_ARGS__ << " -> "; err(__VA_ARGS__); } while(0)
void err() {cerr << "\033[39;0m" << endl;}
template<template<typename...> class T, typename t, typename... A> void err(T<t> a, A... x) {for (auto v: a) cerr << v << ' '; cerr << ", "; err(x...);}
template<typename T, typename... A> void err(T a, A... x) {cerr << a << ' '; err(x...);}
template<typename T> void err(T *a, int len) {for (int i = 0; i < len; i++) cerr << a[i] << ' '; err();}
#else
#define dbg(...)
#define assert(...)
#endif
typedef unsigned long long ull;
typedef long long ll;
typedef double db;
// 快读快输
template<typename T> inline T read() {T x=0; bool f=0; char c=getchar();while(!isdigit(c)){if(c =='-')f= 1; c=getchar();}while(isdigit(c)){x=(x<<3)+(x<<1)+(c^48);c=getchar();}return f?~x+1:x;}
template<typename T> inline void print(T k){ int num = 0,ch[20]; if(k == 0){ putchar('0'); return ; } (k<0)&&(putchar('-'),k = -k); while(k>0) ch[++num] = k%10, k /= 10; while(num) putchar(ch[num--]+48); }
//图论
const int GRAPHN = 1, GRAPHM = 1;
int h[GRAPHN], e[GRAPHM], ne[GRAPHM], wei[GRAPHM], idx;
void AddEdge(int a, int b) {e[idx] = b, ne[idx] = h[a], h[a] = idx++;}
void AddEdge(int a, int b, int c) {e[idx] = b, ne[idx] = h[a], wei[idx] = c, h[a] = idx++;}
void AddEdge(int h[], int a, int b) {e[idx] = b, ne[idx] = h[a], h[a] = idx++;}
void AddEdge(int h[], int a, int b, int c) {e[idx] = b, ne[idx] = h[a], wei[idx] = c, h[a] = idx++;}
// 数学公式
template<typename T> inline T gcd(T a, T b){ return b==0 ? a : gcd(b,a%b); }
template<typename T> inline T lowbit(T x){ return x&(-x); }
template<typename T> inline bool mishu(T x){ return x>0?(x&(x-1))==0:false; }
template<typename T1,typename T2, typename T3> inline ll q_mul(T1 a,T2 b,T3 p){ ll w = 0; while(b){ if(b&1) w = (w+a)%p; b>>=1; a = (a+a)%p; } return w; }
template<typename T,typename T2> inline ll f_mul(T a,T b,T2 p){ return (a*b - (ll)((long double)a/p*b)*p+p)%p; }
template<typename T1,typename T2, typename T3> inline ll q_pow(T1 a,T2 b,T3 p){ ll w = 1; while(b){ if(b&1) w = (w*a)%p; b>>=1; a = (a*a)%p;} return w; }
template<typename T1,typename T2, typename T3> inline ll s_pow(T1 a,T2 b,T3 p){ ll w = 1; while(b){ if(b&1) w = q_mul(w,a,p); b>>=1; a = q_mul(a,a,p);} return w; }
template<typename T> inline ll ex_gcd(T a, T b, T& x, T& y){ if(b == 0){ x = 1, y = 0; return (ll)a; } ll r = ex_gcd(b,a%b,y,x); y -= a/b*x; return r;/*gcd*/ }
template<typename T1,typename T2> inline ll com(T1 m, T2 n) { int k = 1;ll ans = 1; while(k <= n){ ans=((m-k+1)*ans)/k;k++;} return ans; }
template<typename T> inline bool isprime(T n){ if(n <= 3) return n>1; if(n%6 != 1 && n%6 != 5) return 0; T n_s = floor(sqrt((db)(n))); for(int i = 5; i <= n_s; i += 6){ if(n%i == 0 || n%(i+2) == 0) return 0; } return 1; }
/* ----------------------------------------------------------------------------------------------------------------------------------------------------------------- */
const int MAXN = 60;
int T, n, m, K;
struct Point {
int x, y;
Point(int x = 0, int y = 0) : x(x), y(y) {}
void input() {
scanf("%d%d", &x, &y);
}
};
Point st, dest;
struct Edge {
Point p1, p2;
bool h = false;
Edge(bool h = false) : h(h) {}
}edg[MAXN];
const int DIR[4][2] = {0, 1, 1, 0, 0, -1, -1, 0};
int wall[MAXN][MAXN][4];
bool vis[MAXN][MAXN];
bool dfs(int sta) {
for (int i = 1; i <= K; i++) {
if ((sta >> (i - 1)) & 1) continue;
// printf("asd %d %d\n", i, sta);
if (edg[i].h) {
int y = edg[i].p1.y;
for (int j = edg[i].p1.x + 1; j <= edg[i].p2.x; j++) {
wall[j][y][0]++;
wall[j][y + 1][2]++;
}
}
else {
int x = edg[i].p1.x;
for (int j = edg[i].p1.y + 1; j <= edg[i].p2.y; j++) {
wall[x][j][1]++;
wall[x + 1][j][3]++;
}
}
}
memset(vis, false, sizeof vis);
bool flag = false;
queue<Point> que;
que.push(st);
vis[st.x][st.y] = true;
while (que.size()) {
Point cur = que.front();
que.pop();
for (int i = 0; i < 4; i++) {
if (wall[cur.x][cur.y][i] == 0) {
int x = cur.x + DIR[i][0], y = cur.y + DIR[i][1];
if (vis[x][y]) continue;
que.push(Point(x, y));
vis[x][y] = true;
}
}
}
for (int i = 1; i <= K; i++) {
if ((sta >> (i - 1)) & 1) continue;
if (edg[i].h) {
int y = edg[i].p1.y;
for (int j = edg[i].p1.x + 1; j <= edg[i].p2.x; j++) {
wall[j][y][0]--;
wall[j][y + 1][2]--;
}
} else {
int x = edg[i].p1.x;
for (int j = edg[i].p1.y + 1; j <= edg[i].p2.y; j++) {
wall[x][j][1]--;
wall[x + 1][j][3]--;
}
}
}
return vis[dest.x][dest.y];
}
int main() {
#ifdef TanJI
clock_t c1 = clock();
freopen("D:\\Cpp\\1.in", "r", stdin);
freopen("D:\\Cpp\\1.out", "w", stdout);
#endif
//--------------------------------------------
scanf("%d", &T);
while (T--) {
scanf("%d%d%d", &n, &m, &K);
st.input(); st.x++, st.y++;
dest.input(); dest.x++, dest.y++;
for (int i = 1; i <= K; i++) {
edg[i].p1.input();
edg[i].p2.input();
if (edg[i].p1.x == edg[i].p2.x) edg[i].h = false;
else edg[i].h = true;
if (edg[i].p1.x >= edg[i].p2.x) swap(edg[i].p1.x, edg[i].p2.x);
if (edg[i].p1.y >= edg[i].p2.y) swap(edg[i].p1.y, edg[i].p2.y);
}
for (int i = 1; i <= n; i++)
wall[i][1][2] = wall[i][m][0] = 1;
for (int i = 1; i <= m; i++)
wall[1][i][3] = wall[n][i][1] = 1;
int MIN = K;
for (int i = 0; i < (1 << K) - 1; i++) {
if (dfs(i)) {
int cnt = 0;
for (int j = 0; j < K; j++)
if ((i >> j) & 1) cnt++;
MIN = min(cnt, MIN);
}
}
printf("%d\n", MIN);
for (int i = 1; i <= n; i++)
wall[i][1][2] = wall[i][m][0] = 0;
for (int i = 1; i <= m; i++)
wall[1][i][3] = wall[n][i][1] = 0;
}
//--------------------------------------------
#ifdef TanJI
cerr << "Time Used:" << clock() - c1 << "ms" << endl;
#endif
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 250ms
memory: 3856kb
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