BFS - Marching Legion - ab Knight

news/2024/11/16 8:29:40/

目录

  • Marching Legion
  • ab Knight

Marching Legion

时间限制: 1 Sec 内存限制: 128 MB
题目描述
There’s a rebellion in Antioch! Caesar is preparing to dispatch an entire legion of his best troops to quell the rebellion, but before he does, he wants to make certain the legion will make it to Antioch.
Along the way, there are mountains, rivers, seas, and mythical monsters, each of which the legion cannot pass through. Given a map a legion must travel through, find out if the legion can reach Antioch from Rome.
输入
The first line of the input will be a single integer, n ≤ 1, 000. There will be n test cases that follow.
Input will begin with two integers, 0 < h, w < 100, indicating the height and width of the map, respectively. h lines with w characters each will follow. An “X” on the map represents an impassable obstacle while an “O” represents traversable terrain. Note that legions, moving in square formations, can only move up, down, left, and right; legions can never travel diagonally. “R” represents Rome, the starting point of the legion, and “A” represents Antioch, the destination point for the legion
输出
Output “March onward!” if the legion can reach Antioch from Rome, “Stay home!” otherwise. Each output should be line separated.
样例输入
3
6 6
XXXXOA
XOXXOX
XOXXOX
XOOOOX
XOXXXX
XROOOX
4 4
XXOA
XOOX
XOXX
XRXO
2 2
XA
RX
样例输出
March onward!
March onward!
Stay home!

题意:给你一个n*m的棋盘,A位置为出发点,R 位置为目标点,O位置为通路,X位置不可走。询问是否可以从A位置走到R位置.如果可以输出“March onward!”,否则输出“Stay home!”;

第一思路DFS:
Code:

/*
Author:MYS
Date:2021.4.11
*/
#include<bits/stdc++.h>
#pragma GCC optimize(2)
using namespace std;
typedef long long ll;
const int N = 1e3+10;
const int mod = 1e9+7;
#define pp putchar('\n')
inline int read() {int sign=1,num=0;char ch = getchar();while(ch<'0'||ch>'9') {if(ch=='-') sign=-1;ch = getchar();}while(ch>='0'&&ch<='9') {num=num*10+ch-'0';ch = getchar();}return sign*num;
}
char a[1010][1010];
int t,n,m;
int dir[5][5]= {{1,0},{-1,0},{0,1},{0,-1}};
bool vis[1010][1010];
bool judge(int dx,int dy) {if(dx>=1&&dx<=n&&dy>=1&&dy<=m&&(a[dx][dy]=='O'||a[dx][dy]=='R')) {return 1;}return 0;
}
bool f;
void dfs(int x,int y) {if(f) return;//cout<<x<<" "<<y<<" "<<a[x][y]<<endl;if(a[x][y]=='R') {f=1;return;}vis[x][y] = 1;int j=0;for(int i=0; i<4; i++) {int dx = x+dir[i][0];int dy = y+dir[i][1];if(judge(dx,dy)&&!vis[dx][dy]) {dfs(dx,dy);vis[dx][dy] = 0;//回溯}}
}
int main() {scanf("%d",&t);while(t--) {f=0;scanf("%d%d",&n,&m);memset(vis,0,sizeof(vis));int si,sj;for(int i=1; i<=n; i++) {for(int j=1; j<=m; j++) {cin>>a[i][j];if(a[i][j]=='A') si=i,sj=j;}}dfs(si,sj);if(f) printf("March onward!");else printf("Stay home!");putchar('\n');}return 0;
}

结果TEL


换BFS:
Code:

