[BFS + 打表] HDU 3567
2017-11-08 19:40
357 查看
先枚举X不同位置的9种方案,用bfs搜出这种方案到其他所有方案的移动方法
打表的胜利....
#include <algorithm>
#include <iostream>
#include <queue>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
using namespace std;
struct Node {
int x, y;
char map[ 3 ][ 3 ];
Node () {}
Node ( string s ) {
for ( int i = 0; i < (int)s.size (); ++i ) {
map[ i / 3 ][ i % 3 ] = s[ i ];
if ( s[ i ] == 'X' )
x = i / 3, y = i % 3;
}
}
};
Node start;
char str[ 20 ];
int factorial[ 9 ] = {1, 1, 2, 6, 24, 120, 720, 5040, 40320};
int cantor ( Node a ) //康拓
{
int i, j, k, cnt, sum = 0;
int b[ 20 ];
for ( i = 0; i < 3; i++ ) {
for ( j = 0; j < 3; j++ ) {
b[ 3 * i + j ] = a.map[ i ][ j ];
cnt = 0;
for ( k = 3 * i + j - 1; k >= 0; k-- ) {
if ( b[ k ] > b[ 3 * i + j ] )
cnt++;
}
sum += factorial[ 3 * i + j ] * cnt;
}
}
return sum;
}
int num[ 20 ], Hash[ 10 ];
bool vis[ 500000 ];
int pre[ 10 ][ 500000 ], ch[ 10 ][ 500000 ];
int dir[ 4 ][ 2 ] = {{1, 0}, {0, -1}, {0, 1}, {-1, 0}};
char way[ 10 ] = "dlru";
void bfs ( int p ) {
memset ( pre[ p ], -1, sizeof ( pre[ p ] ) );
memset ( vis, false, sizeof ( vis ) );
Node cur, nex;
queue<Node> Q;
Q.push ( start );
vis[ cantor ( start ) ] = true;
while ( !Q.empty () ) {
cur = Q.front ();
Q.pop ();
int sa = cantor ( cur );
for ( int i = 0; i < 4; i++ ) {
nex = cur;
nex.x += dir[ i ][ 0 ];
nex.y += dir[ i ][ 1 ];
if ( nex.x < 0 || nex.x > 2 || nex.y < 0 || nex.y > 2 )
continue;
nex.map[ cur.x ][ cur.y ] = nex.map[ nex.x ][ nex.y ];
nex.map[ nex.x ][ nex.y ] = 'X';
int sb = cantor ( nex );
if ( vis[ sb ] )
continue;
vis[ sb ] = true;
pre[ p ][ sb ] = sa;
ch[ p ][ sb ] = way[ i ];
Q.push ( nex );
}
}
}
int main () {
int t, i, j, cas = 1;
start = Node ( "X12345678" );
bfs ( 0 );
start = Node ( "1X2345678" );
bfs ( 1 );
start = Node ( "12X345678" );
bfs ( 2 );
start = Node ( "123X45678" );
bfs ( 3 );
start = Node ( "1234X5678" );
bfs ( 4 );
start = Node ( "12345X678" );
bfs ( 5 );
start = Node ( "123456X78" );
bfs ( 6 );
start = Node ( "1234567X8" );
bfs ( 7 );
start = Node ( "12345678X" );
bfs ( 8 );
scanf ( "%d", &t );
while ( t-- ) {
scanf ( "%s", str );
int p;
for ( i = 0, j = 0; i < 9; i++ ) //保存位置,因为前面预处理的都是位置
{
if ( str[ i ] == 'X' )
p = i;
else
num[ str[ i ] - '0' ] = j++;
}
scanf ( "%s", str );
for ( i = 0; i < 9; i++ ) //求出目标状态每个数在原状态的位置
{
if ( str[ i ] == 'X' )
continue;
str[ i ] = num[ str[ i ] - '0' ] + '1';
}
//得出来的str是目标串的每个数字对应在开始串里的位置
//位置变成12345678就代表和初始状态相同了
start = Node ( str );
int sum = cantor ( start );
string ss = "";
while ( sum != -1 ) {
ss += ch[ p ][ sum ];
su
a676
m = pre[ p ][ sum ];
}
printf ( "Case %d: %d\n", cas++, (int)ss.size () - 1 );
for ( i = ss.size () - 2; i >= 0; i-- )
printf ( "%c", ss[ i ] );
printf ( "\n" );
}
return 0;
}
打表的胜利....
#include <algorithm>
#include <iostream>
#include <queue>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
using namespace std;
struct Node {
int x, y;
char map[ 3 ][ 3 ];
Node () {}
Node ( string s ) {
for ( int i = 0; i < (int)s.size (); ++i ) {
map[ i / 3 ][ i % 3 ] = s[ i ];
if ( s[ i ] == 'X' )
x = i / 3, y = i % 3;
}
}
};
Node start;
char str[ 20 ];
int factorial[ 9 ] = {1, 1, 2, 6, 24, 120, 720, 5040, 40320};
int cantor ( Node a ) //康拓
{
int i, j, k, cnt, sum = 0;
int b[ 20 ];
for ( i = 0; i < 3; i++ ) {
for ( j = 0; j < 3; j++ ) {
b[ 3 * i + j ] = a.map[ i ][ j ];
cnt = 0;
for ( k = 3 * i + j - 1; k >= 0; k-- ) {
if ( b[ k ] > b[ 3 * i + j ] )
cnt++;
}
sum += factorial[ 3 * i + j ] * cnt;
}
}
return sum;
}
int num[ 20 ], Hash[ 10 ];
bool vis[ 500000 ];
int pre[ 10 ][ 500000 ], ch[ 10 ][ 500000 ];
int dir[ 4 ][ 2 ] = {{1, 0}, {0, -1}, {0, 1}, {-1, 0}};
char way[ 10 ] = "dlru";
void bfs ( int p ) {
memset ( pre[ p ], -1, sizeof ( pre[ p ] ) );
memset ( vis, false, sizeof ( vis ) );
Node cur, nex;
queue<Node> Q;
Q.push ( start );
vis[ cantor ( start ) ] = true;
while ( !Q.empty () ) {
cur = Q.front ();
Q.pop ();
int sa = cantor ( cur );
for ( int i = 0; i < 4; i++ ) {
nex = cur;
nex.x += dir[ i ][ 0 ];
nex.y += dir[ i ][ 1 ];
if ( nex.x < 0 || nex.x > 2 || nex.y < 0 || nex.y > 2 )
continue;
nex.map[ cur.x ][ cur.y ] = nex.map[ nex.x ][ nex.y ];
nex.map[ nex.x ][ nex.y ] = 'X';
int sb = cantor ( nex );
if ( vis[ sb ] )
continue;
vis[ sb ] = true;
pre[ p ][ sb ] = sa;
ch[ p ][ sb ] = way[ i ];
Q.push ( nex );
}
}
}
int main () {
int t, i, j, cas = 1;
start = Node ( "X12345678" );
bfs ( 0 );
start = Node ( "1X2345678" );
bfs ( 1 );
start = Node ( "12X345678" );
bfs ( 2 );
start = Node ( "123X45678" );
bfs ( 3 );
start = Node ( "1234X5678" );
bfs ( 4 );
start = Node ( "12345X678" );
bfs ( 5 );
start = Node ( "123456X78" );
bfs ( 6 );
start = Node ( "1234567X8" );
bfs ( 7 );
start = Node ( "12345678X" );
bfs ( 8 );
scanf ( "%d", &t );
while ( t-- ) {
scanf ( "%s", str );
int p;
for ( i = 0, j = 0; i < 9; i++ ) //保存位置,因为前面预处理的都是位置
{
if ( str[ i ] == 'X' )
p = i;
else
num[ str[ i ] - '0' ] = j++;
}
scanf ( "%s", str );
for ( i = 0; i < 9; i++ ) //求出目标状态每个数在原状态的位置
{
if ( str[ i ] == 'X' )
continue;
str[ i ] = num[ str[ i ] - '0' ] + '1';
}
//得出来的str是目标串的每个数字对应在开始串里的位置
//位置变成12345678就代表和初始状态相同了
start = Node ( str );
int sum = cantor ( start );
string ss = "";
while ( sum != -1 ) {
ss += ch[ p ][ sum ];
su
a676
m = pre[ p ][ sum ];
}
printf ( "Case %d: %d\n", cas++, (int)ss.size () - 1 );
for ( i = ss.size () - 2; i >= 0; i-- )
printf ( "%c", ss[ i ] );
printf ( "\n" );
}
return 0;
}
相关文章推荐
- (多回顾)hdu 3567 Eight2 (bfs逆处理+打表+映射+康托压缩~)
- hdu 1430 bfs + 打表 + 康拓展开
- HDU 3567 Eight II 打表,康托展开,bfs,g++提交可过c++不可过 难度:3
- HDU 3567 - Eight II
- HDU 1043 Eight(八数码第三境界|广搜+哈希+打表)
- HDU 1846 Brave Game 巴什博弈 SG打表
- 【HDU 1495】 非常可乐 (bfs + 模拟)
- hdu 4291 A Short problem 打表找规律&矩阵快速幂
- hdu Humble Numbers(打表)
- hdu 2136 Largest prime factor(打表)
- HDU 1239 Calling Extraterrestrial Intelligence Again 【打表+暴力搜索】
- http://acm.hdu.edu.cn/showproblem.php?pid=3524 (奇偶矩阵乘法 打表)
- hdu 3032 sg打表找规律 *
- HDU 4919 打表找规律 java大数 map 递归
- hdu 1331 (暴力打表)
- HDU 3567 Eight II (搜索)
- HDU 2136 Largest prime factor (素数打表。。。)
- hdu 4548 筛法求素数 打表
- HDU - 5793 A Boring Question 数学(打表找规律)
- HDU 2563 统计问题 (DFS + 打表)