zoukankan      html  css  js  c++  java
  • 【bzoj1085】【SCOI2005】【骑士精神】

    IDA*板子……

      1 #include<map>
      2 #include<queue>
      3 #include<cstdio>
      4 #include<cstring>
      5 #include<algorithm>
      6 using namespace std;
      7 int T,n,ar;
      8 int dx[8]={1,1,2,2,-1,-1,-2,-2};
      9 int dy[8]={2,-2,1,-1,2,-2,1,-1};
     10 map<int,int>vis;
     11 struct node
     12 {
     13     int mp[7][7];
     14     int f;
     15     int g;
     16     int h;
     17     int xx;
     18     int yy;
     19     bool friend operator < (node x,node y)
     20     {
     21         return x.f!=y.f?x.f>y.f:x.g<y.g;
     22     }
     23 }sta,aim;
     24 int sol(node x)
     25 {
     26     int ret=0;
     27     for(int i=1;i<=5;i++)
     28     {
     29         for(int j=1;j<=5;j++)
     30         {
     31             if(x.mp[i][j]!=2)
     32             {
     33                 ret=ret*2+x.mp[i][j];
     34             }
     35         }
     36     }
     37     ret=ret*5+x.xx-1;
     38     ret=ret*5+x.yy-1;
     39     return ret;
     40 }
     41 int work(node x)
     42 {
     43     int ret=0;
     44     for(int i=1;i<=5;i++)
     45     {
     46         for(int j=1;j<=5;j++)
     47         {
     48             if(x.mp[i][j]!=aim.mp[i][j])
     49             {
     50                 ret++;
     51             }
     52         }
     53     }
     54     return ret-1;
     55 }
     56 int bfs()
     57 {
     58     priority_queue<node>q;
     59     vis.clear();
     60     q.push(sta);
     61     while(!q.empty())
     62     {
     63         node tmp=q.top();q.pop();
     64         int now=sol(tmp);
     65         if(vis[now])
     66         {
     67             continue;
     68         }
     69         vis[now]=1;
     70         node nxt;
     71         for(int i=0;i<8;i++)
     72         {
     73             nxt=tmp;
     74             nxt.xx+=dx[i];
     75             nxt.yy+=dy[i];
     76             if(nxt.xx<1||nxt.xx>5||nxt.yy<1||nxt.yy>5)
     77             {
     78                 continue;
     79             }
     80             nxt.mp[tmp.xx][tmp.yy]=tmp.mp[nxt.xx][nxt.yy];
     81             nxt.mp[nxt.xx][nxt.yy]=2;
     82             nxt.g++,nxt.h=work(nxt),nxt.f=nxt.g+nxt.h;
     83             if(nxt.f<=n)
     84             {
     85                 int jr=sol(nxt);
     86                 if(!vis[jr])
     87                 {
     88                     q.push(nxt);
     89                     if(jr==ar)
     90                     {
     91                         return 1;
     92                     }
     93                 }
     94             }
     95         }
     96     }
     97     return 0;
     98 }
     99 int main()
    100 {
    101     scanf("%d",&T);
    102     for(int i=1;i<=5;i++)
    103     {
    104         aim.mp[1][i]=1,aim.mp[2][i]=1;
    105     }
    106     aim.mp[2][1]=0,aim.mp[3][4]=1,aim.mp[3][5]=1,aim.mp[4][5]=1,aim.mp[3][3]=2;
    107     aim.xx=3,aim.yy=3;
    108     ar=sol(aim);
    109     while(T--)
    110     {
    111         for(int i=1;i<=5;i++)
    112         {
    113             char ch[7];
    114             scanf("%s",ch+1);
    115             for(int j=1;j<=5;j++)
    116             {
    117                 if(ch[j]=='*')
    118                 {
    119                     ch[j]='2';
    120                     sta.xx=i,sta.yy=j;
    121                 }
    122                 sta.mp[i][j]=ch[j]-'0';
    123             }
    124         }
    125         if(sol(sta)==ar)
    126         {
    127             puts("0");
    128             continue;
    129         }
    130         sta.g=0,sta.h=work(sta),sta.f=sta.g+sta.h;
    131         int flag=0;
    132         for(int i=1;i<=15;i++)
    133         {
    134             n=i;
    135             if(bfs())
    136             {
    137                 printf("%d
    ",i);
    138                 flag=1;
    139                 break;
    140             }
    141         }
    142         if(!flag)
    143         {
    144             puts("-1");
    145         }
    146     }
    147     return 0;
    148 }
    View Code
  • 相关阅读:
    nodejs REPL清屏
    flask 的relationship使用
    flask 数据库多对多(文章和标签)
    设置管理员的 蘑菇丁日报 周报、月报
    访问个人主页、蘑菇丁、使用:import urllib.request
    使用requests访问、登录蘑菇丁
    UUID
    爬虫经典案例
    selenium 获取 cookies, 然后登录
    用chrome浏览器登录以后 手动找到cookie、加到自己的python代码中
  • 原文地址:https://www.cnblogs.com/stddddd/p/10009451.html
Copyright © 2011-2022 走看看