POJ 1198 Solitaire(bfs)

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.

Input

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.

Output

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.

Sample Input

4 4 4 5 5 4 6 5
2 4 3 3 3 6 4 6

Sample Output

YES


题意是四个棋子,给出起始位置和终止位置(无序),问是否在8步之内满足要求。

我用的是单向bfs


#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<queue>

using namespace std;

#define MAXN 9

int fx[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
bool vis[MAXN][MAXN][MAXN][MAXN][MAXN][MAXN][MAXN][MAXN];
bool map[MAXN][MAXN];

struct node{
	int x[5], y[5];
	int dep;
};

node s, t;

bool check(node p){
	for(int i = 1; i <= 4; i++)
	  if(!map[p.x[i]][p.y[i]]) return false;
	return true;
}

bool ok(int X, int Y){
	if(X <= 0 || Y <= 0 || X > 8 || Y > 8) return false;
	return true;
}

bool ok_c(node t){
	bool &tmp = vis[t.x[1]][t.x[2]][t.x[3]][t.x[4]][t.y[1]][t.y[2]][t.y[3]][t.y[4]];
	if(tmp) return false;
	return tmp = true;
}

bool empty(int X, int Y, node now){
	for(int i = 1; i <= 4; i++)
	  if(X == now.x[i] && Y == now.y[i]) return false;
	return true;
}

void bfs(){
	queue<node> q;
	q.push(s);
	ok_c(s);
	
	while(!q.empty()){
		node now = q.front(); q.pop();
		
		if(now.dep >= 8){
			printf("NO\n");
			return;
		}
				
		for(int i = 1; i <= 4; i++){
			int X = now.x[i], Y = now.y[i];
			for(int j = 0; j < 4; j++){
				node next = now;
				next.dep = now.dep+1;

				int nx = X + fx[j][0], ny = Y + fx[j][1];
				
				if(!ok(nx, ny)) continue;
				next.x[i] = nx; next.y[i] = ny;
									
				if(empty(nx, ny, now)){
					next.x[i] = nx; next.y[i] = ny;
					if(!ok_c(next)) continue;
					
					if(check(next)){
						printf("YES\n");
						return;
					}
					q.push(next);
				}else{
					nx += fx[j][0]; ny += fx[j][1];
					if(!ok(nx, ny)) continue;
					next.x[i] = nx; next.y[i] = ny;
					if(!ok_c(next) || !empty(nx, ny, now)) continue;
					if(check(next)){
						printf("YES\n");
						return;
					}
					
					q.push(next);
				}
			}
		}
	}
	
	printf("NO\n");
	return;
}

int main(){
	freopen("test.in", "r", stdin);
	freopen("test.out", "w", stdout);
	
	for(int i = 1; i <= 4; i++) scanf("%d%d", &s.x[i], &s.y[i]);
	for(int i = 1; i <= 4; i++){
		scanf("%d%d", &t.x[i], &t.y[i]);
		map[t.x[i]][t.y[i]] = true;
	}
	s.dep = 0;
	
	bfs();	
	
	return 0;
}


^-^come on!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值