2464: 中山市选[2009]小明的游戏
Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 842 Solved: 344
[Submit][Status][Discuss]
Description
Input
Output
Sample Input
@#
#@
0 0 1 1
2 2
@@
@#
0 1 1 0
0 0
Sample Output
0
HINT
对于100%的数据满足:1 < = n, m <= 500。
#include<iostream>
#include<cstdlib>
#include<cstdio>
using namespace std;
char ch[505][505];
int qx[5000000],qy[5000000],tot,tow;
int dis[505][505];
bool flag[505][505];
int n,m,sx,sy,tx,ty,nx,ny;
int fx[5]={0,0,1,-1};
int fy[5]={1,-1,0,0};
inline void bfs(){
for (int i=1;i<=n;++i)
for (int j=1;j<=m;++j)
dis[i][j]=1e9;
qx[tot=1]=sx;qy[tow=1]=sy;dis[sx][sy]=0;
while (tot<=tow){
flag[qx[tot]][qy[tot]]=0;
for (int i=0;i<4;++i){
nx=qx[tot]+fx[i];
ny=qy[tot]+fy[i];
if (nx<1 || nx>n || ny<1 || ny>m)continue;
if (dis[nx][ny]>dis[qx[tot]][qy[tot]]+(ch[nx][ny]!=ch[qx[tot]][qy[tot]])){
dis[nx][ny]=dis[qx[tot]][qy[tot]]+(ch[nx][ny]!=ch[qx[tot]][qy[tot]]);
if (!flag[nx][ny])
flag[qx[++tow]=nx][qy[tow]=ny]=1;
}
}
++tot;
}
}
int main (){
while (1){
scanf ("%d%d",&n,&m);
if (!n && !m)return 0;
for (int i=1;i<=n;++i)
scanf ("%s",ch[i]+1);
scanf ("%d%d%d%d",&sx,&sy,&tx,&ty);
sx++;sy++;tx++;ty++;
bfs();
printf ("%d\n",dis[tx][ty]);
}
return 0;
}
3199




被折叠的 条评论
为什么被折叠?



