当前位置: 代码迷 >> 综合 >> poj-1691-暴力DFS
  详细解决方案

poj-1691-暴力DFS

热度:31   发布时间:2023-12-19 11:11:49.0

感觉智商变低了。。。暴力的DFS都不会做了

#include<stdio.h>
#include<iostream>
#include<vector>
#include<string.h>
using namespace std;
struct list
{int x1,y1;int x2,y2;int c;
}node[21];
struct list2
{int ipos;int col;
}po[101],p;
int ru[21];
int maps[21][21];
int vis[21];
int n;
int maxx;
int cmp(struct list2 a,struct list2 b)
{return a.col<b.col;
}
void init()
{int i;maxx=99999;memset(ru,0,sizeof(ru));memset(maps,0,sizeof(maps));memset(vis,0,sizeof(vis));scanf("%d",&n);for(i=0;i<n;i++){scanf("%d%d%d%d%d",&node[i].x1,&node[i].y1,&node[i].x2,&node[i].y2,&node[i].c);}
}
int pan(int x,int y)
{if(node[x].x2!=node[y].x1)return 0;if(node[x].y1>node[y].y1&&node[x].y1<node[y].y2)return 1;if(node[x].y2>node[y].y1&&node[x].y2<node[y].y2)return 1;if(node[x].y1<=node[y].y1&&node[x].y2>=node[y].y2)return 1;return 0;
}
void creat()
{int i,j;for(i=0;i<n;i++){for(j=0;j<n;j++){if(i==j)continue;if(pan(j,i)){ru[i]++;maps[j][i]=1;//  printf("%d->%d\n",j+1,i+1);}}}
}
void dfs(int pre,int x)
{// cout<<x<<" "<<pre<<endl;int i,j;vector<int>co[21];int chui[21];int visi[21];memset(chui,0,sizeof(chui));memset(visi,0,sizeof(visi));for(i=0;i<n;i++){if(vis[i]==0&&ru[i]==0&&node[i].c==pre){vis[i]=1;visi[i]++;for(j=0;j<n;j++){if(maps[i][j]==1){ru[j]--;chui[j]++;}}}}for(i=0;i<n;i++){if(vis[i]==0&&ru[i]==0){co[node[i].c].push_back(i);}}int leap=0;for(i=0;i<21;i++){int len=co[i].size();if(len==0)continue;leap=1;if(i==pre){dfs(i,x);}else dfs(i,x+1);}if(leap==0)maxx=min(maxx,x);for(i=0;i<n;i++){vis[i]=vis[i]-visi[i];ru[i]=ru[i]+chui[i];}
}
int main()
{int t;cin>>t;while(t--){init();creat();dfs(-1,0);cout<<maxx<<endl;}return 0;
}