hdu3900 Unblock Me
来源:互联网 发布:java虚拟机最大内存 编辑:程序博客网 时间:2024/05/17 23:09
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
120 0 1 0 21 1 0 1 12 2 0 2 13 3 0 5 04 1 2 2 25 3 1 3 26 4 1 4 27 5 1 5 38 0 3 1 39 2 3 3 310 4 3 4 411 0 4 0 54
Sample Output
16HintSee the image below to get more details.
Source
2011 Multi-University Training Contest 7 - Host by ECNU
由于每一个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;}
0 0
- hdu3900 Unblock Me
- HDOJ 3900 Unblock Me
- HDU 3900 Unblock Me
- HDU3900 状压BFS
- ME
- me
- ME
- Me
- ME
- me
- me
- me
- Me
- me
- me
- me
- Me
- me
- C/C++ 嵌套结构体动态内存管理实现
- bash ${ } 变量替换用法总结
- 移动应用开发之Java+Eclipse环境搭建
- ubuntu 12.04中搭建nfs服务器
- SPAN和DIV Class与ID的区别汇总
- hdu3900 Unblock Me
- 喵呜...
- hdu1850 Being a Good Boy in Spring Festival nim博弈
- mesademos loaddown
- CSS基础
- 通过调用qt的assistant来实现自定义文档(qhc格式)
- Maximum Depth of Binary Tree
- Tomcat 源码研究(1)环境搭建
- 移动开发之Xamarin+C#环境搭建