【BZOJ】1054: [HAOI2008]移動玩具(bfs+hash)

來源:互聯網
上載者:User

標籤:des   blog   http   os   io   for   ar   資料   div   

http://www.lydsy.com/JudgeOnline/problem.php?id=1054

一開始我還以為要雙向廣搜。。。。但是很水的資料,不需要了。

直接bfs+hash判重即可。

#include <cstdio>#include <cstring>#include <cmath>#include <string>#include <iostream>#include <algorithm>using namespace std;#define rep(i, n) for(int i=0; i<(n); ++i)#define for1(i,a,n) for(int i=(a);i<=(n);++i)#define for2(i,a,n) for(int i=(a);i<(n);++i)#define for3(i,a,n) for(int i=(a);i>=(n);--i)#define for4(i,a,n) for(int i=(a);i>(n);--i)#define CC(i,a) memset(i,a,sizeof(i))#define read(a) a=getint()#define print(a) printf("%d", a)#define dbg(x) cout << #x << " = " << x << endl#define printarr(a, n, m) rep(aaa, n) { rep(bbb, m) cout << a[aaa][bbb]; cout << endl; }inline const int getint() { int r=0, k=1; char c=getchar(); for(; c<‘0‘||c>‘9‘; c=getchar()) if(c==‘-‘) k=-1; for(; c>=‘0‘&&c<=‘9‘; c=getchar()) r=r*10+c-‘0‘; return k*r; }inline const int max(const int &a, const int &b) { return a>b?a:b; }inline const int min(const int &a, const int &b) { return a<b?a:b; }const int N=1000000, fx[]={-1, 1, 0, 0}, fy[]={0, 0, -1, 1};inline const bool check(const int &x, const int &y) { if(x<0 || x>3 || y<0 || y>3) return 0; return 1; }struct Matrix {bool m[4][4];int d;}q[N], ini;int front, tail, ans, vis[100000];inline int hash(const Matrix &x) {int k=1, ret=0;rep(i, 4) rep(j, 4) { ret+=k*x.m[i][j]; k<<=1; }return ret;}int main() {char c[10];tail=1;rep(i, 4) {scanf("%s", c);rep(j, 4)q[0].m[i][j]=c[j]-‘0‘;}rep(i, 4) {scanf("%s", c);rep(j, 4)ini.m[i][j]=c[j]-‘0‘;}int hs;ans=hash(ini);hs=hash(q[0]);if(ans==hs) { printf("0"); return 0; }vis[hs]=1;while(front!=tail) {Matrix &x=q[front++]; if(front==N) front=0;rep(i, 4) rep(j, 4) if(x.m[i][j]) {rep(k, 4) {int px=fx[k]+i, py=fy[k]+j;if(check(px, py) && x.m[px][py]==0) {q[tail]=x; ++q[tail].d;swap(q[tail].m[i][j], q[tail].m[px][py]);hs=hash(q[tail]);if(hs==ans) {printf("%d", q[tail].d);return 0;}if(vis[hs]) continue;vis[hs]=1;++tail; if(tail==N) tail=0;}}}}return 0;}

 

 

 

 

Description

在一個4*4的方框內擺放了若干個相同的玩具,某人想將這些玩具重新擺放成為他心中理想的狀態,規定移動時只能將玩具向上下左右四個方向移動,並且移動的位置不能有玩具,請你用最少的移動次數將初始的玩具狀態移動到某人心中的目標狀態。

Input

前4行表示玩具的初始狀態,每行4個數字1或0,1表示方格中放置了玩具,0表示沒有放置玩具。接著是一個空行。接下來4行表示玩具的目標狀態,每行4個數字1或0,意義同上。

Output

一個整數,所需要的最少移動次數。

Sample Input1111
0000
1110
0010

1010
0101
1010
0101
Sample Output4
HINTSource

【BZOJ】1054: [HAOI2008]移動玩具(bfs+hash)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.