Codeforces Round #325 (Div. 2) D. Phillip and Trains (BFS)
来源:互联网 发布:淘宝会员无线端装修 编辑:程序博客网 时间:2024/05/22 21:23
一开始看这题,还以为是DP,后来写完记忆化搜索之后发现结果怎么也不对,仔细一想,无论时间复杂度还是状态转移好像都不想DP。 后来想了一下,发现应该是BFS(DFS的盲目性太大,而BFS则可以在最早的时刻剪枝)。 所以我们不妨先用一个数组记录下所有列车在各个时间点的状态,然后用来在BFS中做判断。
细节参见代码:
#include<bits/stdc++.h>using namespace std;typedef long long ll;const int mod = 1 + 7;const int INF = 1000000000;const int maxn = 120;int n,m,T,k,len,t[maxn][5][maxn],d[5][maxn];char s[5][maxn];struct cars{ int rol,l,r; cars(int rol, int l=0, int r=0):rol(rol), l(l), r(r) {}};struct node{ int r,c; node(int r=0, int c=0):r(r),c(c) {}}S;int dx[] = {-1,0,1};bool bfs(node S) { queue<node> q; memset(d,-1,sizeof(d)); d[S.r][S.c] = 0; q.push(S); while(!q.empty()) { node u = q.front(); q.pop(); if(u.c >= n) return true; for(int i=0;i<3;i++) { int x = u.r + dx[i], y = u.c + 1; if(x < 1 || x > 3) continue; int tt = d[u.r][u.c] + 1; if(d[x][y] == -1 && !t[tt][x][y] && !t[tt-1][u.r][y] &&!t[tt-1][x][y]) { d[x][y] = tt; q.push(node(x,y)); } } } return false;}int main() { scanf("%d",&T); while(T--) { scanf("%d%d",&n,&k); memset(t,0,sizeof(t)); for(int i=1;i<=3;i++) scanf("%s",s[i]+1); for(int i=1;i<=3;i++) for(int j=1;j<=n;j++) { if(s[i][j] == '.') continue; if(s[i][j] == 's') { S = node(i,j); break; } } for(int i=1;i<=3;i++) { //计算各个列车在各个时刻的状态 for(int j=1;j<=n;j++) { if(s[i][j] != '.' && s[i][j] != 's') { int l = j, r ; for(int k=j+1;k<=n;k++) { if(s[i][k] == '.') { j = k-1; r = k-1; break; } if(k == n) { j = k ; r = k; break; } } int cnt = 0; while(true) { for(int k=(l < 1 ? 1 : l); k<=r; k++) { t[cnt][i][k] = 1; } cnt++; l -= 2; r -= 2; if(r < 1) break; } } } } if(bfs(S)) printf("YES\n"); else printf("NO\n"); } return 0;}
0 0
- Codeforces Round #325 (Div. 2) D. Phillip and Trains (BFS)
- 【Codeforces Round #325 (Div. 2)】D. Phillip and Trains
- Codeforces Round #325 (Div. 2) D. Phillip and Trains
- Codeforces Round #325 (Div. 2)-D. Phillip and Trains
- Codeforces #325 D. Phillip and Trains (bfs)
- Codeforces 586D Phillip and Trains dfs —— Round #325 (Div. 2)
- CodeForces #325 D. Phillip and Trains
- Codeforces #325 Div2 D Phillip and Trains(模拟)
- Codeforces 586D Phillip and Trains【思维+Bfs】好题~!
- codeforces 585B Phillip and Trains(bfs)
- CF Round #325 Div2 D. Phillip and Trains
- CodeForces 586D Phillip and Trains (基础搜索 -- DFS)
- CodeForces 586D Phillip and Trains(搜索)
- 【33.33%】【codeforces 586D】Phillip and Trains
- Codeforces 586D Phillip and Trains
- Codeforces 586D Phillip and Trains
- Codeforces 586D Phillip and Trains 题解
- CF 585B Phillip and Trains(预处理+BFS)
- 简单常用的注解
- 类和对象
- linux自动编译的makefile
- RabbitMq应用在发送短信
- PHP cURL抓取华师学生个人课表
- Codeforces Round #325 (Div. 2) D. Phillip and Trains (BFS)
- C#的哈希
- 循环获取文件夹中子文件夹中的文件
- Leetcode101: Kth Smallest Element in a BST
- cas单点登录系统多个系统容易超时
- 适配
- Android循序渐进 - 3.4 TouchListener PK OnTouchEvent + 多点触碰
- E-Mail notifications in MS Dynamics AX 2012
- Nginx原理简介与 搭建反向代理服务器过程详解