Codeforces 1105D(双层广搜)
要点
- 题意:可以拐弯,即哈密顿距离
- 注意不可以直接一个一个搜,这过程中会把下一轮的标记上,导致同一轮的其它点没能正常完成应有的搜索
- 因此采用双层广搜,把同一轮先都出队列再的一起搜
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <cctype>
using namespace std;
const int maxp = 10;
const int tx[] = {0, 0, -1, 1};
const int ty[] = {-1, 1, 0, 0};
struct node {
int x, y, c;
node() {}
node(int a, int b, int c):x(a), y(b), c(c) {}
};
int n, m, p;
int pace[maxp];
char grid[1001][1001];
vector<node> st[maxp];
queue<node> Q;
queue<pair<node, int>> q;
int cnt[maxp];
int main() {
scanf("%d %d %d", &n, &m, &p);
for (int i = 1; i <= p; i++)
scanf("%d", &pace[i]);
for (int i = 1; i <= n; i++) {
scanf("%s", grid[i] + 1);
for (int j = 1; j <= m; j++)
if (isdigit(grid[i][j]))
st[grid[i][j] - '0'].emplace_back(i, j, grid[i][j] - '0');
}
for (int i = 1; i <= p; i++)
for (auto j : st[i])
Q.emplace(j);
while (!Q.empty()) {
auto t = Q.front(); Q.pop();
for (int dir = 0; dir < 4; dir++) {//本轮第一步
int nx = t.x + tx[dir], ny = t.y + ty[dir];
if (nx < 1 || nx > n || ny < 1 || ny > m || grid[nx][ny] != '.') continue;
grid[nx][ny] = t.c + '0';
q.emplace((node){nx, ny, t.c}, 1);
}
if (Q.size() && t.c == Q.front().c) continue;//同一轮的所有人一起扩展
while (!q.empty()) {//本轮扩展
node tmp = q.front().first;
int len = q.front().second;
q.pop();
if (len == pace[tmp.c]) {//本次最外界
Q.emplace(tmp); continue;
}
for (int dir = 0; dir < 4; dir++) {
int nx = tmp.x + tx[dir], ny = tmp.y + ty[dir];
if (nx < 1 || nx > n || ny < 1 || ny > m || grid[nx][ny] != '.') continue;
grid[nx][ny] = tmp.c + '0';
q.emplace((node){nx, ny, tmp.c}, len + 1);
}
}
}
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
if (isdigit(grid[i][j]))
cnt[grid[i][j] - '0']++;
for (int i = 1; i <= p; i++)
printf("%d ", cnt[i]);
}
Codeforces 1105D(双层广搜)的更多相关文章
- CodeForces 682C Alyona and the Tree(广搜 + 技巧)
方法:从根节点开始广搜,如果遇到了应该删除的点,就再广搜删掉它的子树并标记,然后统计一下被标记的个数就是答案,所谓技巧就是从根节点开始搜索的时候,如果遇到了某个节点的距离<0,就让它是0,0可以 ...
- CF520B——Two Buttons——————【广搜或找规律】
J - Two Buttons Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u Su ...
- HDU--杭电--1195--Open the Lock--深搜--都用双向广搜,弱爆了,看题了没?语文没过关吧?暴力深搜难道我会害羞?
这个题我看了,都是推荐的神马双向广搜,难道这个深搜你们都木有发现?还是特意留个机会给我装逼? Open the Lock Time Limit: 2000/1000 MS (Java/Others) ...
- HDU 5652(二分+广搜)
题目链接:http://acm.hust.edu.cn/vjudge/contest/128683#problem/E 题目大意:给定一只含有0和1的地图,0代表可以走的格子,1代表不能走的格 子.之 ...
- nyoj 613 免费馅饼 广搜
免费馅饼 时间限制:1000 ms | 内存限制:65535 KB 难度:3 描述 都说天上不会掉馅饼,但有一天gameboy正走在回家的小径上,忽然天上掉下大把大把的馅饼.说来gameboy ...
- poj 3984:迷宫问题(广搜,入门题)
迷宫问题 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7635 Accepted: 4474 Description ...
- poj 3278:Catch That Cow(简单一维广搜)
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 45648 Accepted: 14310 ...
- 双向广搜 POJ 3126 Prime Path
POJ 3126 Prime Path Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16204 Accepted ...
- 广搜+打表 POJ 1426 Find The Multiple
POJ 1426 Find The Multiple Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 25734 Ac ...
随机推荐
- js中的window.open返回object的错误
系统中用javascript中的window.open后,页面返回了一个[object].因为系统的原因,必需使用href="javascript:window.open()"这样 ...
- 草原psd素材
草原PSD素材,草原,风景,蓝天白云,飞鸟,阳光,绿色,草地. http://www.huiyi8.com/caoyuan/psd/
- 一小时搞明白自定义注解(Annotation)
原文链接:http://blog.csdn.net/u013045971/article/details/53433874 什么是注解 Annotation(注解)就是Java提供了一种元程序中的元素 ...
- 苹果AppStore如何申请加急审核
登录iTunesconnect,点击右上角的“?”图标,选择“联系我们”. iTunes Connect首页 依次选择“App Review”.“App Store Review” .” Reques ...
- 分享知识-快乐自己:Hibernate框架常用API详解
1):Configuration配置对象 Configuration用于加载配置文件. 1): 调用configure()方法,加载src下的hibernate.cfg.xml文件 Configura ...
- sql中使用timestamp增量抽取数据
网址:http://www.cnblogs.com/shuaifei/p/4469526.html 最近的项目中需要对上百万级的数据进行增量抽取操作,因此了解了一下TIMESTAMP的应用,特此记录 ...
- leetcode 19. Remove Nth Node From End of List(链表)
Given a linked list, remove the nth node from the end of list and return its head. For example, Give ...
- OpenCV——PS滤镜算法之 球面化 (凹陷效果)
// define head function #ifndef PS_ALGORITHM_H_INCLUDED #define PS_ALGORITHM_H_INCLUDED #include < ...
- mongodb入门-关系型RDMS数据库对比及适用场景
引言 最近工作接触到了mongodb数据库,记录下个人对其的理解和使用情况.虽然mongodb 出来的时间已经不短,但是相对mysql mssql oracle 这样传统的关系型数据库来说还是比较年轻 ...
- Set connectionId threw an exception.
今天调试一个WPF程序时,出现一个问题. 程序运行后抛出异常, "Set connectionId threw an exception. XXXXXXXXXX",原因是依赖的一个 ...