题目大意:滚骰子游戏,骰子的上面的点数跟方格中的数相同时或格子中的数是-1时能把格子滚过去,找一条从起点滚到起点的路径。

题目大意:简单BFS,状态转移时细心一些即可。

代码如下;

# include<iostream>
# include<cstdio>
# include<map>
# include<string>
# include<queue>
# include<cstring>
# include<algorithm>
using namespace std; struct Node
{
int x,y,s,t;
string px,py;
Node(int _x,int _y,int _s,int _t,string _px,string _py):x(_x),y(_y),s(_s),t(_t),px(_px),py(_py){}
bool operator < (const Node &a) const {
return t>a.t;
}
};
int Left[50],Right[50],vis[10][10][50],mp[10][10],r,c;
int behind[6]={6,5,4,3,2,1};
int d[4][2]={{-1,0},{1,0},{0,-1},{0,1}};
string name; void init()
{
Left[9]=4,Right[9]=3;
Left[12]=3,Right[12]=4;
Left[10]=2,Right[10]=5;
Left[11]=5,Right[11]=2; Left[15]=3,Right[15]=4;
Left[20]=4,Right[20]=3;
Left[18]=1,Right[18]=6;
Left[17]=6,Right[17]=1; Left[22]=5,Right[22]=2;
Left[27]=2,Right[27]=5;
Left[23]=1,Right[23]=6;
Left[26]=6,Right[26]=1; Left[29]=2,Right[29]=5;
Left[34]=5,Right[34]=2;
Left[30]=6,Right[30]=1;
Left[33]=1,Right[33]=6; Left[36]=4,Right[36]=3;
Left[41]=3,Right[41]=4;
Left[38]=1,Right[38]=6;
Left[39]=6,Right[39]=1; Left[44]=3,Right[44]=4;
Left[47]=4,Right[47]=3;
Left[46]=2,Right[46]=5;
Left[45]=5,Right[45]=2;
} bool ok(int x,int y)
{
return x>=0&&x<r&&y>=0&&y<c;
} void bfs(int sx,int sy,int ss)
{
priority_queue<Node>q;
memset(vis,0,sizeof(vis));
string path="";
q.push(Node(sx,sy,ss,0,path+(char)(sx+'A'),path+(char)(sy+'A')));
while(!q.empty())
{
Node u=q.top();
q.pop(); if(u.t&&u.x==sx&&u.y==sy){
int l=u.px.size();
for(int i=0;i<l;++i){
if(i==0)
printf(" ");
printf("(%d,%d)",u.px[i]-'A'+1,u.py[i]-'A'+1);
if(i==l-1)
printf("\n");
else if(i%9==8)
printf(",\n ");
else
printf(",");
}
return ;
} int dd[4]={u.s%7,behind[u.s%7-1],Right[u.s],Left[u.s]};
for(int i=0;i<4;++i){
int nx=u.x+d[i][0],ny=u.y+d[i][1];
if(ok(nx,ny)&&(mp[nx][ny]==(u.s/7)||mp[nx][ny]==-1)){
int k=u.s%7;
if(i==0)
k=behind[u.s/7-1];
if(i==1)
k=u.s/7;
if(!vis[nx][ny][dd[i]*7+k]){
vis[nx][ny][dd[i]*7+k]=1;
q.push(Node(nx,ny,dd[i]*7+k,u.t+1,u.px+(char)(nx+'A'),u.py+(char)(ny+'A')));
}
}
}
}
printf(" No Solution Possible\n");
} int main()
{
//freopen("UVA-810 A Dicey Problem.txt","r",stdin);
int sx,sy,st,sf;
init();
while(cin>>name)
{
if(name=="END")
break;
scanf("%d%d%d%d%d%d",&r,&c,&sx,&sy,&st,&sf);
for(int i=0;i<r;++i)
for(int j=0;j<c;++j)
scanf("%d",&mp[i][j]);
cout<<name<<endl;
bfs(sx-1,sy-1,st*7+sf);
}
return 0;
}

  

