题意

给出一个$n \times m$的网格,每个位置有一个小写字母,初始在$(1, 1)$,每次可以向上下左右走,问走到$(n, m)$的最小花费

设$(x, y)$为当前位置,$(nx, ny)$为下一位置。$s[x][y]$表示$(x, y)$位置的字符。

若$s[x][y] = s[nx][ny]$,则移动的花费为$0$,否则花费为$1$

Sol

01BFS的裸题,感觉这个点子还是很妙的

01BFS可以在$O(n+m)$求出边权只有$0 / 1$的最短路

我们维护一个双端队列,如当前可以进行松弛那么就进行更新,更新完后判断一下,若边权为$1$,则在队尾加入下一个点,否则在队首加入下一个点

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
//#define int long long
using namespace std;
const int MAXN = ;
inline int read() {
char c = getchar(); int x = , f = ;
while(c < '' || c > '') {if(c == '-') f = -; c = getchar();}
while(c >= '' && c <= '') x = x * + c - '', c = getchar();
return x * f;
}
int N, M;
struct Node {
int x, y, s;
};
deque<Node> q;
int dis[MAXN][MAXN], xx[] = {-, +, , }, yy[] = {, , -, +};
char s[MAXN][MAXN];
void OneZeroBFS() {
q.push_back((Node) {, , });
while(!q.empty()) {
Node p = q.front(); q.pop_front();
for(int i = ; i < ; i++) {
int wx = p.x + xx[i], wy = p.y + yy[i];
int w = (s[wx][wy] != s[p.x][p.y]);
if(dis[wx][wy] > dis[p.x][p.y] + w && (wx > && wy > && wx <= N && wy <= M))
dis[wx][wy] = dis[p.x][p.y] + w,
w == ? q.push_back((Node) {wx, wy, w}) : q.push_front((Node) {wx, wy, w});
}
}
}
main() {
int QwQ = read();
while(QwQ--) {
memset(dis, 0xf, sizeof(dis));
dis[][] = ;
N = read(); M = read();
for(int i = ; i <= N; i++)
scanf("%s", s[i] + );
OneZeroBFS();
printf("%d\n", dis[N][M]);
}
return ;
}
/*
4
2 2
aa
aa
2 3
abc
def
6 6
akaccc
aaacfc
amdfcc
aokhdd
zyxwdp
zyxwdd
5 5
abbbc
abacc
aaacc
aefci
cdgdd
*/

SPOJ KATHTHI - KATHTHI(01BFS)的更多相关文章

  1. SPOJ KATHTHI - KATHTHI

    以前并不知道这个trick. $01BFS$,在$bfs$的时候用一个双端队列来维护,如果边权为$1$就添加到队尾,边权为$0$就添加到队首. 还有一个小trick就是我们可以开一个$dis$数组来代 ...

  2. BZOJ 2588: Spoj 10628. Count on a tree [树上主席树]

    2588: Spoj 10628. Count on a tree Time Limit: 12 Sec  Memory Limit: 128 MBSubmit: 5217  Solved: 1233 ...

  3. SPOJ DQUERY D-query(主席树)

    题目 Source http://www.spoj.com/problems/DQUERY/en/ Description Given a sequence of n numbers a1, a2, ...

  4. SPOJ GSS3 Can you answer these queries III[线段树]

    SPOJ - GSS3 Can you answer these queries III Description You are given a sequence A of N (N <= 50 ...

  5. 【填坑向】spoj COT/bzoj2588 Count on a tree

    这题是学主席树的时候就想写的,,, 但是当时没写(懒) 现在来填坑 = =日常调半天lca(考虑以后背板) 主席树还是蛮好写的,但是代码出现重复,不太好,导致调试的时候心里没底(虽然事实证明主席树部分 ...

  6. SPOJ bsubstr

    题目大意:给你一个长度为n的字符串,求出所有不同长度的字符串出现的最大次数. n<=250000 如:abaaa 输出: 4 2 1 1 1 spoj上的时限卡的太严,必须使用O(N)的算法那才 ...

  7. 【SPOJ 7258】Lexicographical Substring Search

    http://www.spoj.com/problems/SUBLEX/ 好难啊. 建出后缀自动机,然后在后缀自动机的每个状态上记录通过这个状态能走到的不同子串的数量.该状态能走到的所有状态的f值的和 ...

  8. 【SPOJ 1812】Longest Common Substring II

    http://www.spoj.com/problems/LCS2/ 这道题想了好久. 做法是对第一个串建后缀自动机,然后用后面的串去匹配它,并在走过的状态上记录走到这个状态时的最长距离.每匹配完一个 ...

  9. 【SPOJ 8222】Substrings

    http://www.spoj.com/problems/NSUBSTR/ clj课件里的例题 用结构体+指针写完模板后发现要访问所有的节点,改成数组会更方便些..于是改成了数组... 这道题重点是求 ...

随机推荐

  1. [CROATIAN2009] OTOCI

    [题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=1180 [算法] 动态树维护森林连通性 时间复杂度 : O(NlogN ^ 2) [代 ...

  2. Thrift之代码生成器Compiler原理及源码详细解析2

    我的新浪微博:http://weibo.com/freshairbrucewoo. 欢迎大家相互交流,共同提高技术. 2  t_generator类和t_generator_registry类 这个两 ...

  3. bzoj 3527 [Zjoi2014] 力 —— FFT

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3527 看了看TJ才推出来式子,还是不够熟练啊: TJ:https://blog.csdn.n ...

  4. GitHub---github入门

    setup git --- create a repository-----fork a repository -- a pull request (be social)

  5. 【转】C/C++使用心得:enum与int的相互转换

    https://blog.csdn.net/lihao21/article/details/6825722 如何正确理解enum类型? 例如: enum Color { red, white, blu ...

  6. C. Pearls in a Row

    C. Pearls in a Row time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  7. docker的安装与卸载

    卸载老版本docker sudo apt-get remove docker docker-engine docker.io /var/lib/docker/目录下存放着 images, contai ...

  8. 【eclipse插件开发实战】Eclipse插件开发4——插件JDE、PDE开发方式及plugin.xml配置文件结构

    Eclipse插件开发4--插件JDE.PDE开发方式及plugin.xml配置文件结构 开发方式分为:java开发环境JDE开发插件的方式和插件开发环境PDE开发插件方式. 插件通过添加到预定义的扩 ...

  9. UVa 1660 Cable TV Network (最大流,最小割)

    题意:求一个无向图的点连通度. 析:把每个点拆成两个,然后中间连接一个容量为1的边,然后固定一个源点,枚举每个汇点,最小割. 代码如下: #pragma comment(linker, "/ ...

  10. jquery 点击某一行,得到这一行的每个列的数据

    <html><head> <title>test</title> <script src="../Scripts/jquery-1.8. ...