URAL 1016. Cube on the Walk(搜索)比较难
2012-08-27 09:53
471 查看
题意:给你一个立方体,每一个面都有一个数字。。从一个坐标滚到另一个坐标,使底面的数字之和最小。。
思路:搜索,对每一个坐标记录起底面和前面,,就有24种状态。。。
思路:搜索,对每一个坐标记录起底面和前面,,就有24种状态。。。
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <queue> using namespace std; const int INF = 0x3f3f3f3f; #define up 3,2,5,1 #define down 3,1,5,2 #define left 3,6,5,4 #define right 3,4,5,6 struct cub{ int a[7]; int v[7]; void oper(int i1,int i2,int i3,int i4) { int tmp = a[i1]; a[i1]=a[i4],a[i4]=a[i3],a[i3]=a[i2],a[i2]=tmp; tmp = v[i1]; v[i1]=v[i4],v[i4]=v[i3],v[i3]=v[i2],v[i2]=tmp; } }; struct nod{ int x,y,dis; cub t; bool operator<(const nod t) const { return dis>t.dis; } }; int dis[10][10][10][10]; int pre[10][10][10][10]; int Prev[10][10][10][10]; priority_queue<nod> que; bool oor(int x,int y) { if(x<0||x>7) return false; if(y<0||y>7) return false; return true; } int ans[109][2],cnt; int main() { freopen("in.txt","r",stdin); int stx,sty,enx,eny; char ch[5]; scanf("%s",ch); stx = ch[0]-'a';sty =ch[1]-'1'; scanf("%s",ch); enx = ch[0]-'a';eny =ch[1]-'1'; cub tmp; for(int i=1;i<=6;i++) scanf("%d",&tmp.a[i]),tmp.v[i]=i; nod e,t; e.x=stx,e.y=sty,e.dis=tmp.a[5];e.t = tmp; while(!que.empty()) que.pop(); que.push(e); memset(dis,-1,sizeof(dis)); dis[stx][sty][5][1] = tmp.a[5]; int anst1,anst2; while(!que.empty()) { e = que.top();que.pop(); // cout<<e.x<<" "<<e.y<<" "<<e.dis<<" v "<<e.t.v[5]<<endl; // for(int i=1;i<7;i++) cout<<e.t.a[i]<<" ";cout<<endl; //dis[e.x][e.y] = e.dis; if(e.x==enx&&e.y==eny) { anst1 = e.t.v[5]; anst2 = e.t.v[1]; break; } if(oor(e.x-1,e.y)) { t = e; t.x = e.x-1; t.t.oper(left); t.dis+=t.t.a[5]; if(dis[t.x][t.y][t.t.v[5]][t.t.v[1]]>t.dis||dis[t.x][t.y][t.t.v[5]][t.t.v[1]]==-1) { dis[t.x][t.y][t.t.v[5]][t.t.v[1]] = t.dis; pre[t.x][t.y][t.t.v[5]][t.t.v[1]] = 0; Prev[t.x][t.y][t.t.v[5]][t.t.v[1]] = e.t.v[5]*10+e.t.v[1]; que.push(t); } } if(oor(e.x+1,e.y)) { t = e; t.x = e.x+1; t.t.oper(right); t.dis+=t.t.a[5]; if(dis[t.x][t.y][t.t.v[5]][t.t.v[1]]>t.dis||dis[t.x][t.y][t.t.v[5]][t.t.v[1]]==-1) { dis[t.x][t.y][t.t.v[5]][t.t.v[1]] = t.dis; pre[t.x][t.y][t.t.v[5]][t.t.v[1]] = 1; Prev[t.x][t.y][t.t.v[5]][t.t.v[1]] =e.t.v[5]*10+e.t.v[1]; que.push(t); } } if(oor(e.x,e.y-1)) { t = e; t.y = e.y-1; t.t.oper(down); t.dis+=t.t.a[5]; if(dis[t.x][t.y][t.t.v[5]][t.t.v[1]]>t.dis||dis[t.x][t.y][t.t.v[5]][t.t.v[1]]==-1) { dis[t.x][t.y][t.t.v[5]][t.t.v[1]] = t.dis; pre[t.x][t.y][t.t.v[5]][t.t.v[1]] = 2; Prev[t.x][t.y][t.t.v[5]][t.t.v[1]] = e.t.v[5]*10+e.t.v[1]; que.push(t); } } if(oor(e.x,e.y+1)) { t = e; t.y = e.y+1; t.t.oper(up); t.dis+=t.t.a[5]; if(dis[t.x][t.y][t.t.v[5]][t.t.v[1]]>t.dis||dis[t.x][t.y][t.t.v[5]][t.t.v[1]]==-1) { dis[t.x][t.y][t.t.v[5]][t.t.v[1]] = t.dis; pre[t.x][t.y][t.t.v[5]][t.t.v[1]] = 3; Prev[t.x][t.y][t.t.v[5]][t.t.v[1]] = e.t.v[5]*10+e.t.v[1]; que.push(t); } } } //return 0; int tx = enx,ty = eny,td1 = anst1,td2=anst2; // cout<<enx<<" ++ "<<eny<<endl; //ans[cnt][0]=enx,ans[cnt][1]=eny; while(tx!=stx||ty!=sty||td1!=5||td2!=1) { // cout<<tx<<" "<<ty<<" "<<endl; int tp1,tp2; ans[cnt][0]=tx,ans[cnt][1]=ty; cnt++; if(pre[tx][ty][td1][td2]==0) tp1=Prev[tx][ty][td1][td2]/10,tp2=Prev[tx][ty][td1][td2]%10,tx++; else if(pre[tx][ty][td1][td2]==1) tp1=Prev[tx][ty][td1][td2]/10,tp2=Prev[tx][ty][td1][td2]%10,tx--; else if(pre[tx][ty][td1][td2]==2) tp1=Prev[tx][ty][td1][td2]/10,tp2=Prev[tx][ty][td1][td2]%10,ty++; else if(pre[tx][ty][td1][td2]==3) tp1=Prev[tx][ty][td1][td2]/10,tp2=Prev[tx][ty][td1][td2]%10,ty--; td1=tp1,td2=tp2; } ans[cnt][0]=stx,ans[cnt][1]=sty; printf("%d",dis[enx][eny][anst1][anst2]); for(;cnt>=0;cnt--) printf(" %c%d",ans[cnt][0]+'a',ans[cnt][1]+1); printf("\n"); return 0; }
相关文章推荐
- URAL 1016 Cube on the Walk 搜索题
- Ural 1016. Cube on the Walk (bfs滚骰子)
- URAL 1016. Cube on the Walk (分层图+最短路)
- ural 1016 Cube on the Walk
- URAL 1016 Cube on the Walk
- URAL 1016 Cube on the Walk (SPFA+HASH)
- 1016. Cube on the Walk
- pku3093 Margaritas on the River Walk
- ural 1207. Median on the Plane(极角排序)
- POJ - 3039 MARGARITAS ON THE RIVER WALK
- [POJ3093 Margaritas on the River Walk]
- POJ 3093 Margaritas on the River Walk
- 搜索 HOJ 1736 A Game on the Chessboard
- POJ 3093 Margaritas on the River Walk 笔记
- poj 3093 Margaritas on the River Walk
- poj - 3039 Margaritas on the River Walk
- pku 3039 Margaritas on the River Walk 背包dp 解题报告
- URAL 1948 H - The Robot on the Line 二分 + 数学
- POJ 3093 Margaritas(Kind of wine) on the River Walk (背包方案统计)
- UVa 122|HDU 1622|Trees on the level|搜索