ural 1119 Metro

http://acm.timus.ru/problem.aspx?space=1&num=1119

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <queue>
 4 #include <cmath>
 5 #include <algorithm>
 6 #define maxn 1010
 7 using namespace std;
 8 
 9 int head[maxn],e,n,m;
10 int dir[4][2]={{0,-1},{0,1},{1,0},{-1,0}};
11 bool vis[maxn][maxn];
12 bool visi[maxn][maxn];
13 double step;
14 
15 struct node
16 {
17     int x,y;
18     double step;
19 }st,st1,st2;
20 
21 void bfs(node st)
22 {
23     queue<node>q;
24     st.step=0.0;
25     q.push(st);
26     memset(visi,false,sizeof(visi));
27     visi[st.x][st.y]=true;
28     while(!q.empty())
29     {
30         st1=q.front();
31         q.pop();
32         if(st1.x==m&&st1.y==n) {step=st1.step;return;}
33         for(int i=0; i<4; i++)
34         {
35             int xx=st1.x+dir[i][0];
36             int yy=st1.y+dir[i][1];
37             if(xx>=0&&xx<=m&&yy>=0&&yy<=n&&!visi[xx][yy])
38             {
39                 st2.step=st1.step+100.0;
40                 visi[xx][yy]=true;
41                 st2.x=xx;
42                 st2.y=yy;
43                 q.push(st2);
44             }
45         }
46         if(vis[st1.x+1][st1.y+1]&&st1.x+1<=m&&st1.y+1<=n&&st1.x+1>=0&&st1.y+1>=0)
47         {
48             vis[st1.x+1][st1.y+1]=true;
49             st2.x=st1.x+1;
50             st2.y=st1.y+1;
51             st2.step=st1.step+sqrt(2)*100.0;
52             q.push(st2);
53         }
54     }
55 }
56 int main()
57 {
58     int k,a,b;
59     step=0;
60     scanf("%d%d",&n,&m);
61     scanf("%d",&k);
62     memset(vis,false,sizeof(vis));
63     for(int i=1; i<=k; i++)
64     {
65         scanf("%d%d",&a,&b);
66         vis[b][a]=true;
67     }
68     st.x=0;
69     st.y=0;
70     bfs(st);
71     printf("%.0lf
",step);
72     return 0;
73 }
View Code
原文地址:https://www.cnblogs.com/fanminghui/p/3621834.html