topic website: http://poj.org/problem?id=3020
Test instructions
The ellipse is used to cover all the rings (that is, The small dots on the graph), there are two types of oval, left and right facing and up and down, an ellipse can cover up to two adjacent small dots.
Ideas:
Consider each small dot as a vertex, because an ellipse can cover only two small dots, and we can think of the graph as a binary graph. The adjacent two points, one as the vertices within the X collection, and the other as vertices within the Y Collection. however, It is important to note that a vertex may be more than a vertex to connect to (as in the case), so we should consider the above situation as an image without a graph. The maximum number of matches to be calculated by the Non-direction graph is divided by two.
This allows us to convert the problem to the minimum side overlay: all vertices are overwritten with the smallest edge. The value is equal to the number of vertices-the maximum number of Matches. As for the maximum matching number of the binary graph, the Hungarian algorithm can be used Directly.
Code:
1#include <cstdio>2#include <cstring>3#include <vector>4 using namespacestd;5 intmatch[ the];6 structnode{7 intx, y;8}dir[4]={{1,0},{-1,0},{0,1},{0,-1}};9Vector<node>v;Ten intn,m,cnt; one Charmatrix[ -][ -]; a intmp[ -][ -]; - intbook[ the]; - inte[ the][ the]; the voiddeal () { - for(intx=0; x<n; X + +) { - for(inty=0; y<m; y++) { - if(matrix[x][y]!='*')Continue; + intI=mp[x][y]; - for(intD=0, xx,yy; d<4; d++) { +xx=x+dir[d].x; ayy=y+dir[d].y; at if(xx<0|| X>=n | | yy<0|| Yy>=m | | matrix[xx][yy]!='*')Continue; - intj=mp[xx][yy]; -e[i][j]=1; - - } - } in } - } to intDfsintU) { + for(intI=1; i<=cnt;i++){ - if(!book[i] &&e[u][i]) { thebook[i]=1; * if(match[i]==0||DFS (match[i])) { $match[i]=u;Panax Notoginseng return 1; - } the } + } a return 0; the } + intmain () { - intt; $scanf"%d",&t); $ while(t--) { - intsum=0; -Cnt=0; the v.clear (); -Memset (match,0,sizeof(match));WuyiMemset (mp,0,sizeof(mp)); theMemset (e,0,sizeof(e)); -scanf"%d%d",&n,&m); wu for(intI=0; i<n; i++) { - gets (matrix[i]); about for(intj=0; j<m; J + +) { $ if(matrix[i][j]=='*'){ -mp[i][j]=++cnt; - } - } a } + deal (); the for(intI=1; i<=cnt; i++) { -Memset (book,0,sizeof(book)); $ if(dfs (I)) sum++; the } theprintf"%d\n", cnt-sum/2); the } the return 0; -}
POJ 3020 Antenna Placement (binary map of Hungary Algorithm)