ural 1016 Cube on the Walk 状态压缩最短路
来源:互联网 发布:2011网络春晚 翻唱传奇 编辑:程序博客网 时间:2024/06/05 00:42
http://acm.timus.ru/problem.aspx?space=1&num=1016
http://acm.hust.edu.cn:8080/judge/problem/viewProblem.action?id=14076
1016. Cube on the Walk
Time Limit: 2.0 second
Memory Limit: 16 MB
Memory Limit: 16 MB
A cube placed on some square of a usual chessboard. A cube completely covers one square of the chessboard but not anything more, i.e. size of cube’s edge is equal to the size of square’s edge. The integer number N (0 ≤ N ≤ 1000) is written on the each side of the cube. However it does not imply that the same number is written on all sides. On the different sides there are might be different numbers. One can move a cube to the next square by rotating it around the common edge of the cube and the square. During this motion the sum of the numbers on the bottom of the cube is calculated (each number is added as much times as it appeared at the bottom of the cube). Your task is to find the route between two given squares with the minimal sum of numbers on the bottom side. The numbers on the bottom at the beginning and at the end of walk are also counted. The start and the end positions are different.
Input
The only line of the input contains the necessary data set (only spaces used as delimiters). First, the start position is given, and then the end position. Each position is composed from the character (from ‘a’ to ‘h’ inclusively, it defines the number of the column on the chessboard) and the digit (from ‘1’ to ‘8’ inclusively, it defines the number of the row). That positions are followed by 6 numbers which are currently written on the near, far, top, right, bottom and left sides of the cube correspondingly.
Output
The only line of the output must contain the minimal sum followed by the optimal route (one of possible routes with minimal sum). The route must be represented by the sequence of cube’s positions during the walk. It begins with the start square and ends with the finish square. All square positions on the chessboard should be given in the same format as in input. Use spaces as delimiters.
Sample
e2 e3 0 8 1 2 1 1
5 e2 d2 d1 e1 e2 e3
前后上右下左init123456turnleft124563turnright126345turnup531426turndown352416
init:x=e[0]<<50|e[1]<<40|e[2]<<30|e[3]<<20|e[4]<<10|e[5]
turnleft: ((x&d1)|(x&d2)|(x&d4)<<10|(x&d5)<<10|(x&d6)<<10|(x&d3)>>30)
turnright:((x&d1)|(x&d2)|(x&d6)<<30|(x&d3)>>10|(x&d4)>>10|(x&d5)>>10)
turnup:((x&d5)<<40|(x&d3)<<10|(x&d1)>>20|(x&d4)|(x&d2)>>30|(x&d6))
turndown:((x&d3)<<20|(x&d5)<<30|(x&d2)>>10|(x&d4)|(x&d1)>>40|(x&d6))
然后就是最短路了
#include<iostream>#include<algorithm>#include<cstring>#include<fstream>#include<queue>#include<map>using namespace std;typedef long long ll;const ll d6=1023,d5=d6<<10,d4=d5<<10,d3=d4<<10,d2=d3<<10,d1=d2<<10;#define tl(x) ((x&d1)|(x&d2)|(x&d4)<<10|(x&d5)<<10|(x&d6)<<10|(x&d3)>>30)#define tr(x) ((x&d1)|(x&d2)|(x&d6)<<30|(x&d3)>>10|(x&d4)>>10|(x&d5)>>10)#define tu(x) ((x&d5)<<40|(x&d3)<<10|(x&d1)>>20|(x&d4)|(x&d2)>>30|(x&d6))#define td(x) ((x&d3)<<20|(x&d5)<<30|(x&d2)>>10|(x&d4)|(x&d1)>>40|(x&d6))map<ll,int>mm;struct Nod{ int x,y,d; ll s; Nod() {} Nod(int x,int y,int d,ll s):x(x),y(y),d(d),s(s) {} bool operator < (const Nod &p)const{return d>p.d;}};int d[10][10][30];bool vis[10][10][30];Nod pre[10][10][30];int dx[]= {0,-1,0,1};int dy[]= {-1,0,1,0}; //d l u rint dij(int sx,int sy,ll ss,int tx,int ty,ll &ts){ priority_queue<Nod>q; d[sx][sy][mm[ss]]=(ss&d5)>>10; q.push(Nod(sx,sy,(ss&d5)>>10,ss)); int ux,uy,ud,vx,vy,vd; ll us,vs; while(!q.empty()) { ux=q.top().x;uy=q.top().y; ud=q.top().d;us=q.top().s; q.pop(); if(ux==tx&&uy==ty){ts=us;return ud;} if(vis[ux][uy][mm[us]]) continue; vis[ux][uy][mm[us]]=1; for(int i=0; i<4; i++) { vx=ux+dx[i];vy=uy+dy[i]; if(vx<1||vx>8||vy<1||vy>8) continue; if(i==0) vs=td(us); if(i==1) vs=tl(us); if(i==2) vs=tu(us); if(i==3) vs=tr(us); vd=ud+((vs&d5)>>10); if(vd<d[vx][vy][mm[vs]]) { d[vx][vy][mm[vs]]=vd; q.push(Nod(vx,vy,vd,vs)); pre[vx][vy][mm[vs]]=Nod(ux,uy,mm[us],0); } } }}void path(Nod p)//print path{ if(p.x==-1) return; path(pre[p.x][p.y][p.d]); printf(" %c%c",p.x+96,p.y+48);}int main(){ memset(d,63,sizeof(d)); memset(vis,0,sizeof(vis)); memset(pre,-1,sizeof(pre)); char a,b,c,f; ll e[6]; cin>>a>>b>>c>>f; a-=96,b-=48,c-=96,f-=48; for(int i=0; i<6; i++) cin>>e[i]; ll st,x=e[0]<<50|e[1]<<40|e[2]<<30|e[3]<<20|e[4]<<10|e[5],xx=x; int i,j,k,cnt=0; for(i=0; i<4; i++,xx=tl(xx)) for(j=0; j<4; j++,xx=tu(xx)) for(k=0; k<4; k++,xx=tl(xx)) if(mm.find(xx)==mm.end()) mm[xx]=cnt++;//find all states cout<<dij(a,b,x,c,f,st); path(Nod(c,f,mm[st],0)); return 0;}
- ural 1016 Cube on the Walk 状态压缩最短路
- URAL 1016. Cube on the Walk (分层图+最短路)
- URAL 1016 Cube on the Walk (SPFA+HASH)
- URAL 1016 Cube on the Walk 搜索题
- URAL 1016. Cube on the Walk(搜索)比较难
- Ural 1016. Cube on the Walk (bfs滚骰子)
- poj3311 Hie with the Pie【floyd最短路+状态压缩】
- A Walk Through the Forest 最短路+dp
- hdu1142-A Walk Through the Forest-最短路+dfs
- A Walk Through the Forest(最短路+dfs)
- HDU 1142 A Walk Through the Forest 最短路+DFS
- Uva 10917 - Walk Through the Forest 最短路
- hdu1142A Walk Through the Forest (最短路+BFS+DFS )
- A Walk Through the Forest(hdu1142,最短路+dfs)
- hdu 1142 A Walk Through the Forest 最短路
- UVA 10917 Walk Through the Forest(最短路+dp)
- 【UVa】10917 A Walk Through the Forest 最短路+DP
- UVA - 10917 Walk Through the Forest (最短路+DP)
- 元数据
- linux 手动挂载U盘..
- OLAP与数据挖掘的区别
- 只是需要你的钱在非常时刻
- oracle存储过程实现JAVA调用的分页
- ural 1016 Cube on the Walk 状态压缩最短路
- 联合体取值
- 托管代码和非托管代码
- ASP.NET路由系统实现原理:HttpHandler的动态映射
- zz - reprint with rework Definining resources at the theme level
- 应将是寻找企业能够平稳运行
- Linux下常用cvs命令整理
- MySQL索引类型一览 让MySQL高效运行起来
- ORACLE 9I数据库EXP排除部分表导出