bzoj2464
来源:互联网 发布:用数据包怎么传淘宝 编辑:程序博客网 时间:2024/05/21 20:33
2464: 中山市选[2009]小明的游戏
Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 842 Solved: 344
[Submit][Status][Discuss]
Description
小明最近喜欢玩一个游戏。给定一个n * m的棋盘,上面有两种格子#和@。游戏的规则很简单:给定一个起始位置和一个目标位置,小明每一步能向上,下,左,右四个方向移动一格。如果移动到同一类型的格子,则费用是0,否则费用是1。请编程计算从起始位置移动到目标位置的最小花费。
Input
输入文件有多组数据。
输入第一行包含两个整数n,m,分别表示棋盘的行数和列数。
输入接下来的n行,每一行有m个格子(使用#或者@表示)。
输入接下来一行有四个整数x1, y1, x2, y2,分别为起始位置和目标位置。
当输入n,m均为0时,表示输入结束。
Output
对于每组数据,输出从起始位置到目标位置的最小花费。每一组数据独占一行。
Sample Input
2 2
@#
#@
0 0 1 1
2 2
@@
@#
0 1 1 0
0 0
@#
#@
0 0 1 1
2 2
@@
@#
0 1 1 0
0 0
Sample Output
2
0
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;}
阅读全文
1 0
- bzoj2464
- [BZOJ2464]中山市选[2009]小明的游戏
- 【中山市选2009】【BZOJ2464】小明的游戏
- 【bzoj2464】【中山市选2009】【小明的游戏】【spfa】
- BZOJ2464: 中山市选[2009]小明的游戏
- 【BZOJ2464】【中山市选2009】小明的游戏 最短路水过
- hdu 5884 Sort 二分 + K叉哈夫曼树 两个队列
- Apache Flink发布0.8.0,公布2015年规划
- C语言复习总结篇(一)
- 【python学习笔记】26:scipy卷积运算
- 解决img之间的间隙
- bzoj2464
- 银行业和游戏业的技术体系架构
- 以太网帧格式详解
- 多线程----守护线程---Deamon
- 逻辑一致、交叉验证
- Python中数据类型
- 管理网络
- 简单易用的MVC框架:VRaptor
- 三、MongoDB的基本概念之集合