http://noi.openjudge.cn/——2.5基本算法之搜索——200:Solitaire

文章目录

题目

总时间限制: 5000ms 单个测试点时间限制: 1000ms 内存限制: 65536kB
描述
Solitaire is a game played on a chessboard 8x8. The rows and columns of the chessboard are numbered from 1 to 8, from the top to the bottom and from left to right respectively.
There are four identical pieces on the board. In one move it is allowed to:
move a piece to an empty neighboring field (up, down, left or right),
jump over one neighboring piece to an empty field (up, down, left or right).
在这里插入图片描述
There are 4 moves allowed for each piece in the configuration shown above. As an example let’s consider a piece placed in the row 4, column 4. It can be moved one row up, two rows down, one column left or two columns right.
Write a program that:
reads two chessboard configurations from the standard input,
verifies whether the second one is reachable from the first one in at most 8 moves,
writes the result to the standard output.
输入
Each of two input lines contains 8 integers a1, a2, …, a8 separated by single spaces and describes one configuration of pieces on the chessboard. Integers a2j-1 and a2j (1 <= j <= 4) describe the position of one piece – the row number and the column number respectively.
输出
The output should contain one word YES if a configuration described in the second input line is reachable from the configuration described in the first input line in at most 8 moves, or one word NO otherwise.
样例输入
4 4 4 5 5 4 6 5
2 4 3 3 3 6 4 6
样例输出
YES

宽搜代码

#include <bits/stdc++.h>
using namespace std;
const int d[4][2]={
   
   {
   
   0,-1},{
   
   -1,0},{
   
   0,1},{
   
   1,0}};//左上右下四个方向 
struct node{
   
   //每个棋的坐标 
	int x,y;
	bool operator<(const node& b)const{
   
   //排序用 
		if(x==b.x)return y<b.y;
		return x<b.x;
	}
};
struct chesss{
   
   
	int step,id[2];//步数 
	node piece[2][4];//起始和目标四个子的位置 
	bool board[2][9][9];//起始和目标的布局
	chesss(){
   
   //无参构造函数 
		step=0;
		memset(board,0,sizeof(board));//初始化 
	}
	void makeid(int x){
   
   //布局(各棋子的坐标)转换成8位唯一的id 
		sort(piece[x],piece[x]+4);//排序 
		id[x]=0;
		for(int i=0;i<4;i++)
		id[x]=id[x]*100+piece[x][
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值