题意:在网格里面给定了 横,竖 两种多米诺骨牌,同向的不可以覆盖,不同向的可以覆盖,问你最多去掉多少个有覆盖的多米诺,使得网格内剩余的多米诺骨牌最多
解题思路:
一.搜索
(1),分别对横竖两种不同的多米诺建图(同一块标记),然后根据某个封闭覆盖区域的交叉搜索可得到横竖的走向次数,贪心最大可得。
(2).直接建图, 有向深搜,然后面积/2
二,二分匹配,
解题代码:(第一种)
1 #include <stdio.h> 2 #include <string.h> 3 #include <string.h> 4 #include <stdlib.h> 5 int map[200][200]; 6 int map1[200][200]; 7 int visit[200][200]; 8 int visit1[200][200]; 9 10 int suma ,sumb; 11 12 int dfs(int i , int j , int sit) 13 { 14 // printf("%d %d %d\n",i,j,sit); 15 if(sit == 0 ) 16 { 17 if(visit1[i][j] == 0 && map1[i][j] != 0 ) 18 { 19 visit1[i][j] = 1 ; 20 dfs(i,j,1); 21 } 22 if(visit[i+1][j] == 0 && map[i][j] == map[i+1][j]) 23 { 24 visit[i+1][j] = 1; 25 sumb += 1 ; 26 dfs(i+1,j,0); 27 28 } 29 else if(i > 0 && visit[i-1][j] == 0 &&map[i][j] == map[i-1][j]) 30 { 31 visit[i-1][j] = 1; 32 sumb += 1 ; 33 dfs(i-1,j,0); 34 35 } 36 } 37 else 38 { 39 if(visit[i][j] == 0 && map[i][j] != 0 ) 40 { 41 visit[i][j] = 1 ; 42 dfs(i,j,0); 43 } 44 if(visit1[i][j+1] == 0 && map1[i][j] == map1[i][j+1]) 45 { 46 visit1[i][j+1] = 1; 47 suma += 1 ; 48 dfs(i,j+1,1); 49 50 } 51 else if(j > 0 && visit1[i][j-1] == 0 && map1[i][j] == map1[i][j-1] ) 52 { 53 visit1[i][j-1] = 1; 54 dfs(i,j-1,1); 55 suma += 1 ; 56 57 } 58 59 } 60 } 61 62 int main() 63 { 64 int n , m ; 65 while(scanf("%d %d",&n,&m) != EOF) 66 { 67 int sum = 0 ; 68 if(n == 0 && m == 0) 69 break; 70 memset(map,0,sizeof(map)); 71 memset(visit,0,sizeof(visit)); 72 memset(map1,0,sizeof(map1)); 73 memset(visit1,0,sizeof(visit1)); 74 int a, b ; 75 for(int i =1 ;i<= n ;i ++) 76 { 77 scanf("%d %d",&a,&b); 78 map[a][b] = i ; 79 map[a+1][b] = i ; 80 } 81 for(int i = 1;i <= m;i ++) 82 { 83 scanf("%d %d",&a,&b); 84 map1[a][b] = i ; 85 map1[a][b+1] = i ; 86 } 87 for(int i = 0 ;i <= 102; i ++) 88 for(int j = 0; j <= 102; j ++) 89 { 90 if((visit[i][j] == 0 &&map[i][j]!= 0 ) || ( visit1[i][j] == 0 && map1[i][j] != 0 )) 91 { suma = 0 ; 92 sumb = 0 ; 93 94 if(visit[i][j] == 0 &&map[i][j]!= 0 ) 95 { 96 visit[i][j] = 1; 97 dfs(i , j, 0); 98 99 } 100 else if( visit1[i][j] == 0 && map1[i][j] != 0 ) 101 { 102 visit1[i][j] = 1; 103 dfs(i , j, 1); 104 105 } 106 // printf("%d %d\n",suma,sumb); 107 if(suma > sumb) 108 sum += suma ; 109 else 110 sum += sumb ; 111 } 112 } 113 printf("%d\n",sum); 114 } 115 return 0 ; 116 }