bzoj2464: 中山市选[2009]小明的游戏(最短路)

2464: 中山市选[2009]小明的游戏

题目:传送门 

 


 

 

题解:

   最短路的裸题...

    


 

 

代码:

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<cstdlib>
 4 #include<algorithm>
 5 #include<cmath>
 6 using namespace std;
 7 int dx[5]={0,1,-1,0,0};
 8 int dy[5]={0,0,0,-1,1};
 9 struct node
10 {
11     int x,y;
12 }list[510000];
13 int d[550][550],sx,sy,ex,ey;
14 bool v[550][550];
15 char s[550][550];
16 int main()
17 {
18     int n,m;
19     while(scanf("%d%d",&n,&m)!=EOF)
20     {
21         if(n==0 && m==0)break;
22         for(int i=1;i<=n;i++)scanf("%s",s[i]+1);
23         scanf("%d%d%d%d",&sx,&sy,&ex,&ey);sx++;sy++;ex++;ey++;
24         list[1].x=sx;list[1].y=sy;
25         memset(d,63,sizeof(d));d[sx][sy]=0;
26         memset(v,false,sizeof(v));v[sx][sy]=true;
27         int head=1,tail=2;
28         while(head!=tail)
29         {
30             int x=list[head].x,y=list[head].y;
31             for(int i=1;i<=4;i++)
32             {
33                 int tx=x+dx[i],ty=y+dy[i];
34                 if(tx<1 || ty<1 || tx>n || ty>m) continue;
35                 int c;if(s[tx][ty]==s[x][y])c=0;else c=1;
36                 if(d[tx][ty]>d[x][y]+c)
37                 {
38                     d[tx][ty]=d[x][y]+c;
39                     if(v[tx][ty]==false)
40                     {
41                         v[tx][ty]=true;
42                         list[tail].x=tx,list[tail].y=ty;
43                         tail++;if(tail==n*m+1)tail=1;
44                     }
45                 }
46             }
47             head++;if(head==n*m+1) head=1;
48             v[x][y]=false;
49         }
50         printf("%d\n",d[ex][ey]);
51     }
52     return 0;
53 }

 

posted @ 2018-03-26 21:19  CHerish_OI  阅读(208)  评论(0编辑  收藏  举报