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

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

代码如下;

  1. # include<iostream>
  2. # include<cstdio>
  3. # include<map>
  4. # include<string>
  5. # include<queue>
  6. # include<cstring>
  7. # include<algorithm>
  8. using namespace std;
  9.  
  10. struct Node
  11. {
  12. int x,y,s,t;
  13. string px,py;
  14. 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){}
  15. bool operator < (const Node &a) const {
  16. return t>a.t;
  17. }
  18. };
  19. int Left[50],Right[50],vis[10][10][50],mp[10][10],r,c;
  20. int behind[6]={6,5,4,3,2,1};
  21. int d[4][2]={{-1,0},{1,0},{0,-1},{0,1}};
  22. string name;
  23.  
  24. void init()
  25. {
  26. Left[9]=4,Right[9]=3;
  27. Left[12]=3,Right[12]=4;
  28. Left[10]=2,Right[10]=5;
  29. Left[11]=5,Right[11]=2;
  30.  
  31. Left[15]=3,Right[15]=4;
  32. Left[20]=4,Right[20]=3;
  33. Left[18]=1,Right[18]=6;
  34. Left[17]=6,Right[17]=1;
  35.  
  36. Left[22]=5,Right[22]=2;
  37. Left[27]=2,Right[27]=5;
  38. Left[23]=1,Right[23]=6;
  39. Left[26]=6,Right[26]=1;
  40.  
  41. Left[29]=2,Right[29]=5;
  42. Left[34]=5,Right[34]=2;
  43. Left[30]=6,Right[30]=1;
  44. Left[33]=1,Right[33]=6;
  45.  
  46. Left[36]=4,Right[36]=3;
  47. Left[41]=3,Right[41]=4;
  48. Left[38]=1,Right[38]=6;
  49. Left[39]=6,Right[39]=1;
  50.  
  51. Left[44]=3,Right[44]=4;
  52. Left[47]=4,Right[47]=3;
  53. Left[46]=2,Right[46]=5;
  54. Left[45]=5,Right[45]=2;
  55. }
  56.  
  57. bool ok(int x,int y)
  58. {
  59. return x>=0&&x<r&&y>=0&&y<c;
  60. }
  61.  
  62. void bfs(int sx,int sy,int ss)
  63. {
  64. priority_queue<Node>q;
  65. memset(vis,0,sizeof(vis));
  66. string path="";
  67. q.push(Node(sx,sy,ss,0,path+(char)(sx+'A'),path+(char)(sy+'A')));
  68. while(!q.empty())
  69. {
  70. Node u=q.top();
  71. q.pop();
  72.  
  73. if(u.t&&u.x==sx&&u.y==sy){
  74. int l=u.px.size();
  75. for(int i=0;i<l;++i){
  76. if(i==0)
  77. printf(" ");
  78. printf("(%d,%d)",u.px[i]-'A'+1,u.py[i]-'A'+1);
  79. if(i==l-1)
  80. printf("\n");
  81. else if(i%9==8)
  82. printf(",\n ");
  83. else
  84. printf(",");
  85. }
  86. return ;
  87. }
  88.  
  89. int dd[4]={u.s%7,behind[u.s%7-1],Right[u.s],Left[u.s]};
  90. for(int i=0;i<4;++i){
  91. int nx=u.x+d[i][0],ny=u.y+d[i][1];
  92. if(ok(nx,ny)&&(mp[nx][ny]==(u.s/7)||mp[nx][ny]==-1)){
  93. int k=u.s%7;
  94. if(i==0)
  95. k=behind[u.s/7-1];
  96. if(i==1)
  97. k=u.s/7;
  98. if(!vis[nx][ny][dd[i]*7+k]){
  99. vis[nx][ny][dd[i]*7+k]=1;
  100. q.push(Node(nx,ny,dd[i]*7+k,u.t+1,u.px+(char)(nx+'A'),u.py+(char)(ny+'A')));
  101. }
  102. }
  103. }
  104. }
  105. printf(" No Solution Possible\n");
  106. }
  107.  
  108. int main()
  109. {
  110. //freopen("UVA-810 A Dicey Problem.txt","r",stdin);
  111. int sx,sy,st,sf;
  112. init();
  113. while(cin>>name)
  114. {
  115. if(name=="END")
  116. break;
  117. scanf("%d%d%d%d%d%d",&r,&c,&sx,&sy,&st,&sf);
  118. for(int i=0;i<r;++i)
  119. for(int j=0;j<c;++j)
  120. scanf("%d",&mp[i][j]);
  121. cout<<name<<endl;
  122. bfs(sx-1,sy-1,st*7+sf);
  123. }
  124. return 0;
  125. }

  

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. CodeForces 19D Points (线段树+set)

    D. Points time limit per test 2 seconds memory limit per test 256 megabytes input standard input out ...

  2. ZOJ 3469Food Delivery(区间DP)

    Food Delivery Time Limit: 2 Seconds      Memory Limit: 65536 KB When we are focusing on solving prob ...

  3. Python开发【项目】:RPC异步执行命令(RabbitMQ双向通信)

    RPC异步执行命令 需求: 利用RibbitMQ进行数据交互 可以对多台服务器进行操作 执行命令后不等待命令的执行结果,而是直接让输入下一条命令,结果出来后自动打印 实现异步操作 不懂rpc的请移步h ...

  4. 7.2 Models -- Defining Models

    一.概述 1. 模型是一个类,它定义了你呈现给用户的数据的属性和行为.用户希望如果他们离开你的应用程序,并返回后(或如果他们刷新页面)看到的任何东西应该被一个model代表. 2. 确保在ember. ...

  5. C++中的常量定义

    本篇笔记总结自一次代码检视. 一般来说,使用C语言编程时我们都习惯在代码当中使用C当中的宏定义来定义一个数值常量: #define MY_CONST 7 在C++开发项目时,也会经常存在沿袭C当中常量 ...

  6. c#将十进制转64进制

    //由于用于文件命名,所以将64位中的+转换为=,/转换为_     static char[] digits = {          '0' , '1' , '2' , '3' , '4' , ' ...

  7. 1、安装electron

    安装electron安装并非一帆风顺,我有FQ哈,所以网络方面我就不说了,你们不行的话,可以用cnpm,我说的是另一个问题 我是这样解决的,用以下命令就好了 sudo npm install -g e ...

  8. sql2008 express 实现自动备份

    在一个项目中用到的数据库是sqlserver 2008 r2 express .可没想到express版本的功能有些限制,此前一直都不知道啊.百度百科可以看到它的限制: “1.数据库的大小限制:SQL ...

  9. 《零起点,python大数据与量化交易》

    <零起点,python大数据与量化交易>,这应该是国内第一部,关于python量化交易的书籍. 有出版社约稿,写本量化交易与大数据的书籍,因为好几年没写书了,再加上近期"前海智库 ...

  10. SQL: coalesce()函数

    ①用途: 将空值替换成其他值 返回第一个非空值 ②表达式: COALESCE是一个函数, (expression_1, expression_2, ...,expression_n)依次参考各参数表达 ...