/*
Author:MYS
Date:2021.4.11
*/
#include<bits/stdc++.h>
#pragma GCC optimize(2)
using namespace std;
typedef long long ll;
const int N = 1e3+10;
const int mod = 1e9+7;
#define pp putchar('\n')
inline int read() {int sign=1,num=0;char ch = getchar();while(ch<'0'||ch>'9') {if(ch=='-') sign=-1;ch = getchar();}while(ch>='0'&&ch<='9') {num=num*10+ch-'0';ch = getchar();}return sign*num;
}
struct node {int x,y;
};
int m,n;
int t,sx,sy,ex,ey;
char ch[110][110];
int dir[5][5]= {{1,0},{-1,0},{0,1},{0,-1}};queue<node> q;
bool judge(int xx,int yy) {if(xx>=1&&xx<=m&&yy>=1&&yy<=n&&(ch[xx][yy]=='O'||ch[xx][yy]=='R')) return 1;return 0;
}
int bfs() {while(q.size()) q.pop();q.push({sx,sy});while(q.size()) {int nx = q.front().x;int ny = q.front().y;//cout<<nx<<" "<<ny<<endl;q.pop();if(nx==ex&&ny==ey) {return 1;  //找到即立即返回}for(int i=0; i<4; i++) {int dx = nx+dir[i][0];int dy = ny+dir[i][1];if(judge(dx,dy)) {  //符合条件就入队ch[dx][dy] = 'X';q.push({dx,dy});}}}return 0;
}
int main() {cin>>t;while(t--) {sx = sy = ex = ey = 0;cin>>m>>n;for(int i=1; i<=m; i++) {for(int j=1; j<=n; j++) {cin>>ch[i][j];if(ch[i][j]=='A') {sx = i;sy = j;}if(ch[i][j]=='R') {ex = i;ey = j;}}}if(bfs()) puts("March onward!");else puts("Stay home!");}return 0;
}

结果AC


为什么DFS会T泥?有一说一可能是有数据会卡你,问了WSQ学姐,学姐说能BFS就先BFS。我们可以相应的输出一下DFS和BFS跑的路径看一下有何不同;
在这里插入图片描述
在这里插入图片描述

  • 我们通过输出路径可以看出来这个DFS是一条路给他跑到底,不撞南墙不回头的那种,这也就是为啥叫他深度优先搜索,如果跑到最后发现这条路不行就再给他回溯回去。
  • 而BFS不一样,如果在一个地方出现了岔路,就如上面样例的(4,2)处,他是分路同时走,也就是当(4,2)这个节点出现了两个分节点时,他是两个分节点同时遍历的,这也就是为啥叫他广度优先搜索,而不是像个蛮牛一样一路顶到底。

ab Knight

题目描述
In chess, a knight is the only piece that can “jump” over pieces. Namely, a typical knight can move 1 square up, down, left or right, followed by 2 squares in a perpendicular direction. Thus, if a knight is at square (x, y), after its jump it can end up at one of these eight positions:
(x ± 1, y ± 2), (x ± 2, y ± 1), provided that they are in bounds and there isn’t a piece on the destination square.
Now, consider a modified knight called an ab knight, where a and b are both positive integers, not necessarily distinct. From square (x, y), the eight squares the ab knight can reach on a single jump are (x ± a, y ± b), (x ± b, y ± a), provided that they are in bounds.
For the purposes of this problem, we’ll assume that there are no pieces on the board except for the original ab knight. Our goal will be to calculate the fewest number of jumps necessary for the knight to reach each of the other squares on the board.
The Problem
Given the size of a chess board, the values of a and b for the ab knight, and the initial position of
the ab knight, determine the fewest number of moves necessary for the ab knight to reach each of
the squares on the board, or determine that some squares aren’t reachable.
输入
The first line of input will contain a single positive integer, n (n ≤ 20), representing the number of input cases to process. The input cases follow, each taking up three lines. The first line of each input case contains two space separated positive integers, r (3 ≤ r ≤ 100) and c (3 ≤ c ≤ 100), representing the number of rows and columns on the chessboard for the input case. The second line of each input case contains two space separated positive integers, a (a ≤ min(r, c)) and b (b ≤ min(r, c)), representing the values of a and b for the ab knight for the input case. The third line of each input case contains two space separated positive integers, x (x ≤ r) and y (y ≤ c), representing the row and column of the initial location of the ab knight. Assume that the top left hand corner of the board is square is row 1, column 1, the bottom left hand corner of the board is square row r, column 1, the top right hand corner of the board is square row 1, column c, and the bottom right corner of the board is square row r, column c.
输出
For each case, output r lines of c space separated integers, where the jth value on the ith line represent the fewest number of jumps necessary for the ab knight to travel from its initial square to row i column j. Remember not to output a space after the last integer on each row. If a square is unreachable by the ab knight, print out -1 instead.
样例输入
2
3 3
1 2
1 1
2 5
1 1
1 2
样例输出
0 3 2
3 -1 1
2 1 4
-1 0 -1 2 -1
1 -1 1 -1 3

题意:
给你一个r*c的棋盘,再给你一个出发点,让你判断是否你能从出发点走到所有位置,走不到的位置输出“-1”,能到达的位置输出最少多少步到达;

Code:

/*
Author:MYS
Date:2021.4.16
*/
#include<bits/stdc++.h>
#pragma GCC optimize(2)
using namespace std;
typedef long long ll;
const int N = 1e4+10;
const int inf = 0x3f3f3f;
#define pp putchar('\n')
inline int read() {int sign=1,num=0;char ch = getchar();while(ch<'0'||ch>'9') {if(ch=='-') sign=-1;ch = getchar();}while(ch>='0'&&ch<='9') {num=num*10+ch-'0';ch = getchar();}return sign*num;
}
int t,r,c;
int a,b,sx,sy;
struct node {int x,y;
};
queue<node> q;
bool vis[110][110];
int dis[110][110];
void BFS(int x,int y) {int dir[10][10]= {{a,b},{a,-b},{-a,b},{-a,-b},{b,a},{b,-a},{-b,a},{-b,-a}};q.push({x,y});vis[x][y] = 1;dis[x][y] = 0;while(!q.empty()) {int nx = q.front().x;int ny = q.front().y;q.pop();for(int i=0; i<8; i++) {int dx = nx+dir[i][0];int dy = ny+dir[i][1];if(dx>=1&&dx<=r&&dy>=1&&dy<=c&&!vis[dx][dy]) {q.push({dx,dy});vis[dx][dy] = 1;dis[dx][dy] = dis[nx][ny]+1;}}}
}
int main() {cin>>t;while(t--) {cin>>r>>c;cin>>a>>b>>sx>>sy;memset(vis,0,sizeof(vis));for(int i=1; i<=r; i++)for(int j=1; j<=c; j++)dis[i][j] = inf;BFS(sx,sy);for(int i=1; i<=r; i++) {for(int j=1; j<=c; j++) {if(vis[i][j]) cout<<dis[i][j];else cout<<"-1";if(j!=c) cout<<" ";}pp;}}return 0;
}

暂时就酱,如有错误或疑问,还请看官指出


http://www.ppmy.cn/news/364570.html

相关文章

为帮助建筑和设施管理者满足保持社交距离的需求,Bentley 软件公司开放对 LEGION Simulator 和 OpenBuildings Station Designer 的完全访问权限,并在

对重新开放的公共设施人群移动进行模拟&#xff0c;并测试空间安全性 以降低风险 宾夕法尼亚州埃克斯顿--(美国商业资讯)--Bentley 软件公司是全球领先的综合软件和数字孪生服务提供商&#xff0c;致力于推进基础设施的设计、施工和运营。Bentley今天宣布开放其 LEGION Simula…

Legion 一款网络渗透工具

一款名叫Legion的开源软件&#xff0c;这款工具简单易用&#xff0c;且具有高度可扩展性。这是一款半自动化的网络渗透测试工具&#xff0c;可帮助研究人员发现、侦察和利用目标信息系统中的安全漏洞。 Legion&#xff1a;一款易于使用且功能强大的半自动化网络渗透工具功能介绍…

联想拯救者Legion Y7000P 2020款安装ubuntu16.04 解决WIFI 显卡 cuda10.2)

