初级篇——穷竭搜索——广度优先搜索 aoj 0121 seven puzzle

7 puzzles
The 7-puzzle consists of eight square cards and a frame that fits these cards snugly. Each card is numbered 0, 1, 2, …, 7 to distinguish them from each other. You can stack two cards vertically and four cards horizontally.

7 When you start the puzzle, put all the cards in the box first. Only the 0 card in the frame can be exchanged with the adjacent cards vertically and horizontally. For example, if the state of the frame is as shown in Figure (a) and you swap the position with the 7th card that is adjacent to the right of the 0th card, you will get the state shown in Figure (b). Alternatively, from the state of Figure (a), swap the position with the adjacent 2 card below the 0 card to get the state of Figure ©. In the state shown in Figure (a), the 0 card and the cards adjacent to the top, bottom, left, and right are only the 7 and 2 cards, so other positions cannot be swapped.

The goal of the game is to neatly align the cards so they look like Figure (d). Write a program that takes the first state as input and outputs the minimum number of steps required to properly arrange the cards. However, it is assumed that it is possible to move from the entered card status to the status shown in Figure (d).

The input data is given as a space-separated list of eight numbers. These represent the initial set of cards. For example, the numerical representation of figure (a) becomes 0 7 3 4 2 5 1 6 and figure © becomes 2 7 3 4 0 5 1 6 .

在这里插入图片描述
Figure (a) 0 7 3 4 2 5 1 6 When the 在这里插入图片描述Figure (b) 7 0 3 4 2 5 1 6 When the

在这里插入图片描述
Figure © 2 7 3 4 0 5 1 6 When the 在这里插入图片描述Figure (d) 0 1 2 3 4 5 6 7 (final state)
Input
Multiple puzzles are given in the above format. Please process until the end of input. No more than 1,000 puzzles can be given.

Output
For each puzzle, please output the minimum number of steps to move to the final state on one line.

Sample Input
0 1 2 3 4 5 6 7
1 0 2 3 4 5 6 7
7 6 5 4 3 2 1 0
Output for the Sample Input
0
1
28

逆向思维,由目标状态出发反推到各个状态的距离,使用关联数组保存这个距离,板子题。学到了。

#include<iostream>
#include<cstring>
#include<string>
#include<queue>
#include<map>
#include<algorithm>
using namespace std;
map<string,int> seq;
const int MAX = 8 * 7 * 6 * 5 * 4 * 3 * 2 + 10;
int d[4] = {-1,-4,1,4};
int p,np;
string s,ns;
void bfs(){
	queue<string> q;
	s = "01234567";
	q.push(s);
	seq[s] = 0;
	while(!q.empty()){
		s = q.front();
		q.pop();
		p = s.find('0');
		for(int i = 0;i < 4; ++i){
			np = p + d[i];
			if(np >= 0 && np < 8){
				if((i == 2 && p == 3) || (i == 0 && p == 4))
					continue;
				else{
					ns = s;
					swap(ns[p],ns[np]);
					if(seq[ns] == 0){
						seq[ns] = seq[s] + 1;
						q.push(ns);
					}
				}
			}
		}
	}
}
int main(){
	bfs();
	char c;
	while(cin >> c){
		string str = "";
		str += c;
		for(i = 1;i < 8; ++i){
			cin >> c;
			str += c;
		}
		cout << seq[str] << endl;
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值