要点

  • 题意:可以拐弯,即哈密顿距离
  • 注意不可以直接一个一个搜,这过程中会把下一轮的标记上,导致同一轮的其它点没能正常完成应有的搜索
  • 因此采用双层广搜,把同一轮先都出队列再的一起搜
#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(双层广搜)的更多相关文章

  1. CodeForces 682C Alyona and the Tree(广搜 + 技巧)

    方法:从根节点开始广搜,如果遇到了应该删除的点,就再广搜删掉它的子树并标记,然后统计一下被标记的个数就是答案,所谓技巧就是从根节点开始搜索的时候,如果遇到了某个节点的距离<0,就让它是0,0可以 ...

  2. CF520B——Two Buttons——————【广搜或找规律】

    J - Two Buttons Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Su ...

  3. HDU--杭电--1195--Open the Lock--深搜--都用双向广搜,弱爆了,看题了没?语文没过关吧?暴力深搜难道我会害羞?

    这个题我看了,都是推荐的神马双向广搜,难道这个深搜你们都木有发现?还是特意留个机会给我装逼? Open the Lock Time Limit: 2000/1000 MS (Java/Others)  ...

  4. HDU 5652(二分+广搜)

    题目链接:http://acm.hust.edu.cn/vjudge/contest/128683#problem/E 题目大意:给定一只含有0和1的地图,0代表可以走的格子,1代表不能走的格 子.之 ...

  5. nyoj 613 免费馅饼 广搜

    免费馅饼 时间限制:1000 ms  |  内存限制:65535 KB 难度:3   描述 都说天上不会掉馅饼,但有一天gameboy正走在回家的小径上,忽然天上掉下大把大把的馅饼.说来gameboy ...

  6. poj 3984:迷宫问题(广搜,入门题)

    迷宫问题 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7635   Accepted: 4474 Description ...

  7. poj 3278:Catch That Cow(简单一维广搜)

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 45648   Accepted: 14310 ...

  8. 双向广搜 POJ 3126 Prime Path

      POJ 3126  Prime Path Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 16204   Accepted ...

  9. 广搜+打表 POJ 1426 Find The Multiple

    POJ 1426   Find The Multiple Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 25734   Ac ...

随机推荐

  1. jQuery Tab选项卡切换代码

    jQuery Tab选项卡切换代码是一款简单的jquery tab选项卡切换网页特效代码样式,可以修改tab选项卡相关样式. 代码下载:http://www.huiyi8.com/sc/10863.h ...

  2. mysql八:ORM框架SQLAlchemy

    阅读目录 一 介绍 二 创建表 三 增删改查 四 其他查询相关 五 正查.反查 一 介绍 SQLAlchemy是Python编程语言下的一款ORM框架,该框架建立在数据库API之上,使用关系对象映射进 ...

  3. rust borrow and move

    extern crate core; #[deriving(Show)] struct Foo { f : Box<int> } fn main(){ let mut a = Foo {f ...

  4. 0.5px的实现的几种方法

    方法一 通过css3缩放 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> ...

  5. 1139 First Contact(30 分)

    Unlike in nowadays, the way that boys and girls expressing their feelings of love was quite subtle i ...

  6. 「USACO08DEC」「LuoguP2921」在农场万圣节Trick or Treat on the Farm(tarjan

    题意翻译 题目描述 每年,在威斯康星州,奶牛们都会穿上衣服,收集农夫约翰在N(1<=N<=100,000)个牛棚隔间中留下的糖果,以此来庆祝美国秋天的万圣节. 由于牛棚不太大,FJ通过指定 ...

  7. 如何解决outlook2013邮件规则for other machine的失效问题

    如何解决outlook2013邮件规则for other machine的失效问题 问题描述:因为重装系统,outlook2013进去后->Rules and Alerts->发现所有原来 ...

  8. BZOJ3295:[CQOI2011]动态逆序对

    浅谈树状数组与线段树:https://www.cnblogs.com/AKMer/p/9946944.html 题目传送门:https://www.lydsy.com/JudgeOnline/prob ...

  9. DTP模型之一:(XA协议之三)MySQL数据库分布式事务XA优缺点与改进方案

    1 MySQL 外部XA分析 1.1 作用分析 MySQL数据库外部XA可以用在分布式数据库代理层,实现对MySQL数据库的分布式事务支持,例如开源的代理工具:ameoba[4],网易的DDB,淘宝的 ...

  10. mysql5.5换成mysql8.0

    由于在建表钟发现有些语句就是录不进去,研究发现是因为5.5版本过低导致,就想换到5.7版本,结果一看8.0都出了,据官方说明8.0要比5系列快2倍网上,遂直接换成8.0了,不过这个过程真的心累. 1. ...