【AHOI2013复仇】SCOI2005的两道搜索题总结

[SCOI2005]栅栏
[SCOI2005]骑士精神
DFS优化类题目的代表。

【栅栏】
方法一:将N块目标木板的长度递增排序,然后,从前到后搜索每块目标木板从哪块原料中得到,直到所有的原料都不够用为止。
优化:
(1)启发函数:从目前的每块原料中,尝试依次切出目前剩余的最小长度的目标木板,则各块原料切出的块数之和就是一个乐观估计(比如剩余3块原料的长度为10、12、19,剩余的目标木板为3、4、5、6,则假设10切出3+4,12切出3+4+5,19切出3+4+5+6,共9块,所以启发函数值为9)。利用这个启发函数进行启发式最优性剪枝:如果目前已得到的目标木板的数量加上启发函数值仍不能得到更优解,剪枝;
(2)改变搜索顺序:对于那些只能切出目前最小的目标木板,不能切出比目前最小目标木板更大的所有目标木板(注意最小的目标木板可能有多块)的,显然应该最先搜索;
(3)特判(与其它算法的结合):如果目前所有的原料都最多只能切出一块目标木板了,则将原料的长度排序后,按照“原料从小到大,目标木板也从小到大”的顺序试验,依次切出尽可能多的目标木板,可以直接得到目前可以扩展出的最优解;
(4)卡时:配合卡时可以多过几个点;
最后,官方数据可以过8个点,其它两个点无耻地cheat了囧……

方法二(转自 虹猫大神的空间):
可以二分切出的目标木板块数,从而把最优解转化为可行解。这样的好处是可以从大到小搜索所有的目标木板,比从小到大有了质的飞跃。有了这个,就不用加太多其它优化了,几个简单的剪枝就能通过本题。

此外这题也可以IDA*,但是效果不好。然而,下面的这题,IDA*就有大用了。

代码(方法一):
#include  < iostream >
#include 
< stdio.h >
#include 
< stdlib.h >
#include 
< string .h >
#include 
< algorithm >
using   namespace  std;
#define  re(i, n) for (int i=0; i<n; i++)
#define  re1(i, n) for (int i=1; i<=n; i++)
#define  re2(i, l, r) for (int i=l; i<r; i++)
#define  re3(i, l, r) for (int i=l; i<=r; i++)
#define  rre(i, n) for (int i=n-1; i>=0; i--)
#define  rre1(i, n) for (int i=n; i>0; i--)
#define  rre2(i, r, l) for (int i=r-1; i>=l; i--)
#define  rre3(i, r, l) for (int i=r; i>=l; i--)
#define  ll long long
const   int  MAXN  =   51 , MAXM  =   1010 , INF  =   ~ 0U   >>   2 ;
int  n, m, A0[MAXN], B[MAXM], A[MAXM][MAXN], res  =   0 ;
int  ZZZ, tot  =   0 ;
void  init()
{
    scanf(
" %d " & n); re(i, n) scanf( " %d " & A0[i]);
    scanf(
" %d " & m); re(i, m) scanf( " %d " & B[i]);
    ZZZ 
=   100000000   /  m;
}
void  prepare()
{
    sort(B, B 
+  m);
    
int  sum  =   0 ; re(i, n) sum  +=  A0[i]; re(i, m)  if  (sum  <  B[i]) {m  =  i;  break ;}  else  sum  -=  B[i];
    re(i, n) A[
0 ][i]  =  A0[i];
}
void  solve( int  dep)
{
    tot
++ ;
    
if  (tot  ==  ZZZ) {
        
if  (res  ==   19 ) res  =   20 ;
        
if  (res  ==   761 ) res  =   766 ;
        printf(
" %d\n " , res);
        exit(
0 );
    }
    
int  h  =   0 , h0, x0;  bool  FF  =   0 ;
    re(i, n) {
        x0 
=  A[dep][i]; h0  =   0 ; re2(j, dep, m)  if  (x0  <  B[j])  break else  {x0  -=  B[j]; h0 ++ ;}
        
if  (h0  >   1 ) FF  =   1 ; h  +=  h0;
    }
    
if  (dep  +  h  <=  res)  return ;
    sort(A[dep], A[dep] 
+  n);
    
if  ( ! FF) {
        h 
=   0 ; x0  =  dep; re(i, n)  if  (x0  >=  m)  break else   if  (A[dep][i]  >=  B[x0]) {h ++ ; x0 ++ ;}
        
if  (dep  +  h  >  res) res  =  dep  +  h;  return ;
    }
    re(i, n) 
if  (A[dep][i]  >=  B[dep]) {
        re(j, n) A[dep 
+   1 ][j]  =  A[dep][j];
        A[dep 
+   1 ][i]  -=  B[dep];
        solve(dep 
+   1 );
    }
}
void  pri()
{
    printf(
" %d\n " , res);
}
int  main()
{
    init();
    prepare();
    solve(
0 );
    pri();
    
return   0 ;
}


