Jumping on Walls CodeForces - 198B
Jumping on Walls CodeForces - 198B
应该是一个隐式图的bfs,或者叫dp。
先是一个TLE的O(nklogn)
#include<cstdio>
#include<set>
using namespace std;
typedef pair<bool,int> P;
set<P> ss[];
P t;
char s[][];
int n,k,ii;
int main()
{
int i,num,hei;
scanf("%d%d",&n,&k);
scanf("%s",s[]+);
scanf("%s",s[]+);
if(s[][]!='X') ss[].insert(P(,));
for(i=;i<=n;i++)
{
ii^=;
ss[ii].clear();
for(auto t:ss[ii^])
{
num=t.first;
hei=t.second;
if(hei+k>n)
{
puts("YES");
return ;
}
if(hei->i&&s[num][hei-]!='X')
ss[ii].insert(P(num,hei-));
if(hei+>i&&s[num][hei+]!='X')
ss[ii].insert(P(num,hei+));
if(hei+k>i&&s[num^][hei+k]!='X')
ss[ii].insert(P(num^,hei+k));
}
}
puts("NO");
return ;
}
后来意识到了同样的位置,在较早的时间到过之后在较晚的时间再到那里一定不会比较早的时间更好,因此相同状态只需遍历一次,可以把复杂度优化到O(nlogn)(话说这不是显而易见吗,怎么就没有想到呢)
#include<cstdio>
#include<set>
using namespace std;
typedef pair<bool,int> P;
set<P> ss[];
P t;
char s[][];
int n,k,ii;
bool vis[][];
int main()
{
int i,num,hei;
scanf("%d%d",&n,&k);
scanf("%s",s[]+);
scanf("%s",s[]+);
if(s[][]!='X') ss[].insert(P(,));
for(i=;i<=n;i++)
{
ii^=;
ss[ii].clear();
for(auto t:ss[ii^])
{
num=t.first;
hei=t.second;
if(hei+k>n)
{
puts("YES");
return ;
}
if(hei->i&&s[num][hei-]!='X'&&(!vis[num][hei-]))
ss[ii].insert(P(num,hei-)),vis[num][hei-]=;
if(hei+>i&&s[num][hei+]!='X'&&(!vis[num][hei+]))
ss[ii].insert(P(num,hei+)),vis[num][hei+]=;
if(hei+k>i&&s[num^][hei+k]!='X'&&(!vis[num^][hei+k]))
ss[ii].insert(P(num^,hei+k)),vis[num^][hei+k]=;
}
}
puts("NO");
return ;
}
upd20190310:
啧,貌似我以前假了。直接ans[i][j]表示到达i墙j位置的最小时间,如果在这个时间水位已经没过这个点了那么这个点就是废的,不要从这个点去更新其他点
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
#include<queue>
using namespace std;
#define fi first
#define se second
#define mp make_pair
#define pb push_back
typedef long long ll;
typedef unsigned long long ull;
struct P
{
int x,y;
};
queue<P> q;
int n,K;
int ans[][];
char s[][];
char v1[][];
int main()
{
P t;int x,y;
scanf("%d%d",&n,&K);
scanf("%s",s[]+);
scanf("%s",s[]+);
memset(ans,0x3f,sizeof(ans));
ans[][]=;v1[][]=;q.push((P){,});
while(!q.empty())
{
t=q.front();q.pop();
x=t.x;y=t.y;
if(y<=ans[x][y]) continue;
if(s[x][y]=='X') continue;
if(y+>n || y+K>n)
{
puts("YES");
return ;
}
if(y> && !v1[x][y-])
{
v1[x][y-]=;ans[x][y-]=ans[x][y]+;
q.push((P){x,y-});
}
if(!v1[x][y+])
{
v1[x][y+]=;ans[x][y+]=ans[x][y]+;
q.push((P){x,y+});
}
if(!v1[x^][y+K])
{
v1[x^][y+K]=;ans[x^][y+K]=ans[x][y]+;
q.push((P){x^,y+K});
}
}
puts("NO");
return ;
}
Jumping on Walls CodeForces - 198B的更多相关文章
- [BFS,大水题] Codeforces 198B Jumping on Walls
题目:http://codeforces.com/problemset/problem/198/B Jumping on Walls time limit per test 2 seconds mem ...
- Arthur and Walls CodeForces - 525D (bfs)
大意: 给定格点图, 每个'.'的连通块会扩散为矩形, 求最后图案. 一开始想得是直接并查集合并然后差分, 但实际上是不对的, 这个数据就可以hack掉. 3 3 **. .** ... 正解是bfs ...
- Codeforces Round #125 (Div. 2)
A. Hexadecimal's theorem 三个数没有限制,直接输出\(0\ 0\ n\). B. Special Olympics 分包含和外离情况,包含分2种情况. C. About Bac ...
- Codeforces Round #290 (Div. 2) D. Fox And Jumping dp
D. Fox And Jumping 题目连接: http://codeforces.com/contest/510/problem/D Description Fox Ciel is playing ...
- CodeForces 471D MUH and Cube Walls -KMP
Polar bears Menshykov and Uslada from the zoo of St. Petersburg and elephant Horace from the zoo of ...
- Codeforces Beta Round #11 B. Jumping Jack 数学
B. Jumping Jack 题目连接: http://www.codeforces.com/contest/11/problem/B Description Jack is working on ...
- Codeforces Round #297 (Div. 2)D. Arthur and Walls 暴力搜索
Codeforces Round #297 (Div. 2)D. Arthur and Walls Time Limit: 2 Sec Memory Limit: 512 MBSubmit: xxx ...
- Codeforces Round #269 (Div. 2) D - MUH and Cube Walls kmp
D - MUH and Cube Walls Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & % ...
- BFS Codeforces Round #297 (Div. 2) D. Arthur and Walls
题目传送门 /* 题意:问最少替换'*'为'.',使得'.'连通的都是矩形 BFS:搜索想法很奇妙,先把'.'的入队,然后对于每个'.'八个方向寻找 在2*2的方格里,若只有一个是'*',那么它一定要 ...
随机推荐
- MYSQL强制使用索引和禁止使用索引
mysql强制索引和禁止某个索引 1.mysql强制使用索引:force index(索引名或者主键PRI) 例如: select * from table force index(PRI) limi ...
- windows下在eclipse上远程连接hadoop集群调试mapreduce错误记录
第一次跑mapreduce,记录遇到的几个问题,hadoop集群是CDH版本的,但我windows本地的jar包是直接用hadoop2.6.0的版本,并没有特意找CDH版本的 1.Exception ...
- 序列化模块 json pickel shelve
一.json 模块 1.定义 将字典.列表等内容转换成字符串的过程就是序列化. 操作的数据类型有限,但是可以支持所有编程语言操作. 2.为什么要有序列化? 1.以某种存储形式使自定义对象持久化. 2 ...
- jvm 调优(2)垃圾回收算法
可以从不同的的角度去划分垃圾回收算法: 按照基本回收策略分 引用计数(Reference Counting): 比较古老的回收算法.原理是此对象有一个引用,即增加一个计数,删除一个引用则减少一个计数. ...
- Hadoop MapReduce基本原理
一.什么是: MapReduce是一种编程模型,用于大规模数据集(大于1TB)的并行运算.概念"Map(映射)"和"Reduce(归约)",是它们的主要思想,都 ...
- FZU2150 Fire Game —— BFS
题目链接:https://vjudge.net/problem/FZU-2150 Problem 2150 Fire Game Accept: 2702 Submit: 9240 Time Li ...
- [置顶] SQL Server 2005 双机热备的实现
[置顶] SQL Server 2005 双机热备的实现 分类: SQLSERVER2011-08-24 21:25 901人阅读 评论(0) 收藏 举报 sql servermicrosoftsql ...
- Linux查看当前在线用户信息
Linux是多用户系统,支持同时登陆多个用户,在终端中用"w"命令可以查看当前的在线用户,以及每个用户正在执行的进程: 第一行显示的字段信息分别是: 12:16:49:系统当前时间 ...
- iOS成员变量、实例变量、属性变量三者的联系与区别
一.类Class中的属性property 在ios第一版中: 我们为输出口同时声明了属性和底层实例变量,那时,属性是oc语言的一个新的机制,并且要求你必须声明与之对应的实例变量,例如: 注意:(这个是 ...
- [USACO2007 Demo] Cow Acrobats
[题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=1629 [算法] 贪心 考虑两头相邻的牛 , 它们的高度值和力量值分别为ax , ay ...