SPOJ KATHTHI - KATHTHI
以前并不知道这个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的更多相关文章
- SPOJ KATHTHI - KATHTHI(01BFS)
题意 给出一个$n \times m$的网格,每个位置有一个小写字母,初始在$(1, 1)$,每次可以向上下左右走,问走到$(n, m)$的最小花费 设$(x, y)$为当前位置,$(nx, ny)$ ...
- 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 ...
- SPOJ DQUERY D-query(主席树)
题目 Source http://www.spoj.com/problems/DQUERY/en/ Description Given a sequence of n numbers a1, a2, ...
- 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 ...
- 【填坑向】spoj COT/bzoj2588 Count on a tree
这题是学主席树的时候就想写的,,, 但是当时没写(懒) 现在来填坑 = =日常调半天lca(考虑以后背板) 主席树还是蛮好写的,但是代码出现重复,不太好,导致调试的时候心里没底(虽然事实证明主席树部分 ...
- SPOJ bsubstr
题目大意:给你一个长度为n的字符串,求出所有不同长度的字符串出现的最大次数. n<=250000 如:abaaa 输出: 4 2 1 1 1 spoj上的时限卡的太严,必须使用O(N)的算法那才 ...
- 【SPOJ 7258】Lexicographical Substring Search
http://www.spoj.com/problems/SUBLEX/ 好难啊. 建出后缀自动机,然后在后缀自动机的每个状态上记录通过这个状态能走到的不同子串的数量.该状态能走到的所有状态的f值的和 ...
- 【SPOJ 1812】Longest Common Substring II
http://www.spoj.com/problems/LCS2/ 这道题想了好久. 做法是对第一个串建后缀自动机,然后用后面的串去匹配它,并在走过的状态上记录走到这个状态时的最长距离.每匹配完一个 ...
- 【SPOJ 8222】Substrings
http://www.spoj.com/problems/NSUBSTR/ clj课件里的例题 用结构体+指针写完模板后发现要访问所有的节点,改成数组会更方便些..于是改成了数组... 这道题重点是求 ...
随机推荐
- 剑指offer--20.矩形覆盖
链接:https://www.nowcoder.com/questionTerminal/72a5a919508a4251859fb2cfb987a0e6来源:牛客网 @DanielLea 思路分析: ...
- poj-1426-Find The Multiple(打表水过)
思路: 2的最近可以整除的数是10 所以,很关键的一点,只要是偶数,例如: 6:2*3,可以拆分为能够被2整除和能够被3整除的乘积,所以,10*111=1110 144:72*2,8*9*2,2*2* ...
- BEC translation exercise 1
U.S. oil drillers have made major efficiency improvements with a speed that has repeatedly surprised ...
- SQL使用指南(2)—— 约束的使用
主键约束 (1)创建表时添加主键约束 primary key<column_name> (2) 修改表时添加主键约束 ALTER TABLE table_name ADD CONSTRAI ...
- 2017.10.7北京清北综合强化班DAY7
1.计数 (count.cpp/c/pas) 时间限制:1s 内存限制:256MB [问题描述] 给出m个数a[1],a[2],…,a[m] 求1~n中有多少数不是a[1],a[2],…,a[m]的倍 ...
- 【LeetCode】009. Palindrome Number
Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could negativ ...
- nodejs 安装 cnpm 命令
npm install -g cnpm --registry=https://registry.npm.taobao.org
- java程序员图文并茂细说Unity中调用Android的接口
http://bbs.csdn.net/topics/391876421 最近做一个项目,为同事提供接口,能使他在Unity中调用Android中的函数来实现QQ登陆并获取用户信息.按照一些书上和一些 ...
- bzoj 5210 最大连通子块和——动态DP
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=5210 似乎像bzoj4712一样,依然可以用别的方法做.但还是只写了动态DP. 当然是dp[ ...
- Python 算法之冒泡排序
冒泡排序 冒泡排序算法的原理如下:(从后往前) 1.比较相邻的元素.如果第一个比第二个大,就交换他们两个. 2.对每一对相邻元素作同样的工作,从开始第一对到结尾的最后一对.在这一点,最后的元素应该会是 ...