(福大2015年3月月賽)FZU 2186 小明的迷宮 (BFS+狀壓DP),fzubfs

來源:互聯網
上載者:User

(福大2015年3月月賽)FZU 2186 小明的迷宮 (BFS+狀壓DP),fzubfs

題目地址:FZU 2186
這題是很基礎的TSP狀壓,各個點之間的距離要先用BFS預先處理出來。
這題在寫memset(dp,INF,sizeof(dp));時,寫成了memset(dp,INF,sizeof(d));。。。
調試了好長時間。。無語了。。。最近的狀態太不行了。。
代碼如下:

#include <iostream>#include <string.h>#include <math.h>#include <queue>#include <algorithm>#include <stdlib.h>#include <map>#include <set>#include <stdio.h>using namespace std;#define LL __int64#define pi acos(-1.0)const int mod=1e9+7;const int INF=0x3f3f3f3f;const double eqs=1e-9;const int MAXN=300000+10;int d[13][13], dp[1<<12][13], n, m;int mp[110][110], vis[110][110];int jx[]={0,0,1,-1};int jy[]={1,-1,0,0};struct node{        int x, y, step;};void bfs(int x, int y, int z){        queue<node>q;        node f1, f2;        f1.x=x;f1.y=y;f1.step=0;        q.push(f1);        memset(vis,0,sizeof(vis));        vis[x][y]=1;        while(!q.empty()){                f1=q.front();                q.pop();                if(mp[f1.x][f1.y]>0) d[z][mp[f1.x][f1.y]]=f1.step;                for(int i=0;i<4;i++){                        f2.x=f1.x+jx[i];                        f2.y=f1.y+jy[i];                        if(f2.x>=0&&f2.x<n&&f2.y>=0&&f2.y<m&&!vis[f2.x][f2.y]&&mp[f2.x][f2.y]>=0){                                vis[f2.x][f2.y]=1;                                f2.step=f1.step+1;                                q.push(f2);                        }                }        }}int main(){        int i, j, cnt, tot, tmp, k, ans;        while(scanf("%d%d",&n,&m)!=EOF){                cnt=0;                for(i=0;i<n;i++){                        for(j=0;j<m;j++){                                scanf("%d",&mp[i][j]);                                if(mp[i][j]>0&&i+j) mp[i][j]=++cnt;                        }                }                if(mp[0][0]<0){                        puts("-1");                        continue ;                }                if(!cnt){                        puts("0");                        continue ;                }                mp[0][0]=0;                memset(d,INF,sizeof(d));                bfs(0,0,0);                for(i=0;i<n;i++){                        for(j=0;j<m;j++){                                if(mp[i][j]>0){                                        bfs(i,j,mp[i][j]);                                }                        }                }                tot=1<<cnt;                memset(dp,INF,sizeof(dp));                for(i=1;i<tot;i++){                        for(j=0;j<cnt;j++){                                if(i&(1<<j)){                                        tmp=i-(1<<j);                                        if(tmp==0){                                                dp[i][j+1]=d[0][j+1];                                                continue ;                                        }                                        for(k=0;k<cnt;k++){                                                if(tmp&(1<<k)){                                                        dp[i][j+1]=min(dp[i][j+1],dp[tmp][k+1]+d[k+1][j+1]);                                                }                                        }                                }                        }                }                ans=INF;                for(i=1;i<=cnt;i++){                        ans=min(ans,dp[tot-1][i]+d[0][i]);                }                if(ans==INF) puts("-1");                else printf("%d\n",ans);        }        return 0;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.