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的方格里,若只有一个是'*',那么它一定要 ...
随机推荐
- 20170225-第三件事:FR0002测试
第三件事:FR0002测试 MATNR WERKS BERID 800000217 I010 问题,上for all entrys… 1 ...
- HDU 2444 The Accomodation of Students(判断二分图+最大匹配)
The Accomodation of Students Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ( ...
- Android设备adb授权的原理【转】
本文转载自:http://blog.csdn.net/zahuopuboss/article/details/50831171 http://blog.csdn.net/sowhat_ah/artic ...
- HDU4513 吉哥系列故事——完美队形II Manacher算法
题目链接:https://vjudge.net/problem/HDU-4513 吉哥系列故事——完美队形II Time Limit: 3000/1000 MS (Java/Others) Me ...
- HDU2295 Radar —— Dancing Links 可重复覆盖
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2295 Radar Time Limit: 2000/1000 MS (Java/Others) ...
- POJ1077 Eight —— 正向BFS
主页面:http://www.cnblogs.com/DOLFAMINGO/p/7538588.html 代码一:以数组充当队列,利用结构体中的pre追溯上一个状态在数组(队列)中的下标: #incl ...
- ap和路由器有什么区别 ap和路由器的区别介绍【图文】
现在能够摆脱网线限制能够自由方便上网的WiFi和无线网络也来越流行,很多酒店.饭店.宾馆.办公楼等地方都会提供无线网络.而能够提供无线网络的设备有很多,现在我们介绍的是无线ap和无线路由器.那么,ap ...
- CentOS7 安装和配置 mysql5.7
1.下载 mysql源安装包 wget https://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm 2.安装mysql源 ...
- html5--6-44信纸设计
html5--6-44信纸设计 实例 <!doctype html> <html> <head> <meta charset="utf-8" ...
- oracle:通过shell来运行rman命令
每次都手工输入一批rman命令来进行备份等操作是很繁琐的事,有什么简便的方法吗?可以的,你可以把这批rman命令写在 shell命令里面,需要的时候,运行一下sh即可.下面是一个简单的实例: RMAN ...