zoukankan      html  css  js  c++  java
  • uva10944 状态压缩bfs or DP

    又是一道状压搜索,题解有的是状压DP做的目前不会日后补

    写好了以后一直蜜汁WA,看别人代码把判断再次回到原点的语句写在了Q.pop()之后而不是for里,对我也是一种启发吧这样写确实有好处比如起点就是答案的情况也能处理,很方便,我i改过之后就A了。



    #include<bits/stdc++.h>
    using namespace std;
    #define inf 0x3f3f3f3f;
    bool vis[22][22][(1<<16)+1];
    char e[22][22];
    int nut[22][22];
    int N,M,Count,ans;
    int fx[8][2]={1,0,-1,0,0,1,0,-1,1,-1,1,1,-1,1,-1,-1};
    struct node
    {
        int x,y,hal,bs;
        node(){}
        node(int x,int y,int hal,int bs):x(x),y(y),hal(hal),bs(bs){}
    };
    void bfs(int sx,int sy)
    {
      memset(vis,0,sizeof(vis));
      queue<node>Q;
      node t1(sx,sy,0,0),t2;
      Q.push(t1);
      vis[sx][sy][0]=1;
      while(!Q.empty()){
          t1=Q.front();  Q.pop();
          if(e[t1.x][t1.y]=='L'&&t1.hal==ans) {cout<<t1.bs<<endl;return;}
          for(int i=0;i<8;++i){
            t2.x=t1.x+fx[i][0];
            t2.y=t1.y+fx[i][1];
            t2.bs=t1.bs+1;
            t2.hal=t1.hal;
            if(t2.x>0&&t2.y>0&&e[t2.x][t2.y]=='#') t2.hal=(t2.hal|(1<<nut[t2.x][t2.y]));
            if(t2.x<1||t2.y<1||t2.x>N||t2.y>M||vis[t2.x][t2.y][t2.hal]) continue;
            vis[t2.x][t2.y][t2.hal]=1;
            Q.push(t2);
          }
      }
    }
    int main()
    {
        int i,j,k;
        while(scanf("%d%d",&N,&M)!=EOF){Count=0;
        int sx,sy;
        memset(e,0,sizeof(e));
        memset(nut,-1,sizeof(nut));
            for(i=1;i<=N;++i)
               for(j=1;j<=M;++j){
                scanf(" %c",&e[i][j]);
                if(e[i][j]=='L') {sx=i;sy=j;}
                else if (e[i][j]=='#'){
                    nut[i][j]=Count++;
                }
            }
            if(Count==0) {puts("0");continue;}
            ans=(1<<Count)-1;
            bfs(sx,sy);
        }
        return 0;
    }


    /*
    5 5
    L....
    #....
    #....
    .....
    #....
    */

  • 相关阅读:
    HDU 2196 Computer
    HDU 1520 Anniversary party
    POJ 1217 FOUR QUARTERS
    POJ 2184 Cow Exhibition
    HDU 2639 Bone Collector II
    POJ 3181 Dollar Dayz
    POJ 1787 Charlie's Change
    POJ 2063 Investment
    HDU 1114 Piggy-Bank
    Lca hdu 2874 Connections between cities
  • 原文地址:https://www.cnblogs.com/zzqc/p/7116269.html
Copyright © 2011-2022 走看看