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的更多相关文章

  1. [BFS,大水题] Codeforces 198B Jumping on Walls

    题目:http://codeforces.com/problemset/problem/198/B Jumping on Walls time limit per test 2 seconds mem ...

  2. Arthur and Walls CodeForces - 525D (bfs)

    大意: 给定格点图, 每个'.'的连通块会扩散为矩形, 求最后图案. 一开始想得是直接并查集合并然后差分, 但实际上是不对的, 这个数据就可以hack掉. 3 3 **. .** ... 正解是bfs ...

  3. Codeforces Round #125 (Div. 2)

    A. Hexadecimal's theorem 三个数没有限制,直接输出\(0\ 0\ n\). B. Special Olympics 分包含和外离情况,包含分2种情况. C. About Bac ...

  4. 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 ...

  5. 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 ...

  6. Codeforces Beta Round #11 B. Jumping Jack 数学

    B. Jumping Jack 题目连接: http://www.codeforces.com/contest/11/problem/B Description Jack is working on ...

  7. 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 ...

  8. 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 & % ...

  9. BFS Codeforces Round #297 (Div. 2) D. Arthur and Walls

    题目传送门 /* 题意:问最少替换'*'为'.',使得'.'连通的都是矩形 BFS:搜索想法很奇妙,先把'.'的入队,然后对于每个'.'八个方向寻找 在2*2的方格里,若只有一个是'*',那么它一定要 ...

随机推荐

  1. 按行读入xml文件,删除不需要的行 -Java

    删除挺麻烦的,这里其实只是把需要的行存到arraylist中再存到另一个文件中 import java.io.BufferedReader;import java.io.BufferedWriter; ...

  2. mysql_proxy

    mysql_proxy中间件实现:读写分离.负载均衡. mysql_proxy中间件实现:读写分离.负载均衡. 负载均衡:给多台数据库,看能不能均匀的分给不同的数据库. 客户端连的是proxy,此时的 ...

  3. Mysql常见函数

    一.单行函数 1.字符函数 concat拼接 substr截取子串 upper转换成大写 lower转换成小写 trim去前后指定的空格和字符 ltrim去左边空格 rtrim去右边空格 replac ...

  4. 用Delphi7 调用.NET 2.0的WebService 所要注意的问题(Document格式和UTF8编码)

    Delphi7 调用VS.NET 2005开发的基于.NET 2.0的WebService时发生了错误.查阅资料 http://www.community.borland.com/article/bo ...

  5. ios很好的开源库

    Tim9Liu9/TimLiu-iOS 自己总结的iOS.mac开源项目及库,持续更新.. 目录 UI 下拉刷新 模糊效果 AutoLayout 富文本 图表 表相关与Tabbar 隐藏与显示 HUD ...

  6. [FAQ04776]如何默认打开user版本 debug 选项, 默认打开adb 连接【转】

    本文转载自:http://blog.csdn.net/thinkinwm/article/details/24865933 Description] 如何默认打开user 版本的USB debug 选 ...

  7. Oracle:通过dbv查看数据文件是否有坏块

    我们备份的数据文件,可以通过oacle自带的dbv工具来查看是否是好的. 下面实验如下: 环境:oracle10.2.0.1 1.检查数据文件是否有坏块 [oracle@app orcl]$ dbv ...

  8. hdu2552

    点击打开链接 思路: 1.tan(a+b) = ( tan(a) + tan(b) ) / (1 – tan(a) * tan(b) ) 2.tan( atan(x) ) = x arctan(1/s ...

  9. FileInputStream和FileReader

    这两个类都可以读入数据到缓冲区,FileInputStream在传递到buffer的时候要用byte定义buffer,不然报错.比如: byte [] buffer = new byte[100]; ...

  10. Unable to instantiate receiver XXXXXX

    运行一个工程的时候时logcat中出现了“Unable to instantiate receiver XX..”. 检查后发现,由于是东拼西凑的代码,所以在Manifest文件里注册了Receive ...