zoj 2097 Walking on a Chessboard

本文探讨了一个在棋盘上寻径的问题,通过使用广度优先搜索(BFS)算法解决机器人从起点到终点的最短路径问题。机器人在移动时不仅考虑了移动成本,还改变了其状态,这使得传统的BFS方法不适用,引入了状态更新的概念。通过维护一个三维数组记录机器人以特定状态到达每个点的最短距离,实现了问题的有效求解。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Walking on a Chessboard

Time Limit: 2 Seconds      Memory Limit: 65536 KB

A mini robot is put on a cell of a chessboard. The size of the chessboard is 8 * 8. The robot has four states denoted by integer from 1 to 4.

There is an integer in each cell of the chessboard, the integers are positive and not greater than 1000. The robot can walk up, down, left or right to one of the neighboring cells. The cost of the movement is the multiplication of the integer in the new cell and its original state, then the state of the robot is altered to (cost MOD 4 + 1). The initial state of the robot is 1.

Your task is to find the path between two given cells with the minimum total cost. Total cost is the sum of the costs for each walk along the path.


Input

The input contains multiple test cases. Each test case begins with a line containing the row number and column number of the start cell and the target cell. Row number and column number will be within 1 and 8. 8 lines follow, each with 8 integers. This is the chessboard configuration.

There will be a blank line between subsequent test cases. The last line of the input will have 4 zeros which signals the end of the input and should not be processed.


Output

For each test case, print the minimum total cost on a line.


Sample Input

1 1 2 2
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1

0 0 0 0


Sample Output

3



Author: CHEN, Shunbao

Source: Zhejiang University Local Contest 2004


这是一道关于BFS的题,与一般BFS的题不同的是,每走一步,它的状态都会改变,而题目要求的是从一个点到另一个点的最短距离

他每走一步的cost = 原来的状态 * 当前点的值

而他的状态state也更新为 = cost % 4 + 1

所以我们不能用纯BFS来做,我们需要记录到达每个点所花费的时间 

我用status[8][8][5] 来记录这个robot以某个状态到达某个点的最短距离

例如: status[temp.x-1][temp.y-1][temp.state] 就表示robot到达temp.x-1,temp.y-1,并且状态变为temp.state时,所花费的最短时间


下面是我的代码:(自我感觉写的不好)

#include<iostream>
#include<algorithm>
#include<memory.h>
#include<queue>
using namespace std;

struct point
{
	int x,y;
	int state;
	int val;
}start, temp, other;

int n1,n2,m1,m2;
int num[8][8];
int status[8][8][5];
int direction[4][2] = {{0,1},{0,-1},{1,0},{-1,0}};
int cost;

void BFS()
{
	queue<point> que;
	start.x = n1;
	start.y = n2;
	start.state = 1;
	start.val = 0;
	que.push(start);

	while (!que.empty())
	{
		temp = que.front();
		que.pop();
		if( temp.x == m1 && temp.y == m2) 
		{
			if(temp.val < cost)
				cost = temp.val;
		}

		for(int i=0; i<4; i++)
		{
			int x = temp.x + direction[i][0];
			int y = temp.y + direction[i][1];
			if( x>0 && x<=8 && y>0 && y<=8 )
			{
				other.x = x;
				other.y = y;
				other.val = temp.val + temp.state * num[x-1][y-1];
				other.state = (temp.state * num[x-1][y-1]) % 4 + 1;	
				if( status[x-1][y-1][other.state] == 0 || other.val < status[x-1][y-1][other.state] )
				{
					status[x-1][y-1][other.state] = other.val;
					que.push(other);
				}
			}
		}
	}
}

int main()
{
	

	while( cin>>n1>>n2>>m1>>m2 && n1!=0 && n2!=0 && m1!=0 && m2!=0 )
	{
		for( int i=0; i<8; i++)
			for( int j=0; j<8; j++)
				cin>>num[i][j];	
		memset(status, 0, sizeof(status));
		cost = (1 << 30);
		BFS();
		cout<<cost<<endl;
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值