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段距离到达另一个商店(在范围之内),一个商店为一段距离. ...
随机推荐
- linux c 链接详解4-共享库
4. 共享库 4.1. 编译.链接.运行 组成共享库的目标文件和一般的目标文件有所不同,在编译时要加-fPIC选项,例如: $ gcc -c -fPIC stack/stack.c stack/pus ...
- CMakeLists.txt install
本部分是关于ros CMakeLists.txt install :可参考http://wiki.ros.org/catkin/CMakeLists.txt 1.CMakeLists.txt中的in ...
- mysql基于Altas读写分离并实现高可用
实验环境准备: master:192.168.200.111 slave1:192.168.200.112 slave2:192.168.200.113 Altas:192.168.200.114 c ...
- exp ORA-01455: converting column overflows integer datatype
EXP-00008: ORACLE error 1455 encounteredORA-01455: converting column overflows integer datatype add ...
- 前端学习(三十)es6的一些问题(笔记)
赋值表达式 document.onclick = document.onmouseover = fn; var a = b = c = d = 5; 不推荐 逗号表 ...
- MD5文件去重
//计算文件的MD5码 private string getMD5Hash(string pathName) { string strResult = ""; string str ...
- C语言数组,指针小案例
/* ============================================================================ Name : hello.c Autho ...
- 【leetcode】662. Maximum Width of Binary Tree
题目如下: Given a binary tree, write a function to get the maximum width of the given tree. The width of ...
- MOSFET学习
MOS/CMOS集成电路简介及N沟道MOS管和P沟道MOS管 在实际项目中,我们基本都用增强型mos管,分为N沟道和P沟道两种. 我们常用的是NMOS,因为其导通电阻小,且容易制造.在MOS管原理图上 ...
- Python基础教程(004)--Python的设计哲学
前言 Python已经成为了一门流行的编程语言. 知识点 1,优雅 2,明确 3,简单 Python开发者的哲学是:用一种方法,最好是只有一种方法来做一件事. 如果面临多种选择,Python开发者都会 ...