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段距离到达另一个商店(在范围之内),一个商店为一段距离. ...
随机推荐
- 2019-9-2-win10-uwp-切换主题
title author date CreateTime categories win10 uwp 切换主题 lindexi 2019-09-02 12:57:38 +0800 2018-2-13 1 ...
- solrconfig.xml主要配置项
solrconfig.xml中的配置项主要分以下几大块: 1.依赖的lucene版本配置,这决定了你创建的Lucene索引结构,因为Lucene各版本之间的索引结构并不是完全兼容的,这个需要引起你的注 ...
- JS的加载和执行
从JS的加载和执行谈性能优化 ---高性能JS读后感(第一章) 从脚本的"霸道"说起,随着浏览器的进步,js越来越听话了,所以,我们先说说以前的浏览器是怎么加载js的,以及js如何 ...
- pycharm windows 远程修改服务器代码
配置过程 本机环境 操作系统:win10 IDE:Pycharm 远程服务器 操作系统:ubuntu 4.4.0 配置了ssh,可以使用ssh进行远程登陆 配置Deployment 首先,在pycha ...
- 【和孩子一起学编程】 python笔记--第三天
第十章 游戏时间:Skier 首先安装pygame,直接在cmd命令控制框里键入pip install pygame就可以了 代码: import pygame, sys, random skier_ ...
- for循环语句示例
for循环语句示例 一判断/var/目录下所有文件的类型 完整脚本 [root@centos73 ~]# cat shell_scripts/filetype.sh #!/bin/bash #Auth ...
- Python--面向对象的程序设计之组合应用、开发软件规范
组合应用: class Teacher: def __init__(self,name,age,sex,salary,level): self.name=name self.age=age self. ...
- python--闭包函数、装饰器
先来点补充. x= def foo(): print(x) x= foo() 结果: x= def foo(): global x x= print(x) foo() print(x) 结果: x= ...
- 分布式存储Ceph之PG状态详解
https://www.jianshu.com/p/36c2d5682d87 1. PG介绍 继上次分享的<Ceph介绍及原理架构分享>,这次主要来分享Ceph中的PG各种状态详解,PG是 ...
- 工厂方法配置bean
1:静态工厂方法配置bean 1):对象 package com.spring.helloworld; public class Car { private String name; private ...