Unblock Me
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 125536/65536 K (Java/Others)Total Submission(s): 275 Accepted Submission(s): 150
Problem Description
Unblock Me is a simple and addictive puzzle game on iPhone/iPod Touch. The goal is to get the red block out of the board by sliding the other blocks out of the way.
You can only move the block if it has a free space to move. In one step, you can move only one block, and you can move multiple units. To clear the puzzle, you have to move the red block out of the board. The red block can only move in horizontal direction. The vertical blocks can move in vertical direction only. The horizontal blocks can move in horizontal direction only.
Now try to crack it. The board is 6*6. There are two kinds of vertical blocks (1*2, 1*3), and two kinds of horizontal blocks (2*1, 3*1). The exit is always at the right of the grid (5, 2).
You can only move the block if it has a free space to move. In one step, you can move only one block, and you can move multiple units. To clear the puzzle, you have to move the red block out of the board. The red block can only move in horizontal direction. The vertical blocks can move in vertical direction only. The horizontal blocks can move in horizontal direction only.
Now try to crack it. The board is 6*6. There are two kinds of vertical blocks (1*2, 1*3), and two kinds of horizontal blocks (2*1, 3*1). The exit is always at the right of the grid (5, 2).
Input
There are several cases in the input data.
For each case:
The first line contains a number N which indicates how many blocks are on the board, then N lines follows. Each line contains five numbers. The first number is the index of this block, which increase from 0 to N-1. The next two numbers is the coordinate of the left upper corner. The last two numbers is the coordinate of the right down corner.
The last line contains one number which is the red block’s index.
For each case:
The first line contains a number N which indicates how many blocks are on the board, then N lines follows. Each line contains five numbers. The first number is the index of this block, which increase from 0 to N-1. The next two numbers is the coordinate of the left upper corner. The last two numbers is the coordinate of the right down corner.
The last line contains one number which is the red block’s index.
Output
For each case, output an integer in one line, which indicates the minimal steps to crack it.
A solution always exists.
A solution always exists.
Sample Input
12 0 0 1 0 2 1 1 0 1 1 2 2 0 2 1 3 3 0 5 0 4 1 2 2 2 5 3 1 3 2 6 4 1 4 2 7 5 1 5 3 8 0 3 1 3 9 2 3 3 3 10 4 3 4 4 11 0 4 0 5 4
Sample Output
16HintSee the image below to get more details.
Source
由于每一个block最多只能移4,5个位置。所以可以将其状态压缩
进行普通的bfs即可求出解。
p.s. 初始状态情况下的答案为1
#include<cstdio>
#include<cstring>
#include<iostream>
#include<queue>
#include<set>
using namespace std;
typedef long long LL;
struct Block
{
LL r,c,len,s;
};
struct data
{
LL s;
int d;
};
Block block[22];
bool bz[7][7];
int R,n;
set < LL > S;
queue < data > q;
bool check( LL state )
{
LL pos= ( state & (7LL<<3*R) ) >> 3*R;
if (pos+block[R].len-1==5) return true;
else
return false;
}
bool pd ( LL state )
{
memset(bz,0,sizeof(bz));
for (int i=0; i<n; i++)
{
LL pos= ( state & (7LL<<3*i)) >> 3*i;
if (!block[i].s)
{
for (LL j=0; j<block[i].len; j++)
if (bz[block[i].r][pos+j])
return false;
else
bz[block[i].r][pos+j]=1;
}
else
{
for (LL j=0; j<block[i].len; j++)
if (bz[pos+j][block[i].c])
return false;
else
bz[pos+j][block[i].c]=1;
}
}
return true;
}
int bfs( LL state )
{
while ( !q.empty() ) q.pop();
q.push( (data){state, 0} );
S.clear();
S.insert(state);
if ( check( state ) ) return 1;
while ( !q.empty() )
{
data u=q.front();
q.pop();
for (int i=0; i<n; i++)
{
data v;
LL pos= ( u.s & (7LL<<3*i) ) >> 3*i;
for (LL j= pos-1; j>=0; j--)
{
v.s = ( u.s & (~(7LL<<3*i)) ) | (j<<3*i);
v.d = u.d + 1;
if ( !pd( v.s ) ) break;
if ( S.count(v.s) ) continue;
if ( check( v.s) ) return v.d;
S.insert(v.s);
q.push(v);
}
for (LL j=pos+1; j+block[i].len-1<6; j++)
{
v.s = ( u.s & (~(7LL<<3*i)) ) | (j<<3*i);
v.d = u.d + 1;
if ( !pd( v.s ) ) break;
if ( S.count(v.s) ) continue;
if ( check( v.s) ) return v.d;
S.insert(v.s);
q.push(v);
}
}
}
}
int main()
{
while (scanf("%d",&n)!=EOF)
{
for (int i=0; i<n; i++)
{
int lx,ly,rx,ry;
scanf("%d%d%d%d%d",&R,&lx,&ly,&rx,&ry);
if (lx==rx)
{
block[i].r=lx;
block[i].c=ly;
block[i].len=ry-ly+1;
block[i].s=0;
}
if (ly==ry)
{
block[i].c=ly;
block[i].r=lx;
block[i].len=rx-lx+1;
block[i].s=1;
}
}
scanf("%d",&R);
LL state=0;
for (int i=0; i<n; i++)
{
if ( !block[i].s)
state+= ( block[i].c << (3*i) );
else
state+= ( block[i].r << (3*i) );
}
printf("%d\n",bfs ( state ) );
}
return 0;
}