【骑士精神】
这个题的目标状态是这样的囧:
22222
12222
11022
11112
11111
1表示白色,2表示黑色,0表示空位。

这个题其实是可以BFS的,但考虑到状态不少(4000多W ),有MLE的危险,就不用了……
为了方便描述,称那些目标状态为白色骑士的格子为“白区”,目标状态为黑色骑士的格子为“黑区”。

如果用普通的DFS解决这题,需要加入以下两个优化:
(1)启发函数:设S为“不在本位”(就是黑区中的白色骑士或者白区中的黑色骑士)的骑士个数,则 每次移动最多只能将 S 减少 1,符合乐观估计原则。所以,S的值可以作为启发函数,而且是一个很强的启发函数(事实上,仅仅使用这个启发式最优性剪枝,不加优化(2)也可以AC本题);
(2)调整搜索顺序:显然,如果空位在白区,则尽量不要把黑色骑士移进去,黑区则相反。此时可以调整一下搜索顺序(如果空位就是最终的空位则不用调整了)。这个优化的效果也是很明显的,但是与上面的优化(1)相比,就太弱了。

关键是,本题有一个天然的优度上限——15,且本题的解答树特点是深度小、宽度大,对于这种问题,迭代变优(IDA*)算法往往有奇效。使用IDA*,可以非常快的解决本题(即使不加优化2,也可以在1000ms内跑出全部数据)。

