Gym 101981K bfs
思路:暴力让所有的骆驼和第一只骆驼合并,比如现在是第k只骆驼和第一只合并,广搜找出第k只骆驼如果想和第一只骆驼合并需要走哪一步,然后走一步,并更新所有骆驼的位置。
代码:
#include <bits/stdc++.h>
#define pii pair<int, int>
#define INF 0x3f3f3f3f
using namespace std;
const int maxn = 21;
char s[maxn][maxn];
int pre[maxn][maxn];
int dx[4] = {0, 1, 0, -1}, dy[4] = {-1, 0, 1, 0};
vector<pii> a;
vector<int> ans;
int n, m;
bool valid(pii x) {
return x.first >= 1 && x.first <= n && x.first >= 1 && x.first <= m && s[x.first][x.second] == '1';
}
int bfs(pii st, pii ed) {
memset(pre, -1, sizeof(pre));
pre[st.first][st.second] = INF;
queue<pii> q;
q.push(st);
while(!q.empty()) {
pii tmp = q.front();
q.pop();
if(tmp == ed) {
return pre[tmp.first][tmp.second];
}
for (int i = 0; i < 4; i++) {
int x = tmp.first + dx[i], y = tmp.second + dy[i];
if(!valid(make_pair(x, y)) || pre[x][y] != -1) continue;
q.push(make_pair(x, y));
pre[x][y] = (i + 2) % 4;
}
}
}
int main() {
char mp[4];
mp[0] = 'L', mp[1] = 'D', mp[2] = 'R', mp[3] = 'U';
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++) {
scanf("%s", s[i] + 1);
for (int j = 1; j <= m; j++)
if(s[i][j] == '1')
a.push_back(make_pair(i, j));
}
int pos = 1;
while(pos < a.size()) {
if(a[pos] == a[0]) {
pos++;
continue;
}
while(a[pos] != a[0]) {
int tmp = bfs(a[0], a[pos]);
ans.push_back(tmp);
for (int i = 0 ;i < a.size(); i++) {
int x = a[i].first + dx[tmp], y = a[i].second + dy[tmp];
if(valid(make_pair(x, y)))
a[i] = make_pair(x, y);
}
}
}
for (auto x : ans) {
printf("%c", mp[x]);
}
}
Gym 101981K bfs的更多相关文章
- Gym 101981K - Kangaroo Puzzle - [玄学][2018-2019 ACM-ICPC Asia Nanjing Regional Contest Problem K]
题目链接:http://codeforces.com/gym/101981/problem/K Your friend has made a computer video game called “K ...
- Gym - 101981K The 2018 ICPC Asia Nanjing Regional Contest K.Kangaroo Puzzle 暴力或随机
题面 题意:给你1个20*20的格子图,有的是障碍有的是怪,你可以每次指定上下左右的方向,然后所有怪都会向那个方向走, 如果2个怪撞上了,就融合在一起,让你给不超过5w步,让所有怪都融合 题解:我们可 ...
- ACM: Gym 101047E Escape from Ayutthaya - BFS
Gym 101047E Escape from Ayutthaya Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I6 ...
- Codeforces Gym 100187E E. Two Labyrinths bfs
E. Two Labyrinths Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100187/prob ...
- Codeforces gym 100685 F. Flood bfs
F. FloodTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100685/problem/F Desc ...
- Gym 100952F&&2015 HIAST Collegiate Programming Contest F. Contestants Ranking【BFS+STL乱搞(map+vector)+优先队列】
F. Contestants Ranking time limit per test:1 second memory limit per test:24 megabytes input:standar ...
- Gym 101617J Treasure Map(bfs暴力)
http://codeforces.com/gym/101617/attachments 题意:给出一个图,每个顶点代表一个金矿,每个金矿有g和d两个值,g代表金矿初始的金子量,d是该金矿每天的金子量 ...
- Gym - 100971J (思维+简单bfs)
题目链接:http://codeforces.com/gym/100971/problem/J J. Robots at Warehouse time limit per test 2.0 s mem ...
- Gym - 101147E E. Jumping —— bfs
题目链接:http://codeforces.com/gym/101147/problem/E 题意:当人在第i个商店时,他可以向左或向右跳di段距离到达另一个商店(在范围之内),一个商店为一段距离. ...
随机推荐
- mac OS 安装 Homebrew及常用命令
Homebrew 是由国外大神 Max Howell 开发的一款包管理工具,类似Debian的apt,他可以安装任何你想安装的东西. 安装方法 命令行输入 /usr/bin/ruby -e &quo ...
- java 深入剖析ThreadLocal
一.对ThreadLocal中的理解 ThreadLocal的,很多地方叫做线程本地变量,也有些地方叫做线程本地存储,其实意思差不多.可能很多朋友都知道的ThreadLocal为变量在每个线程中都创建 ...
- 2019-8-30-MSBuild-常用参数
title author date CreateTime categories MSBuild 常用参数 lindexi 2019-8-30 8:56:5 +0800 2019-07-20 21:56 ...
- The Preliminary Contest for ICPC Asia Xuzhou 2019 I J
I. query 题意:给出n的一个排列,有m个询问[l,r],询问[l,r]直接有倍数关系的pair个数. 解法:比赛完之后听说是原题,但是我没做过呀,做题太少了qwq.首先因为数字是1-n的,所以 ...
- bzoj 1176 cdq分治套树状数组
题面: 维护一个W*W的矩阵,初始值均为S.每次操作可以增加某格子的权值,或询问某子矩阵的总权值.修改操作数M<=160000,询问数Q<=10000,W<=2000000. Inp ...
- 【Kickstart】2017 Round (Practice ~ G)
Practice Round Problem A Country Leader (4pt/7pt) Problem B Vote (5pt/8pt) Problem C Sherlock and Pa ...
- sql 摘抄
练习题和参考解 (1)查询“001”课程比“002”课程成绩低的所有学生的学号.001学科成绩.002学科成绩 1 SELECT 2 s1.StudentNo, 3 s ...
- Struts2学习笔记 - namespace命名空间
默认的命名空间“ namespace="" ”. 根命名空间 “ namespace="/" ”. <package name="test&qu ...
- 前端解析Markdown
目录 前端解析Markdown 1.使用strapdown 1.1.下载 1.2.使用 2.使用marked(配合highlightjs) 2.1.下载 2.2.使用 3.使用mdjs(配合highl ...
- 深入了解 Flink 网络栈(二):监控、指标和处理背压
在之前的文章中,我们从高级抽象到底层细节各个层面全面介绍了 Flink 网络栈的工作机制.作为这一系列的第二篇文章,本文将在第一篇的基础上更进一步,主要探讨如何监视与网络相关的指标,从而识别背压等因素 ...