1. 程式人生 > 實用技巧 >武士風度的牛(BFS)

武士風度的牛(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;
}