要点

  • 题意:可以拐弯,即哈密顿距离
  • 注意不可以直接一个一个搜,这过程中会把下一轮的标记上,导致同一轮的其它点没能正常完成应有的搜索
  • 因此采用双层广搜,把同一轮先都出队列再的一起搜
#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. BZOJ 2101 [Usaco2010 Dec]Treasure Chest 藏宝箱:区间dp 博弈【两种表示方法】【压维】

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2101 题意: 共有n枚金币,第i枚金币的价值是w[i]. 把金币排成一条直线,Bessie ...

  2. hdu 6121 Build a tree

    /** * 题意:一棵 n 个点的完全 k 叉树,结点标号从 0 到 n - 1,求以每一棵子树的大小的异或和. * 解法:k叉树,当k=1时,特判,用xorn函数,具体解释:http://blog. ...

  3. python二进制数据

    一直以来对python的二进制数据搞不清楚. 一.二进制显示格式与实际存储值区别 1.二进制数据在python中以字节(bytes)类型和字节数组类型(bytearray)保存着,前者数据固定,后者不 ...

  4. codeforces 659G G. Fence Divercity(dp)

    题目链接: G. Fence Divercity time limit per test 2 seconds memory limit per test 256 megabytes input sta ...

  5. deep QA 基于生成的chatbot系统

    deep QA: https://github.com/Conchylicultor/DeepQA  基于论文:https://arxiv.org/pdf/1506.05869.pdf  基于生成的c ...

  6. android自定义控件(五) 自定义组合控件

    转自http://www.cnblogs.com/hdjjun/archive/2011/10/12/2209467.html 代码为自己编写 目标:实现textview和ImageButton组合, ...

  7. Codeforces Round #394 (Div. 2) 题解

    无需吟唱,直接传送 problem A 题目大意 已知有n个偶数,m个奇数,问这些数有没有可能组成一个严格递增1的序列 题解 判断abs(n,m) <= 1即可,注意n,m均为0的情况. Cod ...

  8. linux 中spfvim安装

    1. 安装 git 1.1 安装依赖的包: curl          curl-devel     zlib-devel         openssl-devel      perl      c ...

  9. 2012年浙大:Head of a Gang

    题目描述: One way that the police finds the head of a gang is to check people's phone calls. If there is ...

  10. Advanced R之编程风格

    转载请注明出处,谢谢.   编程风格指导 好的编码风格如同正确使用标点符号一样重要.没有编码规范仍然可以管理代码,但是有代码规范会使代码更易阅读.如同标点样式,编码规范也有不同.下面描述的是我所使用的 ...