原题go

暴搜就行了 鬼知道我怎么往状压想的

时间复杂度 O(2n×n2)O(2 ^ n \times n^2),实际达不到n2n ^ 2

#include<bits/stdc++.h>
using namespace std;
const int N = 29;
int n ;
map<char,int>ma;
struct node{
    int x,y;
}no[N];
bool vis[N];

bool check(int x,int y,int fx,int fy,int dir){
    bool flag = true;//中间无阻碍 
    for(int i = 1;i<=n;i++){
        if(vis[i])continue;
        int xx = no[i].x,yy = no[i].y;
        if(xx == fx && yy == fy)continue;
        if(dir == 0)flag &= (!(xx == x && yy > y && yy < fy));//u
        if(dir == 1)flag &= (!(yy == y && xx > x && xx < fx));//r
        if(dir == 2)flag &= (!(xx == x && yy < y && yy > fy));//d
        if(dir == 3)flag &= (!(yy == y && xx < x && xx > fx));//l
    }
    if(dir == 0)flag &= (x == fx && fy > y);//u
    if(dir == 1)flag &= (fy == y && fx > x);//r
    if(dir == 2)flag &= (x == fx && fy < y);//d
    if(dir == 3)flag &= (fy == y && fx < x);//r
    return flag;
}
int ans = 0;
void dfs(int x,int y,int dir,int cnt){
    if(cnt > ans)ans = cnt;
    for(int i = 1;i<=20;i++){
        if(vis[i])continue;
        int fx = no[i].x,fy = no[i].y; 
        if(check(x,y,fx,fy,dir)){
            vis[i] = true;
            dfs(fx,fy,(dir + 1) % 4,cnt + 1);
            dfs(fx,fy,(dir + 3) % 4,cnt + 1);
            vis[i] = false;
        }
    }
}
int main(){
    ma['U'] = 0;
    ma['R'] = 1;
    ma['D'] = 2;
    ma['L'] = 3;
    cin>>n;
    int ax,ay;cin>>ax>>ay;
    char d;cin>>d;
    for(int i = 1;i<=n;i++){
        int x,y;cin>>x>>y;
        no[i] = {x,y};
    }
    dfs(ax,ay,ma[d],0);
    cout<<ans<<'\n';
    return 0;
}

0 条评论

目前还没有评论...