蓝桥杯2017-省赛-C/C++-A组1题

题目
标题:迷宫

X星球的一处迷宫游乐场建在某个小山坡上。
它是由10x10相互连通的小房间组成的。

房间的地板上写着一个很大的字母。
我们假设玩家是面朝上坡的方向站立,则:
L表示走到左边的房间,
R表示走到右边的房间,
U表示走到上坡方向的房间,
D表示走到下坡方向的房间。

X星球的居民有点懒,不愿意费力思考。
他们更喜欢玩运气类的游戏。这个游戏也是如此!

开始的时候,直升机把100名玩家放入一个个小房间内。
玩家一定要按照地上的字母移动。

迷宫地图如下:
------------
UDDLUULRUL
UURLLLRRRU
RRUURLDLRD
RUDDDDUUUU
URUDLLRRUU
DURLRLDLRL
ULLURLLRDU
RDLULLRDDD
UUDDUDUDLL
ULRDLUURRR
------------

请你计算一下,最后,有多少玩家会走出迷宫?
而不是在里边兜圈子。

请提交该整数,表示走出迷宫的玩家数目,不要填写任何多余的内容。

如果你还没明白游戏规则,可以参看一个简化的4x4迷宫的解说图:
p1.png

p1

代码

 1 #include<iostream>
 2 using namespace std;
 3 char a[10][10];
 4 bool judge(int i,int j){
 5      int curi,curj;
 6      curi=i;
 7      curj=j;
 8      int prei=-1,prej=-1;
 9      int nexti,nextj;
10      while((curi>=0)&&(curj>=0)&&(curi<10)&&(curj<10)){ 
11          cout<<a[curi][curj]<<" "<<curi<<" "<<curj<<endl; 
12          switch(a[curi][curj]){//根据方向选择下一次变化的位置 
13              case 'L':nexti=curi;
14                       nextj=curj-1;
15                       break;
16              case 'U':nexti=curi-1;
17                       nextj=curj;
18                       break;
19              case 'D':nexti=curi+1;
20                       nextj=curj;
21                       break;
22              case 'R':nexti=curi;
23                       nextj=curj+1;
24                       break;
25              default: break; 
26          }
27          if(nexti==prei&&nextj==prej){//判断是否出现死循环 
28              return false;
29          }else{//未出现死循环,变换坐标,继续 
30              prei=curi;
31              prej=curj;
32              curi=nexti;
33              curj=nextj;
34          }
35      }
36      return true;
37 }
38 int main(){
39      for(int i=0;i<10;i++){
40          for(int j=0;j<10;j++){
41              cin>>a[i][j]; 
42          }
43      }
44      int cnt=0;
45      for(int i=0;i<10;i++){
46          for(int j=0;j<10;j++){
47              cout<<"location: "<<i<<" "<<j<<endl;
48              if(judge(i,j)){
49                  cout<<"    yes"<<endl;
50                  cnt++;
51              }else{
52                  cout<<"    no "<<endl;
53              }
54          }
55      }
56      cout<<cnt<<endl;
57 }
posted @ 2020-01-21 13:29  Mem_Ocean  阅读(237)  评论(0)    收藏  举报