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 }

 

转载于:https://www.cnblogs.com/CHerish_OI/p/8654013.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值