标签:des style blog http color io os ar for
1 #include<cstdio> 2 #include<cstring> 3 struct target{ 4 int x,y; 5 }t[1000001]; 6 struct queue{ 7 int x,y; 8 }q[1000001]; 9 const int mx[4]={0,1,0,-1}; 10 const int my[4]={1,0,-1,0}; 11 int n,m,cnt,x1,y1,x2,y2,head,tail,ans=100000000; 12 int map[1001][1001]; 13 int dis1[1001][1001]; 14 int dis2[1001][1001]; 15 bool mrk[1001][1001]; 16 inline int min(int a,int b) 17 {return a<b?a:b;} 18 inline int read() 19 { 20 int x=0,f=1;char ch=getchar(); 21 while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();} 22 while(ch>=‘0‘&&ch<=‘9‘){x=x*10+ch-‘0‘;ch=getchar();} 23 return x*f; 24 } 25 inline void bfs1(int x,int y) 26 { 27 head=0;tail=1;mrk[x][y]=1; 28 q[1].x=x;q[1].y=y; 29 while (head<tail) 30 { 31 int nx=q[++head].x,ny=q[head].y; 32 for (int k=0;k<4;k++) 33 { 34 int xx=nx+mx[k],yy=ny+my[k]; 35 if (xx<1||xx>n||yy<1||yy>m)continue; 36 if (mrk[xx][yy]||map[xx][yy]==1) continue; 37 dis1[xx][yy]=dis1[nx][ny]+1; 38 q[++tail].x=xx;q[tail].y=yy; 39 mrk[xx][yy]=1; 40 } 41 } 42 } 43 inline void bfs2(int x,int y) 44 { 45 memset(q,0,sizeof(q)); 46 memset(mrk,0,sizeof(mrk)); 47 head=0;tail=1;mrk[x][y]=1; 48 q[1].x=x;q[1].y=y; 49 while (head<tail) 50 { 51 int nx=q[++head].x,ny=q[head].y; 52 for (int k=0;k<4;k++) 53 { 54 int xx=nx+mx[k],yy=ny+my[k]; 55 if (xx<1||xx>n||yy<1||yy>m)continue; 56 if (mrk[xx][yy]||map[xx][yy]==1) continue; 57 dis2[xx][yy]=dis2[nx][ny]+1; 58 q[++tail].x=xx;q[tail].y=yy; 59 mrk[xx][yy]=1; 60 } 61 } 62 } 63 int main() 64 { 65 m=read();n=read(); 66 for (int i=1;i<=n;i++) 67 for(int j=1;j<=m;j++) 68 { 69 map[i][j]=read(); 70 if (map[i][j]==4) 71 { 72 t[++cnt].x=i; 73 t[cnt].y=j; 74 }else 75 if (map[i][j]==2) 76 { 77 x1=i; 78 y1=j; 79 map[i][j]=0; 80 }else 81 if (map[i][j]==3) 82 { 83 x2=i; 84 y2=j; 85 map[i][j]=0; 86 } 87 } 88 bfs1(x1,y1); 89 bfs2(x2,y2); 90 for (int i=1;i<=cnt;i++) 91 { 92 int nx=t[i].x,ny=t[i].y; 93 if (!(dis1[nx][ny]+dis2[nx][ny]))continue; 94 ans=min(ans,dis1[nx][ny]+dis2[nx][ny]); 95 } 96 printf("%d",ans); 97 }
BZOJ1671: [Usaco2005 Dec]Knights of Ni
标签:des style blog http color io os ar for
原文地址:http://www.cnblogs.com/zyfzyf/p/3984053.html