QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#41329#4376. Dragon slayer01shijian#AC ✓189ms3872kbC++2.2kb2022-07-29 17:09:002022-07-29 17:09:01

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2022-07-29 17:09:01]
  • 评测
  • 测评结果:AC
  • 用时:189ms
  • 内存:3872kb
  • [2022-07-29 17:09:00]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
typedef pair<int,int> PII;
// typedef pari<int,PII> PIII;
const int N = 20;

bool st[N][N],g[N<<1][N<<1];
int dx[4] = {1,-1,0,0},dy[4] = {0,0,1,-1};
int n,m,K,xs,ys,xt,yt;

struct Node
{
    int x1,y1,x2,y2;
}wall[N];

bool bfs()
{
    queue<PII> q;
    memset(st,0,sizeof st);
    st[xs][ys] = 1;
    q.push({xs,ys});
    while(q.size())
    {
        auto t = q.front();
        q.pop();
        if(t.first==xt&&t.second==yt) return 1;
        for(int i=0;i<4;i++)
        {
            int nx = t.first + dx[i],ny = t.second + dy[i];
            int w = 0;
            if(nx>n||nx<=0||ny>m||ny<=0) continue;
            if(st[nx][ny]) continue;
            int tx,ty;
            if(!dx[i]) tx = 2*nx - 1,ty = min(t.second,ny);
            else tx = min(t.first,nx)*2,ty = ny;
            if(g[tx][ty]) continue;
            st[nx][ny] = 1;
            q.push({nx,ny});
        }
    }
    return 0;
}

int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d%d",&n,&m,&K);
        scanf("%d%d%d%d",&xs,&ys,&xt,&yt);
        xs++,ys++,xt++,yt++;
        memset(g,0,sizeof g);
        for(int i=1;i<=K;i++)
        {
            int x1,y1,x2,y2;scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
            wall[i] = {x1,y1,x2,y2};
        }
        int ans = INT_MAX;
        for(int i=0;i<1<<K;i++)
        {
            memset(g,0,sizeof g);
            int cnt = 0;
            for(int j=0;j<K;j++)
                if(i>>j&1)
                {
                    cnt++;
                    int x1 = wall[j+1].x1,x2 = wall[j+1].x2,y1 = wall[j+1].y1,y2 = wall[j+1].y2;
                    if(x1==x2){
                        if(y1>=y2) swap(y1,y2);
                        for(int i=y1+1;i<=y2;i++)
                            g[2*x1][i] = 1;
                    }
                    else{
                        if(x1>=x2) swap(x1,x2);
                        for(int i=x1;i<=x2-1;i++)
                            g[2*i+1][y1] = 1;
                    }
                }
            if(bfs()) ans = min(K-cnt,ans);
        }
        printf("%d\n",ans);
    }
    return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 189ms
memory: 3872kb

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