Uva - 439 - Knight Moves


bfs求最短路,经典数据结构题目。

AC代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cctype>
#include <cstring>
#include <string>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <algorithm>
#include <stack>
#include <queue>
#include <bitset> 
#include <cassert> 
#include <cmath>

using namespace std;

const int maxn = 8;
int r1, c1, r2, c2;
int vis[maxn][maxn]; // 记录访问状态

const int dr[] = { -2, -2, -1, -1, 1, 1, 2, 2 };
const int dc[] = { -1, 1, -2, 2, -2, 2, -1, 1 };

struct State
{
	int r, c, cnt;
	State(int r = 0, int c = 0, int cnt = 0) : r(r), c(c), cnt(cnt) {}
	bool operator==(const State& rhs) // 重载==操作符
	{
		return r == rhs.r && c == rhs.c;
	}
};

// 把列也转化成数字来维护
void tr(char* p, int &r, int &c)
{
	r = p[0] - 'a';
	c = p[1] - '1';
}

// bfs求最短路,并维护cnt
int bfs()
{
	State f(r1, c1, 0), t(r2, c2);
	if (f == t) {
		return 0;
	}
	queue<State> q;
	q.push(f);
	vis[r1][c1] = 1;

	while (!q.empty()) {
		State s = q.front();
		q.pop();
		if (s == t) {
			return s.cnt;
		}
		for (int i = 0; i < maxn; i++) {
			int nr, nc;
			nr = s.r + dr[i];
			nc = s.c + dc[i];
			if (nr >= 0 && nr < 8 && nc >= 0 && nc < 8 && !vis[nr][nc]) {
				q.push(State(nr, nc, s.cnt+1));
				vis[nr][nc] = 1;
			}
		}
	}
	return -1;
}

int main()
{
	char p1[3];
	char p2[3];
	while (scanf("%s%s", p1, p2) != EOF) {
		tr(p1, r1, c1);
		tr(p2, r2, c2);
		memset(vis, 0, sizeof(vis));
		int cnt = bfs();
		printf("To get from %s to %s takes %d knight moves.\n", p1, p2, cnt);
	}

	return 0;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值