题意:
给你8个格子 0那个格子可以上下左右的动 - - 一开始以为所有格子都能动 毕竟日文题目 那么就很像8数码问题了
由其他格子到“01234567” - - 如果挨个bfs的话 case多肯定TLE 那么思维反向一下 由“01234567”到其他状态 把每个最小步数存到map中 对于每个case直接查找就好了
AC代码如下:
//
// AOJ 0121 Seven Puzzle
//
// Created by TaoSama on 2015-02-20
// Copyright (c) 2014 TaoSama. All rights reserved.
//
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <set>
#include <vector>
#define CLR(x,y) memset(x, y, sizeof(x))
using namespace std;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
const int N = 1e5 + 10;
string s;
typedef pair<string, int> Sta;
map<string, int> dp;
int d[4] = { -1, 1, 4, -4};
void bfs() {
queue<Sta> q;
dp["01234567"] = 0; q.push(Sta("01234567", 0));
while(!q.empty()) {
string s = q.front().first;
int cur = q.front().second;
q.pop();
for(int i = 0; i < 4; ++i) {
string ns = s;
int nxt = cur + d[i];
if(nxt < 0 || nxt > 7 || cur == 3 && nxt == 4 || cur == 4 && nxt == 3)
continue;
swap(ns[cur], ns[nxt]);
if(!dp.count(ns)) {
dp[ns] = dp[s] + 1;
q.push(Sta(ns, nxt));
}
}
}
}
int main() {
#ifdef LOCAL
freopen("in.txt", "r", stdin);
// freopen("out.txt","w",stdout);
#endif
ios_base::sync_with_stdio(0);
bfs();
while(cin >> s) {
for(int i = 1; i <= 7; ++i) {
char x; cin >> x;
s += x;
}
cout << dp[s] << endl;
}
return 0;
}