当前位置:网站首页>AcWing_188. 武士风度的牛_bfs
AcWing_188. 武士风度的牛_bfs
2022-07-02 22:53:00 【这题AC再睡.】
农民 John 有很多牛,他想交易其中一头被 Don 称为 The Knight 的牛。
这头牛有一个独一无二的超能力,在农场里像 Knight 一样地跳(就是我们熟悉的象棋中马的走法)。
虽然这头神奇的牛不能跳到树上和石头上,但是它可以在牧场上随意跳,我们把牧场用一个 x,y 的坐标图来表示。
这头神奇的牛像其它牛一样喜欢吃草,给你一张地图,上面标注了 The Knight 的开始位置,树、灌木、石头以及其它障碍的位置,除此之外还有一捆草。
现在你的任务是,确定 The Knight 要想吃到草,至少需要跳多少次。
The Knight 的位置用 K 来标记,障碍的位置用 * 来标记,草的位置用 H 来标记。
这里有一个地图的例子:
11 | . . . . . . . . . .
10 | . . . . * . . . . .
9 | . . . . . . . . . .
8 | . . . * . * . . . .
7 | . . . . . . . * . .
6 | . . * . . * . . . H
5 | * . . . . . . . . .
4 | . . . * . . . * . .
3 | . K . . . . . . . .
2 | . . . * . . . . . *
1 | . . * . . . . * . .
0 ----------------------
1
0 1 2 3 4 5 6 7 8 9 0
The Knight 可以按照下图中的 A,B,C,D…A,B,C,D… 这条路径用 5 次跳到草的地方(有可能其它路线的长度也是 5):
11 | . . . . . . . . . .
10 | . . . . * . . . . .
9 | . . . . . . . . . .
8 | . . . * . * . . . .
7 | . . . . . . . * . .
6 | . . * . . * . . . F<
5 | * . B . . . . . . .
4 | . . . * C . . * E .
3 | .>A . . . . D . . .
2 | . . . * . . . . . *
1 | . . * . . . . * . .
0 ----------------------
1
0 1 2 3 4 5 6 7 8 9 0
注意: 数据保证一定有解。
输入格式
第 1 行: 两个数,表示农场的列数 C 和行数 R。
第 2..R+1 行: 每行一个由 C 个字符组成的字符串,共同描绘出牧场地图。
输出格式
一个整数,表示跳跃的最小次数。
数据范围
1≤R,C≤150
输入样例:
10 11
..........
....*.....
..........
...*.*....
.......*..
..*..*...H
*.........
...*...*..
.K........
...*.....*
..*....*..
输出样例:
5//
#include<bits/stdc++.h>
using namespace std;
const int dx[]={ 1,1,-1,-1,2,2,-2,-2 };
const int dy[]={ 2,-2,2,-2,1,-1,1,-1 };
const int INF=0x3f3f3f3f;
const int N=222;
bool used[N][N]; // 标记
char s[N][N];
int n,m,ans;
struct A
{
int x,y,cnt;
A( int xx=0,int yy=0,int in=0 ):x(xx),y(yy),cnt(in) {}
}tt;
void bfs( int x,int y,int cnt )
{
int i,tx,ty;
queue<A> q;
q.push( A( x,y,cnt ) ); used[x][y]=1;
while( !q.empty() )
{ // front()
tt=q.front(); q.pop();
if( s[tt.x][tt.y]=='H' ) { ans=min( ans,tt.cnt ); continue; }
for( i=0;i<8;i++ )
{
tx=tt.x+dx[i]; ty=tt.y+dy[i];
if( tx>=0 && tx<n && ty>=0 && ty<m &&
used[tx][ty]==0 && s[tx][ty]!='*' ) // tt.cnt
{ used[tx][ty]=1; q.push( A( tx,ty,tt.cnt+1 ) ); }
} // A() 构造函数
}
}
int main()
{
int i,j;
while( cin>>m>>n )
{
memset( used,0,sizeof( used ) );
memset( s,0,sizeof( s ) );
for( i=0;i<n;i++ ) cin>>s[i];
for( i=0;i<n;i++ )
for( j=0;j<m;j++ )
if( s[i][j]=='K' ) goto out;
out:
ans=INF; bfs( i,j,0 );
cout<<ans<<endl;
}
return 0;
}边栏推荐
- Connexion à distance de la tarte aux framboises en mode visionneur VNC
- Where is the win11 automatic shutdown setting? Two methods of setting automatic shutdown in win11
- 67页新型智慧城市整体规划建设方案(附下载)
- Container runtime analysis
- 数据集-故障诊断:西储大学轴承的各项数据以及数据说明
- Three solutions to frequent sticking and no response of explorer in win11 system
- 来自数砖大佬的 130页 PPT 深入介绍 Apache Spark 3.2 & 3.3 新功能
- VIM interval deletion note
- [analysis of STL source code] imitation function (to be supplemented)
- 程序分析与优化 - 9 附录 XLA的缓冲区指派
猜你喜欢

Remote connection of raspberry pie by VNC viewer

What if win11 can't turn off the sticky key? The sticky key is cancelled but it doesn't work. How to solve it

What is the official website address of e-mail? Explanation of the login entry of the official website address of enterprise e-mail
![[shutter] shutter open source project reference](/img/3f/b1d4edd8f8e8fd8e6b39548448270d.jpg)
[shutter] shutter open source project reference

Convolution和Batch normalization的融合

How to apply for company email when registering in company email format?

JDBC練習案例

How QT exports data to PDF files (qpdfwriter User Guide)

Highly available cluster (HAC)
![[shutter] shutter photo wall (center component | wrap component | clickrrect component | stack component | positioned component | button combination component)](/img/c5/2f65d37682607aab98443d7f1ba775.jpg)
[shutter] shutter photo wall (center component | wrap component | clickrrect component | stack component | positioned component | button combination component)
随机推荐
leetcode 650. 2 keys keyboard with only two keys (medium)
What is the official website address of e-mail? Explanation of the login entry of the official website address of enterprise e-mail
How QT exports data to PDF files (qpdfwriter User Guide)
Bean load control
采用VNC Viewer方式遠程連接樹莓派
How to apply for company email when registering in company email format?
SharedPreferences save list < bean > to local and solve com google. gson. internal. Linkedtreemap cannot be cast to exception
返回二叉树两个节点间的最大距离
Practical series - free commercial video material library
Load balancing cluster (LBC)
sourcetree 详细
1380. Lucky numbers in the matrix
VIM interval deletion note
67页新型智慧城市整体规划建设方案(附下载)
What are the projects of metauniverse and what are the companies of metauniverse
QT 如何将数据导出成PDF文件(QPdfWriter 使用指南)
yolov5detect. Py comment
MFC file operation
What if win11 can't turn off the sticky key? The sticky key is cancelled but it doesn't work. How to solve it
Optimization of streaming media technology