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. Linux MySQL主从复制(Replication)配置

    MySQL是开源的关系型数据库系统.复制(Replication)是从一台MySQL数据库服务器(主服务器master)复制数据到另一个服务器(从服务器slave)的一个进程. 配置主服务器(mast ...

  2. visual studio 2013 update 3正式版出来了

    微软的更新速度还是蛮快的吗.新版本号出来了,大家快下载体验一下吧,详细下载地址在http://www.visualstudio.com/zh-cn/downloads/download-visual- ...

  3. poj 2264 Advanced Fruits(DP)

    Advanced Fruits Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 1944   Accepted: 967   ...

  4. 字符串函数---strcat()与strncat具体解释及实现

    一.strcat()与strncat() strcat():strcat(dest,src);        strcat把src所指向的字符加入到dest结尾处(覆盖原dest结尾处的'\0').并 ...

  5. Storm项目:流数据监控1《设计文档…

    博客公告: (1)本博客全部博客文章搬迁至<博客虫>http://blogchong.com/ (2)文章相应的源代码下载链接參考博客虫站点首页的"代码GIT". (3 ...

  6. SpringInAction4笔记——web

    1,java配置 extends AbstractAnnotationConfigDispatcherServletInitializer public class SpitterWebInitial ...

  7. Delphi通过POST传递参数给PHP

    Delphi代码 ******************************************************************************************* ...

  8. dedecms5.7二级域名文章图片不显示修改方法.相对路径改为绝对路径的方法

    dedecms5.7(织梦CMS5.7)二级域名文章图片不显示修改方法.相对路径改为绝对路径的方法 dedecms升级到5.7SP1后,开启二级域名,你会发现,在二级域名下的文章,上传的图片地址都是: ...

  9. 用于JS日期格式化,以及简单运算的Date包装工具类

    1. [文件] yDate.js/** * | yDate.js | Copyright (c) 2013 yao.yl | email: redrainyi@126.com | Date: 2012 ...

  10. 西交校赛 F. GZP and Poker

    F. GZP and Poker GZP often plays games with his friends.Today they went to a board game.There are n ...