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....
    #....
    #....
    .....
    #....
    */

  • 相关阅读:
    Angularjs中的ng-class
    AngularJS 的表单验证
    Eclipse更新慢、插件安装慢解决方案zz
    PSD的单位及计算方法[转]
    .NET控件名称缩写一览表 zz
    C#Stopwatch的简单计时zz
    VsVim的快捷键
    MySQL-mysql 8.0.11安装教程
    使用open live writer客户端写博客zz
    WPFToolkit DataGrid 使用介绍zz
  • 原文地址:https://www.cnblogs.com/zzqc/p/7116269.html
Copyright © 2011-2022 走看看