武士风度的牛(BFS)

题目:

农民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…这条路径用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(C<=150)和行数R(R<=150)。

第2..R+1行: 每行一个由C个字符组成的字符串,共同描绘出牧场地图。

输出格式
一个整数,表示跳跃的最小次数。

题解:题意大概就是这头牛走'日'字,然后不能跳到障碍物上,需要跳几次才能到达终点,求最短步数。看了数据范围,bfs比较稳过一些,大概思路就是用bfs去搜索路径,但是需要标记一下之前是否走过,因为第二次走肯定不是最短的,这样会消耗大量内存空间,这应该是一开始为啥MLE的原因,可以利用一个二维数组记录步数,顺便标记是否之前走过。

AC代码:

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<queue>
#include<string.h>
#include<stdlib.h>
#include<math.h>
using namespace std;
const int N = 155;
int n, m; //行、列 
char str[N][N];
int dis[N][N]; //一开始初始化为-1, 如果遍历过就不再遍历, 否则Mle 
int dir[8][2] = {{1, 2}, {2, 1}, {2, -1,}, {-1, 2}, {-2, 1}, {1, -2}, {-2, -1}, {-1, -2}};
queue<pair<int, int> >q;

bool cp(int x, int y)
{
	if(x < 1 || y < 1 || x > n || y > m || str[x][y] == '*' || dis[x][y] != -1) return false;
	else return true;
}

int bfs(int ex, int ey)
{
	while(q.size())
	{
		pair<int,int> nowNode;
		nowNode = q.front();
		q.pop();
		if(nowNode.first == ex && nowNode.second == ey)
			return dis[nowNode.first][nowNode.second];
		for(int i = 0; i < 8; i++)
		{
			int xx = dir[i][0] + nowNode.first;
			int yy = dir[i][1] + nowNode.second;
			if(cp(xx, yy))
			{
				dis[xx][yy] = dis[nowNode.first][nowNode.second] + 1;
				q.push(make_pair(xx, yy));
			}
		}
	}
}

int main()
{
	int ex, ey; //终点坐标 
	ios::sync_with_stdio(false);
	memset(dis, -1, sizeof(dis)); 
//	freopen("cin.in", "r", stdin);
//	freopen("cout.out", "w", stdout);
	cin >> m >> n;
	for(int i = 1; i <= n; i++)
	{
		for(int j = 1; j <= m; j++)
		{
			cin >> str[i][j];
			if(str[i][j] == 'K')
			{
				q.push(make_pair(i, j));
				dis[i][j] = 0; //设置起始点不需要步数 
			}
			if(str[i][j] == 'H')
			{
				ex = i;
				ey = j;
			}
		}
	}
	cout << bfs(ex, ey);
	return 0;
}
 

posted @ 2020-10-14 21:28  ~K2MnO4  阅读(583)  评论(0)    收藏  举报