以前并不知道这个trick。

$01BFS$,在$bfs$的时候用一个双端队列来维护,如果边权为$1$就添加到队尾,边权为$0$就添加到队首。

还有一个小trick就是我们可以开一个$dis$数组来代替$vis$数组做类似于$dp$的操作,因为双端插入的特性使$vis$的表示可能产生歧义,每一次能更新$dis$即入队,这样子也刚好对应了最短路的意义。

时间复杂度$O(Tn^2)$。

Code:

#include <cstdio>
#include <cstring>
#include <deque>
using namespace std; const int N = ;
const int dx[] = {, , -, };
const int dy[] = {, , , -}; int testCase, n, m, dis[N][N];
char mp[N][N]; struct Node {
int x, y, stp; inline Node(int nowX = , int nowY = , int nowStp = ) {
x = nowX, y = nowY, stp = nowStp;
} };
deque <Node> Q; inline bool valid(Node now) {
return now.x >= && now.x <= n && now.y >= && now.y <= m;
} int bfs() {
if(n == && m == ) return ;
memset(dis, 0x3f, sizeof(dis));
dis[][] = ;
Q.clear(); Q.push_back(Node(, , ));
for(; !Q.empty(); ) {
Node out = Q.front(); Q.pop_front();
for(int i = ; i < ; i++) {
Node in = Node(out.x + dx[i], out.y + dy[i], );
if(!valid(in)) continue;
if(dis[in.x][in.y] <= dis[out.x][out.y] + (mp[out.x][out.y] != mp[in.x][in.y]))
continue;
dis[in.x][in.y] = dis[out.x][out.y] + (mp[out.x][out.y] != mp[in.x][in.y]);
in.stp = dis[in.x][in.y];
if(in.stp == out.stp) Q.push_front(in);
else Q.push_back(in);
}
}
} int main() {
for(scanf("%d", &testCase); testCase--; ) {
scanf("%d%d", &n, &m);
for(int i = ; i <= n; i++) scanf("%s", mp[i] + );
bfs();
printf("%d\n", dis[n][m]); /* for(int i = 1; i <= n; i++, printf("\n"))
for(int j = 1; j <= m; j++)
printf("%c ", mp[i][j]); printf("\n");
for(int i = 1; i <= n; i++, printf("\n"))
for(int j = 1; j <= m; j++)
printf("%d ", vis[i][j]); */ }
return ;
}

SPOJ KATHTHI - KATHTHI的更多相关文章

  1. SPOJ KATHTHI - KATHTHI(01BFS)

    题意 给出一个$n \times m$的网格,每个位置有一个小写字母,初始在$(1, 1)$,每次可以向上下左右走,问走到$(n, m)$的最小花费 设$(x, y)$为当前位置,$(nx, ny)$ ...

  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. uva120 Stacks of Flapjacks (构造法)

    这个题没什么算法,就是想出怎么把答案构造出来就行. 思路:越大的越放在底端,那么每次就找出还没搞定的最大的,把它移到当前还没定好的那些位置的最底端,定好的就不用管了. 这道题要处理好输入,每次输入的一 ...

  2. uuid 学习

    #include <vector> #include <iostream> #include <boost/uuid/uuid.hpp> #include < ...

  3. @media screen页面自适应尺寸!!

    @media screen and (max-width:360px){body,input,select{font-size:38%}} @media screen and (min-width:3 ...

  4. Node.Js and Mongoose

    Mongoose官方API,我做完之后整理出来的心得. ONE· Getting Started First be sure you have MongoDB and Node.js installe ...

  5. 查看.Net Framework版本号

    目录 摘要 .NET Framework 的版本 确定计算机上安装的 .NET Framework 版本 补充几个查看.Net Framework版本号 概要 本文描述如何确定计算机上安装的 Micr ...

  6. Python函数-input()

    input([prompt]) 如果[prompt]是存在的,它被写入标准输出中没有换行.然后函数读取输入,将其转换为一个字符串,然后返回. >>> s = input('--> ...

  7. POJ1456:Supermarket(并查集版)

    浅谈并查集:https://www.cnblogs.com/AKMer/p/10360090.html 题目传送门:http://poj.org/problem?id=1456 堆作法:https:/ ...

  8. Ubuntu 16.04安装Elasticsearch,Logstash和Kibana(ELK)Filebeat

    https://www.howtoing.com/how-to-install-elasticsearch-logstash-and-kibana-elk-stack-on-ubuntu-16-04 ...

  9. 第四篇 PHP的成长路线

    学PHP开发这么久,进步不大,个人进行了分析.认为是我自己没有设定目标,就是对于自己要学成什么样没有清晰的认识. 今天特别了解了一下PHP的成长参考路线,以便自己以后迷失方向.PHP主要应该基于MYS ...

  10. SPI驱动框架-1(DM8127 Linux2.6.37为例)

    一.驱动程序结构 1.platform_device 文件:/arch/arm/mach-omap2/device.c static struct omap2_mcspi_platform_confi ...