Camelot USACO 3.3(阵亡)

好烦的一题,思路不是很难,bfs,数据结构要定义好,写起来各种问题,看nocow题解,照某个敲,到case20就是不过。

我擦,后来我直接把他代码复制过去都过不了。找了个别人的先过了...

 1 /*
 2 
 3 ID: hubiao cave
 4 
 5 PROG: camelot
 6 
 7 LANG: C++
 8 
 9 */
10 
11 #include <iostream>
12 #include <fstream>
13 #include<cstring>
14  
15 using namespace std;
16  
17 struct data{
18        int x,y,d;
19 }q[10000];
20  
21 bool use[40][40];
22 int l,r,n,m,x[1001],y[1001],kx,ky,d[40][40][40][40],tt=0,res=2147483647,step[8][2]={{1,2},{2,1},{-1,2},{2,-1},{1,-2},{-2,1},{-1,-2},{-2,-1}};
23  
24 int main()
25 {
26     ifstream fin("camelot.in");
27     ofstream fout("camelot.out");
28  
29     char cy;
30     fin>>n>>m;
31     fin>>cy>>kx;
32     ky=cy-'A'+1;
33     while (!fin.eof())
34     {
35           tt++;
36           fin>>cy>>x[tt];
37           y[tt]=cy-'A'+1;
38     }
39     tt++;
40     for (int i=1;i<=n;i++)
41         for (int j=1;j<=m;j++)
42             for (int k=1;k<=n;k++)
43                 for (int t=1;t<=m;t++)
44                     d[i][j][k][t]=10000000;
45     for (int i=1;i<=n;i++)
46         for (int j=1;j<=m;j++)
47         {
48            memset(q,0,sizeof(q));
49            memset(use,false,sizeof(use));
50            l=1;
51            r=1;
52            q[1].x=i;
53            q[1].y=j;
54            use[i][j]=true; 
55            d[i][j][i][j]=0;
56            for(;;)
57            {
58                   for (int k=0;k<=7;k++)
59                   {
60                       int xx=q[l].x+step[k][0],yy=q[l].y+step[k][1];
61                       if (((xx>=1)&&(xx<=n)&&(yy>=1)&&(yy<=m)&&(!use[xx][yy]))){
62                                                                                 r++;
63                                                                                 q[r].x=xx;
64                                                                                 q[r].y=yy;
65                                                                                 d[i][j][xx][yy]=q[l].d+1;
66                                                                                 q[r].d=d[i][j][xx][yy];
67                                                                                 use[xx][yy]=true;
68                                                                                 }
69                   }
70                   l++;
71                   if (l>r) break;
72            }
73         }
74  
75     for (int i=1;i<=n;i++)
76         for (int j=1;j<=m;j++)
77         {
78             int minn=2147483647,sum=0;
79             for (int k=1;k<=tt;k++)
80                 sum+=d[i][j][x[k]][y[k]];
81             if (sum+max(abs(kx-i),abs(ky-j))<minn) minn=sum+max(abs(kx-i),abs(ky-j));
82             for (int xx=max(1,kx-2);xx<=min(n,kx+2);xx++)
83                 for (int yy=max(1,ky-2);yy<=min(m,ky+2);yy++)
84                     for (int k=1;k<=tt;k++)
85                     {
86                         int now=sum-d[i][j][x[k]][y[k]]+d[x[k]][y[k]][xx][yy]+d[xx][yy][i][j]+max(abs(kx-xx),abs(ky-yy));
87                         if (now<minn) minn=now;
88                     }
89             if (minn<res) res=minn;
90         }
91     fout<<res<<endl;
92     return 0;
93 }

 

posted @ 2013-10-15 00:06  cavehubiao  阅读(291)  评论(0编辑  收藏  举报