本文参考了以下博客的方法 https://blog.csdn.net/HerrKang/article/details/108931253 https://blog.csdn.net/MIRANA0/article/details/106696334 1.U盘安装完Linux系统后,先换源 具体可以参考以下博客的方法 https://blog.csdn.net/u010592301/article/details/90451179 2.联…

联想拯救者Legion Y7000P 2020款ubuntu20.04安装ros noetic与bloom-generate打包ros noetic为deb软件包

一、下载并安装Ubuntu20.04 阿里云下载ubuntu系统官方镜像&#xff1b;&#xff08;阿里云的此镜像我已验证可顺利安装ros&#xff09;Index of /ubuntu-releases/20.04/ 下载&#xff1a;ubuntu-20.04.3-desktop-amd64.iso 3.也可以在autolabor官网下载他们做好的ros开源镜像…

计算机专业y9000x,LEGION Y9000X笔记本U盘一键重装Win10专业版的教程

LEGION Y9000X最高可配备15.6英寸4K屏幕&#xff0c;并拥有轻薄化设计&#xff0c;机身厚度14.9mm&#xff0c;重量不超过1.7kg。与市面上其它同类规格的笔记本明显不同的是&#xff0c;LEGION Y9000X配备了45W高性能标压处理器&#xff0c;且拥有4个散热风扇为CPU进行独享散热…

Legion使用:半自动化网络渗透工具

0x00 背景 Legion, SECFORCEs Sparta的一个副本&#xff0c;Sparta已停止维护&#xff0c;所以Legion作为Sparta的优化升级版本&#xff0c;它是一个开源、易用、超级可扩展和半自动化的网络渗透测试工具&#xff0c;有助于信息系统的发现、侦察和利用。 项目官网&#xff1a;…

legion--一款开源,易用,扩展性强的半自动化渗透测试工具

文章目录 简介特点与Sparta的区别GitHub项目地址安装用法启动选择目标查看结果 简介 Legion是SECFORCE的Sparta的分支&#xff0c;是一个开源&#xff0c;易于使用&#xff0c;超扩展和半自动化的网络渗透测试框架&#xff0c;针对发现&#xff0c;侦察和利用漏洞的信息系统。…

​ 最大尺寸的超宽高刷新率显示器 —— Legion Y44w 上手体验

早几年的时候&#xff0c;我们提到16:9 可能还有人说是宽屏&#xff0c;随着显示器行业的不断发展和游戏玩家们的需求不断变化&#xff0c;把屏幕往横向加宽就成了一个大趋势&#xff0c;用一个超宽屏代替双联屏甚至三连屏&#xff0c;由于其没有中间接缝&#xff0c;连线更简洁…