当前位置:网站首页>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;
}
边栏推荐
- 一文掌握基于深度学习的人脸表情识别开发(基于PaddlePaddle)
- Data set - fault diagnosis: various data and data description of bearings of Western Reserve University
- yolov5detect. Py comment
- Define MySQL function to realize multi module call
- Three solutions to frequent sticking and no response of explorer in win11 system
- Program analysis and Optimization - 9 appendix XLA buffer assignment
- 基于FPGA的VGA协议实现
- leetcode 650. 2 keys keyboard with only two keys (medium)
- 流媒体技术优化
- 来自数砖大佬的 130页 PPT 深入介绍 Apache Spark 3.2 & 3.3 新功能
猜你喜欢
In February 2022, the ranking list of domestic databases: oceanbase regained its popularity with "three consecutive increases", and gaussdb is expected to achieve the largest increase this month
MySQL Foundation
MATLAB signal processing [Q & a notes-1]
Maybe you read a fake Tianlong eight
Connexion à distance de la tarte aux framboises en mode visionneur VNC
Third party payment function test point [Hangzhou multi tester _ Wang Sir] [Hangzhou multi tester]
Interface switching based on pyqt5 toolbar button -1
JDBC Exercise case
Where is the win11 automatic shutdown setting? Two methods of setting automatic shutdown in win11
35 pages dangerous chemicals safety management platform solution 2022 Edition
随机推荐
How to set automatic reply for mailbox and enterprise mailbox?
35页危化品安全管理平台解决方案2022版
95页智慧教育解决方案2022
可知论与熟能生巧
35 pages dangerous chemicals safety management platform solution 2022 Edition
ArrayList analysis 2: pits in ITR, listiterator, and sublist
基于OpenCV实现口罩识别
leetcode 650. 2 keys keyboard with only two keys (medium)
leetcode 650. 2 keys keyboard with only two keys (medium)
Fusion de la conversion et de la normalisation des lots
Using tensorflow to realize voiceprint recognition
Maybe you read a fake Tianlong eight
Container runtime analysis
Interface switching based on pyqt5 toolbar button -2
一文掌握基于深度学习的人脸表情识别开发(基于PaddlePaddle)
MFC文件操作
SharedPreferences save list < bean > to local and solve com google. gson. internal. Linkedtreemap cannot be cast to exception
開源了 | 文心大模型ERNIE-Tiny輕量化技術,又准又快,效果全開
来自数砖大佬的 130页 PPT 深入介绍 Apache Spark 3.2 & 3.3 新功能
What is the official website address of e-mail? Explanation of the login entry of the official website address of enterprise e-mail