Surface
As if there is nothing to say, just practice the code force ...
First run out of the island with BFs, and then run the shortest way to find the distance between the islands, the final pressure DP to come to the answer
Attention to detail, code code 2333
1#include <Set>2#include <queue>3#include <cstdio>4#include <cstring>5#include <algorithm>6 using namespacestd;7 Const intn= -, m= the, l= -;8 Const intmov[4][2]={{0,1},{0,-1},{1,0},{-1,0}};9 structaTen { One intXx,yy; A}que[l*L]; - CharMapp[l][l]; - intval[n][n],dp[1<<N] [N]; the intMapp[l][l],vis[l][l],dis[l][l]; - intn,m,f,b,nx,ny,land,shal,nde,all,ans=2e9; -Deque<a>Qs; - BOOLOk1 (intXinty) + { - returnmapp[x][y]=='X'&&! mapp[x][y]&&x>=1&&x<=n&&y>=1&&y<=m; + } A voidBFS1 (intXinty) at { -que[f=b=0]= (a) {x,y},mapp[x][y]=++Land ; - while(f<=b) - { -A tn=que[f++]; - inttx=tn.xx,ty=tn.yy; in for(intI=0;i<4; i++) - if(Ok1 (nx=tx+mov[i][0],ny=ty+mov[i][1])) tomapp[nx][ny]=land,que[++b]=(a) {nx,ny}; + } - } the BOOLOK2 (intXinty) * { $ returnmapp[x][y]=='S'&&! mapp[x][y]&&x>=1&&x<=n&&y>=1&&y<=m;Panax Notoginseng } - voidBFS2 (intXinty) the { +que[f=b=0]= (a) {x,y},mapp[x][y]=--Shal; A while(f<=b) the { +A tn=que[f++]; - inttx=tn.xx,ty=tn.yy; $ for(intI=0;i<4; i++) $ if(Ok2 (nx=tx+mov[i][0],ny=ty+mov[i][1])) -mapp[nx][ny]=shal,que[++b]=(a) {nx,ny}; - } the } - BOOLOkintXinty)Wuyi { the returnmapp[x][y]&&!vis[x][y]&&x>=1&&x<=n&&y>=1&&y<=m; - } Wu voidBFS (intXinty) - { About Qs.push_back ((a) {x, y}); $memset (Vis,0,sizeofvis); -memset (DIS,0,sizeofdis); -nde=mapp[x][y],val[nde][nde]=0, vis[x][y]=true; - while(!qs.empty ()) A { +a Tt=qs.front (); Qs.pop_front ();inttx=tt.xx,ty=tt.yy; the for(intI=0;i<4; i++) - if(OK (nx=tx+mov[i][0],ny=ty+mov[i][1])) $ { thevis[nx][ny]=true; the if(mapp[tx][ty]<0) dis[nx][ny]=dis[tx][ty]+1, Qs.push_back ((a) {nx,ny}); the Else if(mapp[tx][ty]>0) dis[nx][ny]=Dis[tx][ty],qs.push_front ((a) {nx,ny}); the } - } in for(intI=1; i<=n;i++) the for(intj=1; j<=m;j++) the if(mapp[i][j]>0) Aboutval[nde][mapp[i][j]]=min (val[nde][mapp[i][j]],dis[i][j]); the } the intInsintNDE) the { + return 1<< (nde-1); - } the intMain ()Bayi { thescanf"%d%d",&n,&m); the for(intI=1; i<=n;i++) -scanf"%s", mapp[i]+1); - for(intI=1; i<=n;i++) the for(intj=1; j<=m;j++) the if(Ok1 (i,j)) BFS1 (i,j); the Else if(Ok2 (i,j)) BFS2 (i,j); theMemset (Val,0x3f,sizeofval); - for(intI=1; i<=n;i++) the for(intj=1; j<=m;j++) the if(mapp[i][j]>0) BFS (i,j); theAll= (1<<land)-1; 94Memset (DP,0x3f,sizeofDP); the for(intI=1; i<=land;i++) Dp[ins (i)][i]=0; the for(intI=0; i<=all;i++) the for(intj=1; j<=land;j++) 98 if(i&ins (j)) About for(intk=1; k<=land;k++) - if(! (I&ins (k)) &&j!=k)101Dp[i|ins (k)][k]=min (Dp[i|ins (k)][k],dp[i][j]+val[j][k]);102 for(intI=1; i<=land;i++) ans=min (ans,dp[all][i]);103printf"%d", ans);104 return 0; the}
View Code
Problem solving: Usaco13jan Island Travels