lightoj 1046 - Rider(bfs)
A rider is a fantasy chess piece that can jump like a knight several times in a single move. A rider that can perform a maximum of K jumps during a single move is denoted as a K-rider. For example, a 2-rider can jump once or twice during a single move, and a 1-rider is a traditional knight.
There are some riders of different types on a chessboard. You are given a 2D board representing the layout of the pieces. The jth character of the ith element of board is the content of the square at row i, column j. If the character is a digit K between '1' and '9', the square contains a K-rider. Otherwise, if the character is a '.', the square is empty. Find the minimal total number of moves necessary to move all the riders to the same square. Only one piece can move during each move. Multiple riders can share the same squares all times during the process. Print -1 if it is impossible.
A traditional knight has up to 8 moves from a square with coordinates (x, y) to squares (x+1, y+2), (x+1, y-2), (x+2, y+1), (x+2, y-1), (x-1, y+2), (x-1, y-2), (x-2, y+1), (x-2, y-1), and can't move outside the chessboard.
Input
Input starts with an integer T (≤ 100), denoting the number of test cases.
Each case begins with a blank line and two integers m, n (1 ≤ m, n ≤ 10) denoting the rows and the columns of the board respectively. Each of the next m lines will contain n integers each denoting the board.
Output
For each case of input you have to print the case number the desired result.
题意:矩阵中有一些骑士, 称为k-Rider, 一步跳k次, 问最少多少步,把所有骑士放到一个位置上。
由于这题数据比较小所以直接bfs各个骑士到所有点的最小值然后再一一比较即可
#include <iostream>
#include <cstring>
#include <queue>
using namespace std;
const int inf = 0X3f3f3f3f;
struct TnT {
int x , y , num;
};
int n , m , vis[30][30] , va[110][30][30] , dr[8][2] = {1, 2, 1, -2, 2, 1, 2, -1, -1, 2, -1, -2, -2, 1, -2, -1};
char map[30][30];
void bfs(int i , int j , int total , int count) {
memset(vis , 0 , sizeof(vis));
queue<TnT>q;
TnT p;
p.x = i , p.y = j , p.num = 0;
vis[p.x][p.y] = 1;
va[count][i][j] = 0;
q.push(p);
while(!q.empty()) {
TnT gg = q.front();
for(int i = 0 ; i < 8 ; i++) {
TnT gl = gg;
gl.x += dr[i][0];
gl.y += dr[i][1];
if(gl.x >= 0 && gl.x < n && gl.y >= 0 && gl.y < m) {
gl.num++;
int temp;
if(gl.num > total) {
if(gl.num % total == 0) {
temp = gl.num / total;
}
else {
temp = gl.num / total + 1;
}
}
else {
temp = 1;
}
if(vis[gl.x][gl.y] == 0) {
va[count][gl.x][gl.y] = temp;
q.push(gl);
}
if(vis[gl.x][gl.y] == 0) {
vis[gl.x][gl.y] = 1;
}
}
}
q.pop();
}
}
int main()
{
int t;
cin >> t;
int ans = 0;
while(t--) {
memset(va , -1 , sizeof(va));
ans++;
cin >> n >> m;
int cnt = inf , count = 0;
for(int i = 0 ; i < n ; i++) {
cin >> map[i];
}
for(int i = 0 ; i < n ; i++) {
for(int j = 0 ; j < m ; j++) {
if(map[i][j] != '.') {
count++;
bfs(i , j , map[i][j] - '0' , count);
}
}
}
for(int i = 0 ; i < n ; i++) {
for(int j = 0 ; j < m ; j++) {
int sum = 0;
for(int l = 1 ; l <= count ; l++) {
sum += va[l][i][j];
if(va[l][i][j] == -1) {
sum = inf;
break;
}
}
cnt = min(cnt , sum);
}
}
cout << "Case " << ans << ": ";
if(cnt == inf) {
cout << -1 << endl;
}
else {
cout << cnt << endl;
}
}
return 0;
}
lightoj 1046 - Rider(bfs)的更多相关文章
- 【lightoj-1046】Rider(BFS)
链接:http://www.lightoj.com/volume_showproblem.php?problem=1046 题意: 给m*n的棋盘,数字k代表这个位置上有棋子,并且一步可以连续跳1-k ...
- LightOJ 1012 简单bfs,水
1.LightOJ 1012 Guilty Prince 简单bfs 2.总结:水 题意:迷宫,求有多少位置可去 #include<iostream> #include<cstr ...
- Lightoj 1174 - Commandos (bfs)
题目链接: Lightoj 1174 - Commandos 题目描述: 有一军队秉承做就要做到最好的口号,准备去破坏敌人的军营.他们计划要在敌人的每一个军营里都放置一个炸弹.军营里有充足的士兵,每 ...
- 暑期训练狂刷系列——Lightoj 1084 - Winter bfs
题目连接: http://www.lightoj.com/volume_showproblem.php?problem=1084 题目大意: 有n个点在一条以零为起点的坐标轴上,每个点最多可以移动k, ...
- loj 1046(bfs)
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=26766 思路:由于数据不是很大,我们可以枚举骑士最后聚集的位置,然 ...
- lightoj 1111 - Best Picnic Ever(dfs or bfs)
题目链接 http://www.lightoj.com/volume_showproblem.php?problem=1111 题意:给你一个有向图再给你几个人的位置,问所有人可以在哪些点相聚. 简单 ...
- LightOJ 1337 F - The Crystal Maze (bfs)
Description You are in a plane and you are about to be dropped with a parasuit in a crystal maze. As ...
- Lightoj 1066 Gathering Food (bfs)
Description Winter is approaching! The weather is getting colder and days are becoming shorter. The ...
- LightOJ 1009 二分图染色+BFS/种类并查集
题意:有两个阵营的人,他们互相敌对,给出互相敌对的人,问同个阵营的人最多有多少个. 思路:可以使用种类并查集写.也可以使用使用二分图染色的写法,由于给定的点并不是连续的,所以排序离散化一下,再进行BF ...
随机推荐
- 后端开发实践系列之二——领域驱动设计(DDD)编码实践
Martin Fowler在<企业应用架构模式>一书中写道: I found this(business logic) a curious term because there are f ...
- KD-tree 专题「Hide and Seek · 巧克力王国」
Lockey的瞎理解 抄了一遍板子又水了俩题,感觉对KD-tree 稍稍理解了一点儿,唠叨一下(二维的KD-tree),如有错误请指出(Lockey 洗脸恭听) 普通平衡树维护的是一维的序列,但对于二 ...
- JAVA基础——Switch条件语句
JAVA基础——switch 条件语句 switch语句结构: switch(表达式){ case值1: 语句体1: break: case值2: 语句体2: break: case值3: 语句体3: ...
- 33行代码爬取妹子图片(bs4+urllib)
from bs4 import BeautifulSoupimport urllib2import urllibimport lxmlimport os def get_imgs(): image_c ...
- 【算法】【排序】【插入类】希尔排序 ShellSort
#include<stdio.h> #include <time.h> #include<stdlib.h> int main(){ ]; //设立随机数 sran ...
- UML类图(1.3)
UML:Unified modeling Language 统一建模语言 UML类图:用来描述系统所包含的类以及类之间的关系. 画图工具:https://www.processon.com 类之间的6 ...
- 带你剖析WebGis的世界奥秘----Geojson数据加载(高级)
前言:前两周我带你们分析了WebGis中关键步骤瓦片加载+点击事件(具体的看前两篇文章),下面呢,我带大家来看看Geojson的加载及其点击事件 Geojson数据解析 GeoJSON是一种对各种地理 ...
- let 、const 、var、function声明关键字的新理解
今天在群里看到大佬们讨论let .const 的提升问题,有个大佬问 三种声明都在什么阶段提升? 什么阶段?这个真不清楚,以前是只知道let.const存在死区,没有变量提升,一下子就懵了 后经手 ...
- 100天搞定机器学习|Day21 Beautiful Soup
前情回顾 机器学习100天|Day1数据预处理 100天搞定机器学习|Day2简单线性回归分析 100天搞定机器学习|Day3多元线性回归 100天搞定机器学习|Day4-6 逻辑回归 100天搞定机 ...
- Throughput Controller
吞吐量控制器(Throughput Controller)介绍 作用:控制其子节点的执行次数与负载比例分配 Total Executions: 整个测试计划中的总执行次数 Percent Execut ...