POJ2222+暴力搜索
一共2^15个状态
比较简单
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 /* 2 2^15 states 3 */ 4 #include<stdio.h> 5 #include<string.h> 6 #include<stdlib.h> 7 #include<math.h> 8 #include<algorithm> 9 using namespace std; 10 11 const int maxn = 10005; 12 const int inf = 99999999; 13 const int dx[]={0,0,1,-1,1,-1,1,-1}; 14 const int dy[]={1,-1,0,0,-1,1,1,-1}; 15 char mat[ 12 ][ 12 ]; 16 struct Node{ 17 int x,y; 18 char ch; 19 bool flag; 20 }node[ 24 ]; 21 int vis[ 24 ]; 22 23 bool in( int x,int y,int n,int m ){ 24 if( x>=0&&x<n&&y>=0&&y<m ) return true; 25 else return false; 26 } 27 28 bool ok_k( int x,int y,int n,int m ){ 29 for( int i=0;i<8;i++ ){ 30 int tx = x+dx[i]; 31 int ty = y+dy[i]; 32 if( in(tx,ty,n,m)==true ) { 33 if( mat[tx][ty]!='E' ) 34 return false; 35 } 36 } 37 return true; 38 } 39 bool ok_q( int x,int y,int n,int m ){ 40 for( int i=1;i<=12;i++ ){ 41 for( int k=0;k<8;k++ ){ 42 int tx = x+i*dx[k]; 43 int ty = y+i*dy[k]; 44 if( in(tx,ty,n,m)==true ){ 45 if( mat[tx][ty]!='E' ) 46 return false; 47 } 48 } 49 } 50 return true; 51 } 52 bool ok_b( int x,int y,int n,int m ){ 53 for( int i=1;i<=12;i++ ){ 54 for( int k=4;k<8;k++ ){ 55 int tx = x+i*dx[k]; 56 int ty = y+i*dy[k]; 57 if( in(tx,ty,n,m)==true ){ 58 if( mat[tx][ty]!='E' ) 59 return false; 60 } 61 } 62 } 63 return true; 64 } 65 bool ok_r( int x,int y,int n,int m ){ 66 for( int i=1;i<=10;i++ ){ 67 for( int k=0;k<4;k++ ){ 68 int tx = x+i*dx[k]; 69 int ty = y+i*dy[k]; 70 if( in(tx,ty,n,m)==true ){ 71 if( mat[tx][ty]!='E' ) 72 return false; 73 } 74 } 75 } 76 return true; 77 } 78 bool ok_n( int x,int y,int cnt_node ){ 79 for( int i=0;i<cnt_node;i++ ){ 80 if( node[i].x==x&&node[i].y==y ) continue; 81 if( node[i].flag==false ) continue; 82 int ddx = abs( x-node[i].x ); 83 int ddy = abs( y-node[i].y ); 84 if( (ddx==2&&ddy==1)||(ddx==1&&ddy==2) ) 85 return false; 86 } 87 return true; 88 } 89 90 void re( int cnt_node ){ 91 for( int i=0;i<cnt_node;i++ ){ 92 if( vis[ i ]==1 ){ 93 mat[ node[i].x ][ node[i].y ] = node[i].ch; 94 node[ i ].flag = true; 95 } 96 } 97 } 98 99 100 bool judge( int cnt_node,int n,int m ){ 101 for( int i=0;i<cnt_node;i++ ){ 102 if( vis[ i ]==1 ){ 103 mat[ node[i].x ][ node[i].y ] = 'E'; 104 node[ i ].flag = false; 105 } 106 } 107 bool flag = true; 108 for( int i=0;i<n;i++ ){ 109 for( int j=0;j<m;j++ ){ 110 if( mat[i][j]=='E' ) 111 continue; 112 if( mat[i][j]=='N'&&ok_n( i,j,cnt_node )==false ){ 113 flag = false; 114 break; 115 } 116 else if( mat[i][j]=='R'&&ok_r( i,j,n,m )==false ){ 117 flag = false; 118 break; 119 } 120 else if( mat[i][j]=='B'&&ok_b( i,j,n,m )==false ){ 121 flag = false; 122 break; 123 } 124 else if( mat[i][j]=='Q'&&ok_q( i,j,n,m )==false ){ 125 flag = false; 126 break; 127 } 128 else if( mat[i][j]=='K'&&ok_k( i,j,n,m )==false ){ 129 flag = false; 130 break; 131 } 132 } 133 if( flag==false ) break; 134 } 135 re( cnt_node ); 136 return flag; 137 } 138 139 int main(){ 140 char s[ 12 ]; 141 while( scanf("%s",&s)!=EOF ){ 142 int n,m; 143 int cnt_node = 0; 144 scanf("%d%d",&m,&n); 145 for( int i=0;i<n;i++ ){ 146 for( int j=0;j<m;j++ ){ 147 scanf("%s",s); 148 mat[i][j] = s[0]; 149 if( s[0]!='E' ){ 150 node[ cnt_node ].x = i; 151 node[ cnt_node ].y = j; 152 node[ cnt_node ].flag = true; 153 node[ cnt_node++ ].ch = s[0]; 154 } 155 } 156 } 157 scanf("%s",s); 158 int N = (1<<cnt_node); 159 //printf("cnt_node=%d\n",cnt_node); 160 int ans = inf; 161 for( int i=0;i<N;i++ ){ 162 int temp_ans = 0; 163 for( int j=0;j<cnt_node;j++ ){ 164 if( i&(1<<j) ) {vis[ j ] = 1,temp_ans++;} 165 else vis[ j ] = 0; 166 } 167 if( judge( cnt_node,n,m )==true ){ 168 ans = min( ans,temp_ans ); 169 } 170 } 171 printf("Minimum Number of Pieces to be removed: %d\n",ans); 172 } 173 return 0; 174 }
keep moving...