Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
dfs过,构造有向图时粗心wa了两发~~#include<iostream> #include<algorithm> #include<cstdio> #include<cstring> #include<cmath> #include<queue> #include<map> using namespace std; #define PI acos(-1.0) #define INF 0x3f3f3f3f #define ll long long #define lowbit(i) i&(-i) #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 const int maxn=16; const int mod=1e9+7; const double eps=1e-6; int n,ans; bool vis[maxn]; typedef struct node{ int x1,y1,x2,y2; int color; }Node; Node temp[maxn]; bool graph[maxn][maxn]; bool IsOk(int x){ for(int i=0;i<n;i++){ if(i==x) continue; if(graph[x][i]){ if(vis[i]) continue; else return false; } } return true; } void dfs(int cur,int step,int col){ if(cur==n){ ans=min(ans,step); return ; } for(int i=0;i<n;i++){ if(!vis[i]&&(temp[i].x1==0||IsOk(i))){ vis[i]=true; if(temp[i].color==col) dfs(cur+1,step,temp[i].color); else dfs(cur+1,step+1,temp[i].color); vis[i]=false; } } } void build_graph(){ memset(graph,false,sizeof(graph)); for(int i=0;i<n;i++) for(int j=0;j<n;j++){ if(i==j) continue; if(temp[j].x2<=temp[i].x1&&((temp[j].y1>=temp[i].y1&&temp[j].y1<=temp[i].y2)||(temp[j].y1<=temp[i].y1&&temp[j].y2>=temp[i].y2)||(temp[j].y2>=temp[i].y1&&temp[j].y2<=temp[i].y2))) graph[i][j]=true; } } int main() { int T; scanf("%d",&T); while(T--){ scanf("%d",&n); ans=INF; memset(vis,false,sizeof(vis)); for(int i=0;i<n;i++) scanf("%d%d%d%d%d",&temp[i].x1,&temp[i].y1,&temp[i].x2,&temp[i].y2,&temp[i].color); build_graph(); dfs(0,0,0); cout<<ans<<endl; } return 0 ; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator