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 ...
随机推荐
- 高性能MySQL之【第十六章MySQL用户工具】学习记录
接口工具: Msql Workbench http://www.mysql.com/products/workbench SQLyog http://www.webyog.c ...
- 【Lintcode】159.Find Minimum in Rotated Sorted Array
题目: Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 ...
- SVN版本控制详解
1 版本控制 1.1 如果没有版本控制? Team开发必备. 一个人开发(必备). 版本控制:控制(代码)版本. 论文:版本控制? 毕业论文-4-22.doc 毕业论文-5-01.doc 毕业论文-f ...
- tyvj1659救援队——最小生成树
题目:http://www.joyoi.cn/problem/tyvj-1659 想清楚了是非常简单的最小生成树: 1.树中每条边都会被走两边: 2.每个点会走度数遍,起点又多走一遍: 根据以上两条处 ...
- unreal Script(US)一些注意事项
转自:http://www.cnblogs.com/dongbo/archive/2012/07/10/2585311.html 1.如果计算量很大,考虑使用native Code来完成. 2.如果代 ...
- docker基于宿主机系统版本创建镜像
这里讲如何定制自己centos镜像,仅供测试docker使用. A) 安装软件 yum -y install febootstrap B)下载镜像febootstrap -i bash -i wget ...
- 六 Vue学习 首页 (下)
一:Store介绍: state: 相当于数据 action: action去commit mutations mutation: 只有mutation 才能改变state 例: const stor ...
- JSON 生成 C# Model
http://www.cnblogs.com/tianqiq/p/4309791.html
- hdu2732 Leapin' Lizards (网络流dinic)
D - Leapin' Lizards Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u ...
- 项目debug2
用户登录后,邮件发送失败?为什么呢? 密码得是,授权码,而不是,qq的密码.