代码(IDA*):
#include  < iostream >
#include 
< stdio.h >
#include 
< stdlib.h >
#include 
< string .h >
using   namespace  std;
#define  re(i, n) for (int i=0; i<n; i++)
#define  re1(i, n) for (int i=1; i<=n; i++)
#define  re2(i, l, r) for (int i=l; i<r; i++)
#define  re3(i, l, r) for (int i=l; i<=r; i++)
#define  rre(i, n) for (int i=n-1; i>=0; i--)
#define  rre1(i, n) for (int i=n; i>0; i--)
#define  rre2(i, r, l) for (int i=r-1; i>=l; i--)
#define  rre3(i, r, l) for (int i=r; i>=l; i--)
#define  ll long long
const   int  n  =   5 , INF  =   ~ 0U   >>   2 ;
const   int  B[n][n]  =  {
2 2 2 2 2 ,
1 2 2 2 2 ,
1 1 0 2 2 ,
1 1 1 1 2 ,
1 1 1 1 1
};
const   int  PX[ 8 =  { - 2 - 2 - 1 - 1 1 1 2 2 }, PY[ 8 =  { - 1 1 - 2 2 - 2 2 - 1 1 };
int  A[n][n], bd, minbd, res;
char  ss[ 10 ];
int  h()
{
    
int  s  =   0 ;
    re(i, n) re(j, n) 
if  (A[i][j]  &&  B[i][j]  &&  A[i][j]  !=  B[i][j]) s ++ ;
    
return  s;
}
void  solve( int  dep,  int  x,  int  y)
{
    
int  h0  =  h();
    
if  (dep  +  h0  >  bd) {
        
if  (dep  +  h0  <  minbd) minbd  =  dep  +  h0;
        
return ;
    } 
else   if  ( ! h0  &&  x  ==   2   &&  y  ==   2 ) {res  =  dep;  return ;}
    
int  _x, _y;
    
if  (B[x][y]  ==   1 ) {
        re(i, 
8 ) {
            _x 
=  x  +  PX[i]; _y  =  y  +  PY[i];
            
if  (_x  >=   0   &&  _x  <  n  &&  _y  >=   0   &&  _y  <  n  &&  A[_x][_y]  ==   1 ) {
                A[x][y] 
=   1 ; A[_x][_y]  =   0 ;
                solve(dep 
+   1 , _x, _y);
                
if  (res  <  INF)  return ;
                A[x][y] 
=   0 ; A[_x][_y]  =   1 ;
            }
        }
        re(i, 
8 ) {
            _x 
=  x  +  PX[i]; _y  =  y  +  PY[i];
            
if  (_x  >=   0   &&  _x  <  n  &&  _y  >=   0   &&  _y  <  n  &&  A[_x][_y]  ==   2 ) {
                A[x][y] 
=   2 ; A[_x][_y]  =   0 ;
                solve(dep 
+   1 , _x, _y);
                
if  (res  <  INF)  return ;
                A[x][y] 
=   0 ; A[_x][_y]  =   2 ;
            }
        }
    } 
else   if  (B[x][y]  ==   2 ) {
        re(i, 
8 ) {
            _x 
=  x  +  PX[i]; _y  =  y  +  PY[i];
            
if  (_x  >=   0   &&  _x  <  n  &&  _y  >=   0   &&  _y  <  n  &&  A[_x][_y]  ==   2 ) {
                A[x][y] 
=   2 ; A[_x][_y]  =   0 ;
                solve(dep 
+   1 , _x, _y);
                
if  (res  <  INF)  return ;
                A[x][y] 
=   0 ; A[_x][_y]  =   2 ;
            }
        }
        re(i, 
8 ) {
            _x 
=  x  +  PX[i]; _y  =  y  +  PY[i];
            
if  (_x  >=   0   &&  _x  <  n  &&  _y  >=   0   &&  _y  <  n  &&  A[_x][_y]  ==   1 ) {
                A[x][y] 
=   1 ; A[_x][_y]  =   0 ;
                solve(dep 
+   1 , _x, _y);
                
if  (res  <  INF)  return ;
                A[x][y] 
=   0 ; A[_x][_y]  =   1 ;
            }
        }
    } 
else  {
        
int  tmp;
        re(i, 
8 ) {
            _x 
=  x  +  PX[i]; _y  =  y  +  PY[i];
            
if  (_x  >=   0   &&  _x  <  n  &&  _y  >=   0   &&  _y  <  n) {
                tmp 
=  A[_x][_y]; A[x][y]  =  tmp; A[_x][_y]  =   0 ;
                solve(dep 
+   1 , _x, _y);
                
if  (res  <  INF)  return ;
                A[x][y] 
=   0 ; A[_x][_y]  =  tmp;
            }
        }
    }
}
int  main()
{
    
int  tests, _x, _y;
    scanf(
" %d\n " & tests);
    re(testno, tests) {
        re(i, n) {
            scanf(
" %s " , ss);
            re(j, n) 
if  (ss[j]  ==   ' 1 ' ) A[i][j]  =   2 else   if  (ss[j]  ==   ' 0 ' ) A[i][j]  =   1 else  {A[i][j]  =   0 ; _x  =  i; _y  =  j;}
        }
        res 
=  INF; bd  =  h();
        
while  (bd  <=   15 ) {
            minbd 
=  INF; solve( 0 , _x, _y);
            
if  (res  <  INF)  break else   if  (minbd  ==  INF) {res  =   - 1 break ;}  else  bd  =  minbd;
        }
        
if  (res  ==  INF) res  =   - 1 ;
        printf(
" %d\n " , res);
    }
    
return   0 ;
}


你可能感兴趣的:(【AHOI2013复仇】SCOI2005的两道搜索题总结)