UVA-810 A Dicey Problem (BFS)的更多相关文章

  1. UVA 810 - A Dicey Problem(BFS)

    UVA 810 - A Dicey Problem 题目链接 题意:一个骰子,给你顶面和前面.在一个起点,每次能移动到周围4格,为-1,或顶面和该位置数字一样,那么问题来了,骰子能不能走一圈回到原地, ...

  2. UVa 1363 - Joseph's Problem(数论)

    链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  3. UVa 1640 - The Counting Problem(数论)

    链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  4. UVa 439骑士的移动(BFS)

    https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  5. UVa 816 Abbott的复仇(BFS)

    寒假的第一道题目,在放假回家颓废了两天后,今天终于开始刷题了.希望以后每天也能多刷几道题. 题意:这道BFS题还是有点复杂的,给一个最多9*9的迷宫,但是每个点都有不同的方向,每次进入该点的方向不同, ...

  6. Uva - 810 - A Dicey Problem

    根据状态进行bfs,手动打表维护骰子滚动. AC代码: #include <iostream> #include <cstdio> #include <cstdlib&g ...

  7. UVA - 816 Abbott's Revenge(bfs)

    题意:迷宫从起点走到终点,进入某点的朝向不同,可以出去的方向也不同,输出最短路. 分析:因为朝向决定接下来在该点可以往哪里走,所以每个点需要有三个信息:x,y,d(坐标和进入该点的朝向),所以将起点的 ...

  8. SDUT OJ 数据结构实验之图论五:从起始点到目标点的最短步数(BFS)

    数据结构实验之图论五:从起始点到目标点的最短步数(BFS) Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss P ...

  9. SDUT-2139_从起始点到目标点的最短步数(BFS)

    数据结构实验之图论五:从起始点到目标点的最短步数(BFS) Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 在古老的魔兽 ...

随机推荐

  1. Use of ‘const’ in Functions Return Values

    Use of 'Const' in Function Return Values 为什么要在函数的返回值类型中添加Const? 1.Features Of the possible combinati ...

  2. 清空messages方法

    1.du -sh /var/log/messages 2.losf /var/log/messages 3.cat /dev/null > /var/log/messages 4.du -sh ...

  3. Allocation Sinking Optimization

    LuaJIT Sponsorship Program http://luajit.org/sponsors.html Sponsorship for allocation/store sinking ...

  4. Qunar机票技术部就有一个全年很关键的一个指标:搜索缓存命中率,当时已经做到了>99.7%。再往后,每提高0.1%,优化难度成指数级增长了。哪怕是千分之一,也直接影响用户体验,影响每天上万张机票的销售额。 在高并发场景下,提供了保证线程安全的对象、方法。比如经典的ConcurrentHashMap,它比起HashMap,有更小粒度的锁,并发读写性能更好。线程安全的StringBuilder取代S

    Qunar机票技术部就有一个全年很关键的一个指标:搜索缓存命中率,当时已经做到了>99.7%.再往后,每提高0.1%,优化难度成指数级增长了.哪怕是千分之一,也直接影响用户体验,影响每天上万张机 ...

  5. BBS - 后台管理

    一.添加文章 注: 后台管理页面,应该有个新得 app /blog/backend/ # 文章列表页/blog/add_article/ # 添加文章 # 后台管理re_path(r'backend/ ...

  6. 转!!mysql 字段 is not null 和 字段 !=null

      今天在查询数据时,查到包含一条某个时间startTime(该字段默认为null ) 为null的记录,想把它过滤,加了 startTime != null 的条件,结果记录都没了,应该用条件 is ...

  7. docker 离线环境安装oracle

    因测试需要,需在内网的测试环境搭建一套docker Oracle 11g环境进行测试,测试环境为redhat 6.6 安装docker 1.7,本机windows 7 环境,安装docker 17.1 ...

  8. apache ab test使用 单独安装ab和htpasswd

    apache ab test使用 apache ab test使用 单独安装ab和htpasswd 转载自: http://www.cnblogs.com/super-d2/p/3831155.htm ...

  9. 7.如何将python脚本打包为exe形式

    先安装pyinstaller,pip install pyinstaller 然后 pyinstaller -F combine.py打包即可

  10. Spark Streaming带状态更新

    带状态的更新是使用的updateStateByKey方法,里面传入一个函数,函数要自己写,注意需要设置checkpoint import org.apache.spark.streaming.kafk ...