正解太妙了
將6
66位上的數字壓縮成乙個int
intin
t,然後雙向 bfs
bfsbf
s 爆搜即可,卡卡常數勉強過去
由於**十分簡單, 所以就不 想 打注釋了
#include
#include
#include
#include
#include
#include
using
namespace std;
#define get(x, y) (x / w[y] % 10)
#define ll long long
#define ull unsigned long long
template
<
typename t>
void read (t &x)
while
(tem >=
'0'&& tem <=
'9')
x *= f;
}template
<
typename t>
void write (t x)
if(x >
9) write (x /10)
;putchar (x %10+
'0');}
template
<
typename t> t max (t x, t y)
template
<
typename t> t min (t x, t y)
template
<
typename t> t abs (t x)
const
int maxn =6;
const
int maxkind =
5999999
;int n =6;
int w[maxn +1]
=;char s_str[maxn]
, t_str[maxn]
;struct node s, t;
int hh =
1, tt =0;
queue q;
int vis[2]
[maxkind +5]
;int get_order (
char c,
int index)
int two_way_bfs (
)while
(q.size ())
now.val = last;}if
(now.index != n -1)
now.val = last;}if
(get (now.val, now.index)!=0
)
now.val = last;}if
(get (now.val, now.index)!=9
)
now.val = last;}if
(now.index !=0)
now.index++
; now.val = last;}if
(now.index != n -1)
now.index--
; now.val = last;}}
return-1
;}int main ()if
(flag)
cout << two_way_bfs ();
return0;
}
雖然我用法1卡了過去,但是看到了 zzb
zzbzz
b巨佬和14
1414
巨佬跑的快了不止10
1010
倍,所以我去網上搜了正解。
網上的題解寫的稀奇古怪,但我勉強弄明白了tata
ta所想表達的意思,照著tata
ta的思路順利的解決了這道題。
我們可以預處理,得到乙個三元組 (數列,游標位置,經過的原位置)
舉個例子
原數列: 1 2 3 4 5 6 游標: 1
預處理數列:2 1 3 4 5 6 游標:2 所需步數:2 經過的位置:原1, 2
方案:1.游標位置右移一位
2.交換 (1, 2)
然後我們在考慮用加減法
值會被改變的元素必定是被游標掃過的元素,所以我們可以在掃過的元素任意加減,加一次或減一次的價值是1
11 (相當於是在掃到這個元素時就對它進行加減,使其符合最終位置)
思路理清了,**就好寫了
參考**
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define ll long long
#define ull unsigned long long
#define move(x) (1 << x - 1)
#define get(x, y) ((x >> y - 1) & 1)
using
namespace std;
template
<
typename t>
int read (t &x)
while
(tem >=
'0'&& tem <=
'9')
x *= f;
return1;
}template
<
typename t>
void write (t x)
if(x >
9) write (x /10)
;putchar (x %10+
'0');}
template
<
typename t> t max (t x, t y)
template
<
typename t> t min (t x, t y)
template
<
typename t> t abs (t x)
const
int maxn =6;
const
int maxkind =
5040
;const
int inf =
0x3f3f3f3f
;int n =6;
int ans[maxkind *10+
6+5]
;int vis[maxkind *10+
6*5]
;struct node
;int fac[maxn +5]
=;struct cantor
int lowbit (
int x)
void update (
int index,
int x,
int*str)
int sum (
int x,
int*str)
int get_order (
) order = sum;
return sum;
}void get_seq ()if
(sum (l, bit)
== x) c[i]
= l;
else c[i]
= r;
update (c[i],-
1, bit);}
}};int get_order (node x)
} res +
= fac[n - i]
* cnt;
}int val = res *
10+ x.index;
return val;
}void bfs ()}
if(now.index !=1)
}if(now.index != n)}if
(now.index != n)}}
}char s[maxn +5]
, t[maxn +5]
;int main (
) res +
= abs (s[now.c[k]
]- t[k]);
//將相對的元素值修改
} cnt = min (cnt, res)
;//去最小值 }}
printf (
"%d"
, cnt)
;return0;
}
由於作者水平有限和腦子剛弄丟了,所以講解不清的地方請隨意@ 聰明的打字員
題目描述 阿蘭是某機密部門的打字員,她現在接到乙個任務 需要在一天之內輸入幾百個長度固定為6的密碼。當然,她希望輸入的過程中敲擊鍵盤的總次數越少越好。不幸的是,出於保密的需要,該部門用於輸入密碼的鍵盤是特殊設計的,鍵盤上沒有數字鍵,而只有以下六個鍵 swap0,swap1,up,down,left,...
聰明的打字員
題目描述 阿蘭是某機密部門的打字員,她現在接到乙個任務 需要在一天之內輸入幾百個長度固定為6的密碼。當然,她希望輸入的過程中敲擊鍵盤的總次數越少越好。不幸的是,出於保密的需要,該部門用於輸入密碼的鍵盤是特殊設計的,鍵盤上沒有數字鍵,而只有以下六個鍵 swap0,swap1,up,down,left,...
POJ 1184 聰明的打字員
簡直難到沒朋友。雙向bfs 剪枝。剪枝策略 對於2 5位置上的數,僅僅有當游標在相應位置時通過swap up。down來改變。那麼當當前位置沒有達到目標狀態時,left和right無意義。好了。僅僅剪掉這裡就過掉了。還有比較炫酷的方法實現列舉720種排列。然後狀壓什麼的。功力不夠全然看不懂。incl...