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 ...
随机推荐
- hdoj 4762 Cut the Cake
题意很简单就不说了. 解题的关键就是这个公式 answer=n/(m^(n-1)); 要用到大数的乘法.然后java水过. import java.util.*; import java.math.* ...
- zookeeper 集群配置
安装前要先确保配置好 jdk,这里不在讲述 一. 将zookeeper 安装包下载到你想要的目录 下载地址:http://mirrors.hust.edu.cn/apache/zookeeper/ m ...
- 一个基于TCP/IP的服务器与客户端通讯的小项目(超详细版)
1.目的:实现客户端向服务器发送数据 原理: 2.建立两个控制台应用,一个为服务器,用于接收数据.一个为客户端,用于发送数据. 关键类与对应方法: 1)类IPEndPoint: 1.是抽象类EndPo ...
- asp.net core系列 70 即时通迅-WebSocket+Redis发布订阅
一.概述 在asp.net core 中可以用WebSocket 或asp.net core SignalR来开发即时通迅.在项目中由于开发前后端分离,对于SignalR前端技术人员不想依赖juqer ...
- .lib .dll 区别介绍、使用(dll的两种引入方式)
.lib .dll文件都是程序可直接引用的文件,前者就是所谓的库文件,后者是动态链接库(Dynamic Link Library)也是一个库文件.而.pdb则可以理解为符号表文件.DLL(Dynami ...
- 值得花费一周研究的算法 -- KMP算法(indexOf)
KMP算法是由三个科学家(kmp分别是他们名字的首字母)创造出来的一种字符串匹配算法. 所解决的问题: 求文本字符串text内寻找第一次出现字符串s的下标,若未出现返回-1. 例如 text : &q ...
- Java连载14-补码简介&浮点型整数
一.补码简介 1.计算机中的符号数有三种表示方式,即为:原码.反码.补码.三种表示方法均有符号位和数值位,符号位都是0表示正数,符号位都是1表示负数. 2.计算机中的数字的存储方式:在计算机系统中,数 ...
- 测试自动化:java+selenium3 UI自动化(2) - 启动Firefox
1. selenium和浏览器 基于selenium的这套自动化体系,其实现关键就在于对于各浏览器的顺畅操作. 事实上当selenium刚开始起家的时候,他使用的还是javascript注入的方式来驱 ...
- Docker入门-搭建docker私有仓库
Docker Hub 目前Docker官方维护了一个公共仓库Docker Hub,其中已经包括了数量超过15000个镜像.大部分需求都可以通过在Docker Hub中直接下载镜像来使用. 注册登录 可 ...
- 本地VSCode编辑远程服务器文件
前言 先说下我的场景:服务器搭设了一系列复杂环境,然后需要使用PHP实现某些功能 选这种远程编辑的原因: 首先PHP打死我也不想装(这个现在是出了VB外最惹人厌的语言了) 然后环境比较复杂,本地装下比 ...