思路:暴力让所有的骆驼和第一只骆驼合并,比如现在是第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的更多相关文章

  1. 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 ...

  2. Gym - 101981K The 2018 ICPC Asia Nanjing Regional Contest K.Kangaroo Puzzle 暴力或随机

    题面 题意:给你1个20*20的格子图,有的是障碍有的是怪,你可以每次指定上下左右的方向,然后所有怪都会向那个方向走, 如果2个怪撞上了,就融合在一起,让你给不超过5w步,让所有怪都融合 题解:我们可 ...

  3. ACM: Gym 101047E Escape from Ayutthaya - BFS

    Gym 101047E Escape from Ayutthaya Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I6 ...

  4. Codeforces Gym 100187E E. Two Labyrinths bfs

    E. Two Labyrinths Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100187/prob ...

  5. Codeforces gym 100685 F. Flood bfs

    F. FloodTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100685/problem/F Desc ...

  6. 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 ...

  7. Gym 101617J Treasure Map(bfs暴力)

    http://codeforces.com/gym/101617/attachments 题意:给出一个图,每个顶点代表一个金矿,每个金矿有g和d两个值,g代表金矿初始的金子量,d是该金矿每天的金子量 ...

  8. Gym - 100971J (思维+简单bfs)

    题目链接:http://codeforces.com/gym/100971/problem/J J. Robots at Warehouse time limit per test 2.0 s mem ...

  9. Gym - 101147E E. Jumping —— bfs

    题目链接:http://codeforces.com/gym/101147/problem/E 题意:当人在第i个商店时,他可以向左或向右跳di段距离到达另一个商店(在范围之内),一个商店为一段距离. ...

随机推荐

  1. JS设置首字母大写算法

    返回一个字符串,确保字符串的每个单词首字母都大写,其余部分小写. 像'the'和'of'这样的连接符同理. function titleCase(str) { //把字符串所有的字母变为小写,并根据空 ...

  2. 软件安装 RPM SRPM YUM

    RPM介绍 RPM是已经编译好的软件安装库.编译是有相应环境相适应的,包括系统,版本等相关信息都要跟编译版本一致才行,否则肯定会出现安装不成功的情况,强制安装的话,也会出现各种各样的问题. 在这种情况 ...

  3. 容斥原理+补集转化+MinMax容斥

    容斥原理的思想大家都应该挺熟悉的,然后补集转化其实就是容斥原理的一种应用. 一篇讲容斥的博文https://www.cnblogs.com/gzy-cjoier/p/9686787.html 当我们遇 ...

  4. mocha.js

    mocha 如果你听说过“测试驱动开发”(TDD:Test-Driven Development),单元测试就不陌生. 单元测试是用来对一个模块.一个函数或者一个类来进行正确性检验的测试工作. 比如对 ...

  5. mqtt发送消息,消息体为16进制

    在idea 加入代码  :-server -XX:PermSize=256M -XX:MaxPermSize=256M -Dfile.encoding=UTF-8

  6. windows 安装yaml支持和pytest支持等

    打开cmd 输入pip install pyyaml    #yaml文件支持 输入pip install pytest     #pytest框架支持 输入pip install requests  ...

  7. ruby之基础语法

    ruby语法之哈希 =>相当于python的字典 ruby语法之数组 =>相当于python的列表 举例: gitaly= Hash.new #建立新Hash类型 gitaly['firs ...

  8. Hashtable、HashMap、TreeMap、ConcurrentHashMap、ConcurrentSkipListMap区别

    原创转载请注明出处:https://www.cnblogs.com/agilestyle/p/11444013.html 并发场景下的Map容器使用场景 如果对数据有强一致要求,则需使用Hashtab ...

  9. 【leetcode】1006. Clumsy Factorial

    题目如下: Normally, the factorial of a positive integer n is the product of all positive integers less t ...

  10. paper 148: Face Databases

    持续更新ing~ 1.人脸检测数据库: (1999年发布)CMU+MIT:180幅图像,共734个人脸.包含3个正面人脸测试子集和一个旋转人脸测试子集,其中正面人脸测试子集有130幅图像,共511个人 ...