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的方格里,若只有一个是'*',那么它一定要 ...
随机推荐
- SDOI2016R1(不是解题报告)
话说洗澡的时候想了一堆要说的,坐到电脑前反而不知所措了-- 序章 听学长说他们都是省选一周前才停的课.然而我们这届--自聪哥韩大他们在省选两周前悄悄跑路后(据说班主任非常支持),信息小组内部一呼百应, ...
- 用NHibernate处理带属性的多对多关系
1.引言 老谭在面试开发者的时候,为了考察他们的数据库开发能力,经常祭出我的法宝,就是大学数据库教程中讲到的一个模式:学生选课.这个模式是这种: 在这个模式中,学生(Student)和课程(Cours ...
- mac的终端窗口的工作组的使用
1.打开终端,打开多个tab,分别进入目录, 2.点击窗口,将窗口存储为组,弹窗如下图 可以勾选恢复所有命令,存储 3.下次使用时,点击窗口,打开工作组即可
- 区分虚拟机和machine simulator
1 虚拟机和machine simulator的不同 虚拟机是让多个操作系统同时共用现有的硬件架构,它不会模拟新的硬件架构.qemu这样的模拟器是模拟新的硬件架构,这个架构和host不同.
- Mongoose Embedded Web Server Library
https://github.com/cesanta/mongoose http://ltp.ai/docs/ltpserver.html LTP Server在轻量级服务器程序mongoose基础上 ...
- 「翻译」Unity中的AssetBundle详解(一)
AssetBundles AssetBundle是一个存档文件,其中包含平台在运行时加载的特定资产(模型,纹理,预制,音频剪辑,甚至整个场景).AssetBundles可以表示彼此之间的依赖关系;例如 ...
- mysql优化---in型子查询,exists子查询,from 型子查询
in型子查询引出的陷阱:(扫更少的行,不要临时表,不要文件排序就快) 题: 在ecshop商城表中,查询6号栏目的商品, (注,6号是一个大栏目) 最直观的: mysql); 误区: 给我们的感觉是, ...
- Velocity模板引擎笔记
模板引擎中判断对象是否为空: #if(!${jsonObj.data.buyerName} || ${jsonObj.data.buyerName} == '') <p>采 ...
- POJ2914 Minimum Cut —— 最小割
题目链接:http://poj.org/problem?id=2914 Minimum Cut Time Limit: 10000MS Memory Limit: 65536K Total Sub ...
- poj 2531 Network Saboteur 解题报告
题目链接:http://poj.org/problem?id=2531 题目意思:将 n 个点分成两个部分A和B(也就是两个子集啦), 使得子集和最大(一定很难理解吧,呵呵).举个例子吧,对于样例,最 ...