`
249326109
  • 浏览: 53743 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

uva 439 - Knight Moves

    博客分类:
  • acm
 
阅读更多

先研究下国际象棋中马是怎么走的,然后bfs就哦了。

 

#include<stdio.h>
#include<string.h>
#include<stdlib.h>

int map[10][10];

typedef struct Point {
	int x;
	int y;
	int dis;
} Point;

typedef Point* Ptr;

Point queue[5000];

int dir[8][2] = { { -2, 1 }, { -2, -1 }, { -1, 2 }, { 1, 2 }, { -1, -2 }, { 1,
		-2 }, { 2, 1 }, { 2, -1 } };

int bfs(Point from, Point to) {
	if (from.x == to.x && from.y == to.y)
		return 0;

	int front = 0;
	int rear = 1;
	queue[0].x = from.x;
	queue[0].y = from.y;
	queue[0].dis = from.dis;
	while (front < rear) {
		Ptr p = &queue[front++];
		map[p->x][p->y] = 1;
		if (p->x == to.x && p->y == to.y)
			return p->dis;
		int k;
		int newX, newY;
		for (k = 0; k < 8; k++) {
			newX = p->x + dir[k][0];
			newY = p->y + dir[k][1];
			if (newX >= 1 && newX <= 8 && newY >= 1 && newY <= 8
					&& !map[newX][newY]) {
				queue[rear].x = newX;
				queue[rear].y = newY;
				queue[rear].dis = p->dis + 1;
				rear++;
			}
		}
	}
	return -1;
}

int main() {
	char start[5];
	char end[5];
	while (scanf("%s%s", start, end) != EOF) {
		memset(map, 0, sizeof(map));
		Point from, to;
		from.x = start[0] - 'a' + 1;
		from.y = start[1] - '0';
		from.dis = 0;
		to.x = end[0] - 'a' + 1;
		to.y = end[1] - '0';
		to.dis = -1;
		printf("To get from %s to %s takes %d knight moves.\n", start, end,
				bfs(from, to));
	}
	return 0;
}

 

分享到:
评论

相关推荐

    北大acm 1915 Knight Moves C++语言源代码

    北大 acm JudgeOnline poj1915 Knight Moves c++源代码

    Knight Moves

    Your task is to write a program to calculate the minimum number of moves移动次数 needed for a knight to reach one point from another, so that you have the chance to be faster than Somurolov. ...

    leetcode棋盘-minimum-knight-moves:最小骑士移动

    moves = { { 2 , 1 }, { 1 , 2 }, { - 1 , 2 }, { - 2 , 1 }, { - 2 , - 1 }, { - 1 , - 2 }, { 1 , - 2 }, { 2 , - 1 }}; Queue q = new LinkedList&lt;&gt; (); q . add( new int []{ 0 , 0 }); Set&lt; String &gt; ...

    Knight Moves.txt

    因子:因子也叫因数,例如3*5=15,那么3和5是15的因子。同时15*1=15,那么1和15也是15的因子。 1,3,5,15 这四个因子是15的所有因子。 完数:如果一个数等于不含它本身的其他因子之和,则称该数为‘完数’。...

    北大POJ2243Knight Moves

    该题求解从一个坐标到达另一个坐标的最短步数,移动规则需要按照题目给的方式来移动,即按照国际象棋马的走法一致。

    engine-value_moves.c

    engine-value_moves.c

    Python库 | peewee-moves-1.6.1.tar.gz

    资源分类:Python库 所属语言:Python 资源全名:peewee-moves-1.6.1.tar.gz 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059

    阿尔法新代系统后处理

    $----------------------- RAPID MOVES ---------------------------------------- $20 Rapid Move in XY (MILL/ROUTER/FLAME/LASER) or XZ (LATHE) only G0 X[AX] Y[AY] $IF CHANGE = 0 G0 G43 H[OFS] Z[AZ] $...

    Running-MOVES-2014b-vs-3

    Running-MOVES-2014b-vs-3

    knightmoves

    knightmoves

    TinyVM:用于 HWP 的 TinyVM - SS2014

    微型虚拟机命令: LOAD -- Loads value to R0MOVE_FROM_MEM_TO_REG (Rx),Ry -- Moves the content of (Rx) to RyMOVE_FROM_REG_TO_MEM Rx,(Ry) -- Moves the content of Rx to (Ry)MOVE_FROM_REG_TO_REG Rx,Ry -- ...

    宝元系统alphacam 后处理

    宝元系统alphacam 后处理 ...$----------------------- RAPID MOVES ---------------------------------------- $20 Rapid Move in XY (MILL/ROUTER/FLAME/LASER) or XZ (LATHE) only G00 X[AX] Y[AY] $IF CHANGE = 0

    TV-MOVES

    TV-MOVES

    WPF 国际象棋 棋子 ChessProgrammingTest.zip

    You have been provided with a third-party library "ChessLib" which calculates the legal moves a knight can make given a position on an 8 by 8 board. The library has been used to create a program which...

    Fusion Moves:使用融合移动求解图形模型的MATLAB包装器-matlab开发

    # Matlab Wrapper for Fusion Moves (QPBO + alpha-expansion) 作者:Sarun Gulyanon 24.04.2017 --------------------- 描述--------------------- Fusion Moves 是一种基于 QPBO 和 alpha-expansion 的方法,用于...

    vbucket-moves:显示 Couchbase VBucket 运动的工具

    vbucket-moves.py 是一个分析来自 Couchbase Server 的主事件文件的工具。 主事件文件显示重新平衡事件,可以从。 您需要进行身份验证才能获取文件。 此工具的输出是 vbucket 移动的甘特图,显示移动每个 vbucket 所...

    z-moves-bot

    特征Z-Moves Bot具有许多有用而强大的功能。首先,僵尸程序是持久性的,这意味着该僵尸程序具有状态,因为您可以将截止日期和链接添加到数据库中并随时间进行保存。该机器人提供了一些功能:通知事项截止期限链接...

    street-fighter-moves:街头霸王动作

    StreetFighterMoves·

    Eloquent JavaScript 3rd 第三版高清文字版

    Like any good programming book, Eloquent JavaScript begins with fundamentals--variables, control structures, functions, and data structures--then moves on to complex topics like object-oriented ...

    Tic-Tac-Toe-AI:使用极大极小和 alpha-beta 剪枝算法找到井字游戏的赢家

    Moves considered without alpha-beta pruning: 92 Game Result:-1 Moves considered with alpha-beta pruning: 22 Alpha cuts: 10 Beta cuts: 8 ` 安装 将存储库下载到本地目录 构建java文件javac TicTacToeAI ...

Global site tag (gtag.js) - Google Analytics