C - 小明系列故事――捉迷藏 HDU - 4528 bfs +状压 旅游-- 最短路+状压
C - 小明系列故事――捉迷藏
这个题目看了一下题解,感觉没有很难,应该是可以自己敲出来的,感觉自己好蠢。。。
这个是一个bfs 用bfs就很好写了,首先可以预处理出大明和二明能被发现的位置,标记一下。
然后跑bfs,注意这个bfs记录一下状态,记录一下是否看到了大明和二明。
这个题目和之前写的旅游这个题目很像,所以还是很好写的
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <vector>
#include <iostream>
#include <string>
#define inf 0x3f3f3f3f
#define inf64 0x3f3f3f3f3f3f3f3f
using namespace std;
const int maxn = 1e5 + ;
typedef long long ll;
int n, m, t;
char s[][];
bool flaga[][], flagb[][]; void checka(int x,int y)
{
int i = x, j = y;
while (s[i][j] != 'X'&&i >= && s[i][j] != 'E') flaga[i][j] = , i--;
i = x;
while (s[i][j] != 'X'&&i <= n && s[i][j] != 'E') flaga[i][j] = , i++;
i = x;
while (s[i][j] != 'X'&&j <= m && s[i][j] != 'E') flaga[i][j] = , j++;
j = y;
while (s[i][j] != 'X'&&j >= && s[i][j] != 'E') flaga[i][j] = , j--;
} void checkb(int x,int y)
{
int i = x, j = y;
while (s[i][j] != 'X'&&i >= && s[i][j] != 'D') flagb[i][j] = , i--;
i = x;
while (s[i][j] != 'X'&&i <= n && s[i][j] != 'D') flagb[i][j] = , i++;
i = x;
while (s[i][j] != 'X'&&j <= m && s[i][j] != 'D') flagb[i][j] = , j++;
j = y;
while (s[i][j] != 'X'&&j >= && s[i][j] != 'D') flagb[i][j] = , j--;
} int dx[] = { ,,-, };
int dy[] = { ,,,- }; struct node
{
int x, y, now;
node(int x=,int y=,int now=):x(x),y(y),now(now){}
};
int dp[][][];
bool vis[][][];
int bfs(int sx,int sy)
{
int ans = inf;
queue<node>que;
int tmp = ;
memset(vis, , sizeof(vis));
if (flaga[sx][sy]) tmp |= ( << );
if (flagb[sx][sy]) tmp |= ( << );
dp[sx][sy][tmp] = ;
que.push(node(sx, sy, tmp));
while(!que.empty())
{
node u = que.front(); que.pop();
int x = u.x, y = u.y, now = u.now;
if (now == )ans = min(ans, dp[x][y][now]);
for(int i=;i<;i++)
{
int tx = x + dx[i];
int ty = y + dy[i];
int tmp1 = now;
if (tx< || ty< || tx>n || ty>m) continue;
if (s[tx][ty] == 'X') continue;
if (s[tx][ty] == 'E') continue;
if (s[tx][ty] == 'D') continue;
if (flaga[tx][ty]) tmp1 |= ( << );
if (flagb[tx][ty]) tmp1 |= ( << ); if (vis[tx][ty][tmp1]) continue;
vis[tx][ty][tmp1] = ;
dp[tx][ty][tmp1] = dp[x][y][now] + ;
que.push(node(tx, ty, tmp1));
}
}
return ans;
} int main()
{
int tim;
scanf("%d", &tim);
for(int cas=;cas<=tim;cas++)
{
int sx = , sy = ;
scanf("%d%d%d", &n, &m, &t);
memset(flaga, , sizeof(flaga));
memset(flagb, , sizeof(flagb));
for (int i = ; i <= n; i++) {
scanf("%s", s[i] + );
}
for (int i = ; i <= n; i++) {
for (int j = ; j <= m; j++) {
if (s[i][j] == 'D') checka(i, j);
if (s[i][j] == 'E') checkb(i, j);
if (s[i][j] == 'S') sx = i, sy = j;
}
}
int ans=bfs(sx, sy);
printf("Case %d:\n", cas);
if (ans <= t) printf("%d\n", ans);
else printf("-1\n");
}
return ;
}
这个题目大意是:
db爱好运动,但是单纯的运动会使得他很枯燥,现在他想边跑步边看风景。已知现在有n个风景点(编号为1号~n号),同时有m条道路将这n个风景点连接起来。
这些风景点总共有3类:A,B,C;为了方便表示,我们令 A=0,B=1,C=2。db一开始在1号风景点(可以为A,B,C类)。现在db想在跑步的过程中经过至少一个B类风景点的同时至少经过一个C类风景点,最后再回到1号风景点。现在db想要在尽可能短的时间内跑步结束,你能帮他找出一条路程最短同时满足题目条件的路径吗?
这个就是一个最短路的时候记录状态即可。
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <string>
#include <cstring>
#include <queue>
#include <vector>
#define inf 0x3f3f3f3f
#define inf64 0x3f3f3f3f3f3f3f3f
using namespace std;
const int maxn = 4e5 + , sum = ( << );
typedef long long ll;
ll d[maxn][ << ];
int n, m, num[maxn];
bool vis[maxn][ << ];
struct edge {
int from, to;ll dist;
edge(int from=, int to=, ll dist=) :from(from), to(to), dist(dist) {}
};
struct heapnode {
int u, tmp; ll d;
heapnode(ll d = , int u = ,int tmp = ) : d(d), u(u), tmp(tmp) {}
bool operator<(const heapnode &a) const {
return a.d < d;
}
}; vector<edge> e;
vector<int>G[maxn]; void add(int u,int v,ll w)
{
e.push_back(edge(u, v, w));
e.push_back(edge(v, u, w));
int len = e.size();
G[u].push_back(len - );
G[v].push_back(len - );
}
ll ans = ;
void dijkstra(int s) {
priority_queue<heapnode>que;
for(int i=;i<=n;i++) for (int j = ; j < sum; j++) d[i][j] = inf64;
memset(vis, , sizeof(vis));
que.push(heapnode(, s, << num[s]));
d[s][<<num[s]] = ;
while (!que.empty()) {
heapnode x = que.top(); que.pop();
int u = x.u, tmp1 = x.tmp;
if (vis[u][tmp1]) continue;
vis[u][tmp1] = ;
if (tmp1 >= && u == ) ans = min(ans, x.d);
for(int j=;j<G[u].size();j++)
{
edge now = e[G[u][j]];
int v = now.to;
int tmp = << num[v];
tmp |= tmp1;
if(d[v][tmp] >x.d + now.dist &&!vis[v][tmp])
{
d[v][tmp] = x.d + now.dist;
que.push(heapnode(d[v][tmp], v, tmp));
}
}
}
}
int main()
{
scanf("%d%d", &n, &m);
for (int i = ; i <= n; i++) scanf("%d", &num[i]);
while(m--)
{
int u, v; ll w;
scanf("%d%d%lld", &u, &v, &w);
add(u, v, w);
}
ans = inf64;
dijkstra();
printf("%lld\n", ans);
return ;
}
C - 小明系列故事――捉迷藏 HDU - 4528 bfs +状压 旅游-- 最短路+状压的更多相关文章
- HDU 4528 BFS 小明系列故事——捉迷藏
原题直通车:HDU 4528 小明系列故事——捉迷藏 分析: 标记时加两种状态就行. 代码: #include<iostream> #include<cstring> #inc ...
- HDU 4828 小明系列故事——捉迷藏
漂亮妹子点击就送:http://acm.hdu.edu.cn/showproblem.php?pid=4528 Time Limit: 500/200 MS (Java/Others) Memo ...
- hdu4528 小明系列故事——捉迷藏
Time Limit: 500/200 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others) Total Submission(s ...
- HDU 4528 小明系列故事――捉迷藏
广搜. 根据题意,可以知道状态总共有$4*n*m$种.每一个位置四种状态:两个都没有发现:发现$E$没发现$D$:发现$D$没发现$E$:两个都发现. 每次移动的花费都是$1$,队列里面状态的费用是单 ...
- HDU-4528 小明系列故事——捉迷藏 BFS模拟
题意:链接 分析:每一个D或者是E点往四面延伸,并且赋一个特殊的值,能看到D点的点赋值为1,能看到E点的点赋值为1000,这是因为最多100步,因此最后可以根据除以1000和对1000取模来得出某个状 ...
- hdu4528 小明系列故事——捉迷藏(记录状态的BFS)题解
思路: 一道BFS题,和以前的BFS有点不同,这里的vis数组需要记录每次走时的状态,所以开了3维,只对该状态下的vis修改. 注意坑点:S的位置是可以走的 代码: #include<queue ...
- HDU-4511 小明系列故事——女友的考验 floyd变种-标号递增最短路
题意:给定N个点,现在要求出从1号点到N号点的最短路.题目给的限制条件就是对于某条路径是不能够走的,但是可以选择某段路径走,另外就是所走的路径的标号必须是递增的. 分析:由于给定的是一些列的坐标点,这 ...
- hdu 4506 小明系列故事——师兄帮帮忙【幂取模乱搞】
链接: http://acm.hdu.edu.cn/showproblem.php?pid=4506 http://acm.hust.edu.cn/vjudge/contest/view.action ...
- hdu 4542 小明系列故事——未知剩余系
小明系列故事——未知剩余系 题意:操作0表示某数有n个约数,操作1为某数有n个非约数:n <= 47777,若是存在小于2^62的数符合,则输出该数,否则若是不存在输出Illegal,若是大于2 ...
随机推荐
- std::string::assign函数
string& assign (const string& str); string& assign (const string& str, size_t subpos ...
- synchronized 的真正含义
@synchronized 锁的永远是对象 ,只针对于对象,只能锁对象,常量等是不能加synchronized,一旦加编译也不会通过 @synchronized 锁对象中的非static 就是锁调用该 ...
- 王者荣耀英雄全皮肤4K高清大图,python爬虫帮你保存下来
前言 文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理. PS:如有需要Python学习资料的小伙伴可以加点击下方链接自行获取t.cn ...
- ppt和pptx转图片完整代码,解决2003版和2007版中文乱码问题
引入所需依赖,注意poi版本,新版本不支持,最好使用和我一样的版本. <!-- https://mvnrepository.com/artifact/org.apache.poi/poi --& ...
- icepdf和pdfbox转pdf文档为图片
icepdf转pdf文档为图片 首先导入icepdf jar包或maven pdfPath为pdf文件路径.pdfimgpsth为图片保存的路径 public static void icePdfIm ...
- 跑Linux内存占用率的shell脚本
#!/bin/bash ################################################################ # Mem Used Script # eg. ...
- 关于Python+selenium 定位浏览器弹窗元素
首先要确定弹窗的类型: (1)div弹窗 (2)新标签页弹窗 (3)alert弹窗 一,div弹窗div弹窗是浏览器中比较好定位的弹窗,定位的方法与普通的元素一样.不过这里会有一个坑,明明可以找到这个 ...
- 今天我们来谈谈jquery,
---恢复内容开始--- 首先从jquery的两种写法开始: 1.$(document).ready(function(){}); 首先我们的jquery是用来操作DOM节点的,所以必须等到文档加载完 ...
- sftp的用法
linux sftp远程连接命令 sftp -oPort=60001 root@192.168.0.254 使用-o选项来指定端口号. -oPort=远程端口号 sftp> get /var/w ...
- 常见分布式全局唯一ID生成策略
全局唯一的 ID 几乎是所有系统都会遇到的刚需.这个 id 在搜索, 存储数据, 加快检索速度 等等很多方面都有着重要的意义.工业上有多种策略来获取这个全局唯一的id,针对常见的几种场景,我在这里进行 ...