Title Link: http://www.lydsy.com:808/JudgeOnline/problem.php?id=1054
It's a water problem, thanks to Haoi's welfare sample 23333
Bare BFS, using string to do the weighing, will be eight digital will this problem.
Note Because there are a lot of states in the queue, be sure to turn the array of queues up a bit!!!
#include <iostream> #include <stdio.h> #include <stdlib.h> #include <string.h> #include < string> #include <algorithm> #include <map> #define MAXN 5using namespace std;map<string,bool> Visit;int tmp[maxn][maxn],nowstatus[maxn][maxn];int xx[]={1,-1,0,0},yy[]={0,0,1,-1};bool inMap (int x,int y) {if (x <1| | x>4| | y<1| | Y>4) return false; return true;} String Getpermutationfromarray () {string ans= ""; for (int. i=1;i<=4;i++) for (int j=1;j<=4;j++) ans+=tmp[i][j]+ ' 0 '; return ans;} void Getarrayfrompermutaion (string s) {int p=0; for (int i=1;i<=4;i++) for (int j=1;j<=4;j++) tmp[i][j]=s[p++]-' 0 ';} struct node{string status; int step;} Q[100000],first,goal;int h=0,t=0;int BFS () {q[t++]=first; Visit[first.status]=true; while (h<t) {node now=q[h++]; if (now.status==goal.status) return now.step; Getarrayfrompermutaion (Now.status); for (int x=1;x<=4;x++) for (int y=1;y<=4;y++) {if (tmp[x][y]==1)//(x, y) is toy { for (int dir=0;dir<4;dir++) {int tx=x+xx[dir],ty=y+yy[dir]; if (!inmap (tx,ty)) continue; if (tmp[tx][ty]==0)//(Tx,ty) is open space {node Next; next.step=now.step+1; Swap (Tmp[x][y],tmp[tx][ty]); Next.status=getpermutationfromarray (); if (!visit[next.status]) {visit[next.status]=true; Q[t++]=next; } swap (Tmp[x][y],tmp[tx][ty]); }}}}} return-1;} int main () {string S; First.step=0; for (int i=1;i<=4;i++) {cin>>s; First.status+=s; } for (int i=1;i<=4;i++) {cin>>s; Goal.status+=s; } printf ("%d\n", BFS ()); return 0;}
[Bzoj 1054] [Haoi 2008] Mobile Toys (bfs+ weighing)