AOJ 0121 Seven Puzzle (bfs)
来源:互联网 发布:c语言编写安卓 编辑:程序博客网 时间:2024/05/29 21:33
题意:
给你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 LOCALfreopen("in.txt", "r", stdin);//freopen("out.txt","w",stdout);#endifios_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;}
0 0
- AOJ 0121 Seven Puzzle (bfs)
- AOJ 0121:Seven Puzzle(bfs)
- Seven Puzzle (AOJ 0121 bfs)
- Aoj 0121 Seven Puzzle【bfs】
- AOJ 0121: Seven Puzzle (BFS DP STL 逆向推理)
- AOJ 0121: Seven Puzzle (BFS DP STL 逆向推理)
- 搜索 Seven Puzzle (AOJ 0121 bfs)
- AOJ 0121 Seven Puzzle
- AOJ--0121 Seven Puzzle
- G - Seven Puzzle(7宫格)AOJ 0121
- AOJ 0121 Seven Puzzle {广度优先搜索}(*)
- [挑战程序设计竞赛] AOJ 0121 - Seven Puzzle
- Seven Puzzle AOJ 0121宽度优先搜索
- Seven Puzzle (Aizu 0121 反向搜索bfs)
- Aizu 0121 Seven Puzzle
- AOJ0121——Seven Puzzle(BFS)
- AOJ 0121 bfs
- AOJ 0558 Cheese (bfs)
- 今天给大家分享ios中的夜间模式(通知中心)
- 【Android实战】实现新浪微博第三方登录获取用户信息
- USACO Ski Course Design
- Codeforces Round #291 (Div. 2)D.R2D2 and Droid Army——RMQ+二分
- hdoj1007(最近距离点对)Quoit Design
- AOJ 0121 Seven Puzzle (bfs)
- bzoj 1432: [ZJOI2009]Function
- 【JavaScript】JavaScript基础-变量、运算符与控制语句
- Java—Java多线程中join方法的理解
- F5负载均衡器 应用交付网络
- atitit.orm的缺点与orm框架市场占有率,选型attilax总结
- jdbc+记事本编程试水
- android ImageView scaleType属性
- POJ 2718 Smallest Difference (dfs)