题目描述

Sylvester Stallion is an old horse who likes nothing better than to wander around in the fields around his stable. Sylvester is a little single minded and will walk in a straight line unless there is a rock in this path. If that\'s the case, he does one of three things: 1) if there is no rock to his right, he turns right and continues walking straight in that direction; 2) otherwise, if there is no rock to his left, he turns left and walks in that direction; 3) otherwise, he turns around and walks back the way he came. In a particularly rocky field, he may make several turns in his walk and exit the field in quite an unexpected location. For example, in the field shown below, if Sylvester enters the field at square (1,4), he will follow the path shown (a total of 12 squares), exiting at square (3,5).

Many of his other animal friends are concerned about Sylvester, and would like to know where he ends up on his walks (in particular, his good friend, the ram Beau). That\'s where you come in - given a description of a field and the location of Sylvester\'s entrance, you are to determine where he will exit the field and how long it will take him to get there.

输入

Each case starts with three positive integers n m rindicating the number of columns (n) and rows (m) in the field and the number of rocks (r), with n, m ≤ 20. Following the first line will be lines containing the locations of the r rocks. Each location will be of the form c r, indicating the column and row of the rock. There will be no more than 1 rock in any location. Following the rock locations will be the entrance location for Sylvester. Sylvester\'s starting direction will always be perpendicular to the side of the field he enters from (this location will never be a corner square) and there will never be a rock in his entrance location square. A line containing three zeros will terminate input.

输出

For each test case, output the case number followed by the last square Sylvester hits before he leaves the field (Sylvester will never get trapped in any field) and the number of squares that Sylvester visited during his walks, counting repeated squares.

示例输入

6  5  7
2  2  2  5  3  1  4  4
5  1  5  3  6  2
1  4
5  5  0
1  2
0  0  0

示例输出

Case 1: 3 5 12
Case 2: 5 2 5

题意:输入n,m,k;代表一个m*n的矩阵,其中有k个格子中有岩石,最后输入一个入口坐标,问从入口进入要走多少步可以出来,并输出走出来时的坐标。前进的规则是:若前方有路,就直走,否则,向右转继续直走,若右方也没路就左转继续直走,若右方也没路,则后转往回走。优先顺序:直走,右转,左转,后转。

思路:设置一个方向数组,先确定入口的行走方向,按优先顺序递归。注意最后判断它走出去的方法是如果按行走的方向到出口一直有路,说明该方向能走出去,递归结束。因为我建图和上图的相反,横坐标较小的在上方,输出的时候整的特晕,应该是先输出列再输出行。。。

  1 #include<stdio.h>
  2 #include<string.h>
  3 int map[30][30];
  4 int dir[4][2] = {{0,1},{-1,0},{0,-1},{1,0}};//0,1,2,3分别代表向右,向上,向左,向下走
  5 int n,m,k;
  6 int dfs(int r,int c,int d, int step)
  7 {
  8     //printf(",,%d %d %d %d\n",r,c,d,step);
  9     int x,y,i;
 10     //判断是否能走出去
 11     if(d == 0)
 12     {
 13         for(i = c; i <= n; i++)
 14             if(map[r][i] == 1)
 15                 break;
 16         if(i >= n+1)
 17         {
 18             printf("%d %d ",n,m+1-r);
 19             return step + (n-c);
 20         }
 21     }
 22     else if(d == 1)
 23     {
 24         for(i = r; i >= 1; i--)
 25             if(map[i][c] == 1)
 26                 break;
 27         if(i <= 0)
 28         {
 29             printf("%d %d ",c,m);
 30             return step + r-1;
 31         }
 32     }
 33     else if(d == 2)
 34     {
 35         for(i = c; i >= 1; i--)
 36             if(map[r][i] == 1)
 37                 break;
 38         if(i <= 0)
 39         {
 40             printf("%d %d ",1,m+1-r);
 41             return step + c-1;
 42         }
 43     }
 44     else
 45     {
 46         for( i = r; i <= m; i++)
 47             if(map[i][c] == 1)
 48                 break;
 49         if(i >= m+1)
 50         {
 51             printf("%d %d ",c,1);
 52             return step+(m-r);
 53         }
 54     }
 55
 56     x = r+dir[d][0];
 57     y = c+dir[d][1];
 58     if(map[x][y] != 1)//直走
 59     {
 60         dfs(x,y,d,++step);
 61     }
 62     else
 63     {
 64         d = (d+3)%4;
 65         if(map[ r+dir[d][0] ][ c+dir[d][1] ] != 1)
 66             dfs(r+dir[d][0],c+dir[d][1],d,++step);//右转有路,直走
 67         else
 68         {
 69             d = (d+1)%4;//右边没路,变回原来的方向
 70             d = (d+1)%4;//左转;
 71             if(map[ r+dir[d][0] ][ c+dir[d][1] ] != 1)
 72                 dfs(r+dir[d][0],c+dir[d][1],d,++step);//左转有路,直走
 73             else
 74             {
 75                 d = (d+1)%4;//左转没路,后转
 76                 dfs(r+dir[d][0],c+dir[d][1],d,++step);//后转直走
 77             }
 78         }
 79     }
 80 }
 81
 82 int main()
 83 {
 84     int item = 1;
 85     while(~scanf("%d %d %d",&n,&m,&k))
 86     {
 87         if(n == 0 && m == 0 && k == 0)
 88             break;
 89         memset(map,0,sizeof(map));
 90         int a,b;
 91         int r,c,d;
 92         for(int i = 0; i < k; i++)
 93         {
 94             scanf("%d %d",&a,&b);
 95             map[m+1-b][a] = 1;
 96         }
 97         scanf("%d %d",&c,&r);
 98         /*for(int i = 1; i <= m; i++)
 99         {
100             for(int j = 1; j <= n; j++)
101                 printf("%d ",map[i][j]);
102             printf("\n");
103         }*/
104         //确定入口直走的方向
105         if(r == m)
106             d = 3;
107         else if(r == 1)
108             d = 1;
109         else if(c == n)
110             d = 2;
111         else d = 0;
112         int ans;
113         printf("Case %d: ",item++);
114         ans = dfs(m+1-r,c,d,1);
115         printf("%d\n",ans);
116
117     }
118     return 0;
119 }

View Code

 
 

转载于:https://www.cnblogs.com/LK1994/p/3453490.html

Rocky(dfs)相关推荐

  1. 2015 UESTC Winter Training #6【Regionals 2010 North America - Rocky Mountain】

    2015 UESTC Winter Training #6 Regionals 2010 >> North America - Rocky Mountain A - Parenthesis ...

  2. [JS][dfs]题解 | #迷宫问题#

    题解 | #迷宫问题# 题目链接 迷宫问题 题目描述 定义一个二维数组 N*M ,如 5 × 5 数组下所示: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 1, 1 ...

  3. [C] [编程题]连通块(DFS解决)

    时间限制:C/C++ 1秒,其他语言2秒空间限制:C/C++ 256M,其他语言512M 来源:牛客网 金山办公2020校招服务端开发工程师笔试题(一) 题目描述 给一个01矩阵,1代表是陆地,0代表 ...

  4. 【BZOJ2434】[NOI2011]阿狸的打字机 AC自动机+DFS序+树状数组

    [BZOJ2434][NOI2011]阿狸的打字机 Description 阿狸喜欢收藏各种稀奇古怪的东西,最近他淘到一台老式的打字机.打字机上只有28个按键,分别印有26个小写英文字母和'B'.'P ...

  5. usaco snail trails(dfs)

    dfs啊,我还写了好长时间,一天不如一天. /* ID:jinbo wu TASK: snail LANG:C++ */ #include<bits/stdc++.h> using nam ...

  6. usaco shuttle puzzle(dfs剪枝)

    这题一看我也以为找规律,然后无法下手之后又想到bfs最后看题解是用dfs大神dfs用的出神入化. 不过这题好像可以找规律. /* ID:jinbo wu TASK: shuttle LANG:C++ ...

  7. usaco street race(dfs)

    一开始我觉得很麻烦但第一题好做由于数据较小直接每个点删后dfs就可以了,第二问我不知道如何判断有没有公共的道路,最后看别人的也挺简单的就是看分别从两条公路的起点开始dfs如果他们能到达同一点就代表有公 ...

  8. Forest Program dfs+tanjar仙人掌

    题目链接 CCPC2019 F题. 题意:给一颗仙人掌树,让你求每一个小环的边的个数,用快速幂即可求解. 思路:第一反应是tanjar乱搞,把每个环上的点取出来,类似于缩点的方法.但是忽然感觉dfs能 ...

  9. HDU - 5877 Weak Pair 2016 ACM/ICPC 大连网络赛 J题 dfs+树状数组+离散化

    题目链接 You are given a rootedrooted tree of NN nodes, labeled from 1 to NN. To the iith node a non-neg ...

最新文章

  1. android安全问题(二) 程序锁
  2. 判断long类型是否为空_数据类型
  3. 关于redis的文章
  4. BZOJ 2436 NOI嘉年华(单调优化)
  5. Halcon求取矩形顶点坐标
  6. 【转】wpf从我炫系列1----布局控件的使用(上)
  7. keil流水灯c语言程序两个一起亮,我用keil c51编了一个流水灯程序,编译无误却只有第一和第二个灯亮了,最后停在第一个灯处不动...
  8. html 搜索 高亮效果,html5输入框高亮效果
  9. 速率法和终点法的区别_终点法 速率法 二点法
  10. 华为薪酬(2002年左右)
  11. Live Streaming Datasets--网络数据集下载
  12. 维吉尼亚密码(Vigenere)
  13. android 标签开源控件,Android开源控件ViewPager Indicator的使用方法
  14. 【字典树】字典树的创建(入门详细介绍)
  15. 三分频电路Verilog设计
  16. java :工资计算
  17. 【STM32+cubemx】0026 HAL库开发:NRF24L01无线2.4G通信模块的应用
  18. 如何帮助新员工快速融入团队?先搞清楚“融入”背后的难点,再对症下药
  19. [ctf misc][wp]一些内存取证的wp(含[2021蓝帽杯北部赛区分区赛]博人的文件)
  20. TSQL与PL/SQL的区别

热门文章

  1. Python Django创建项目命令
  2. eureka实例相关配置
  3. Linux free指令查看内存使用情况
  4. Linux shell条件判断
  5. 网页视频播放php拉伸代码,网页在线视频播放代码大全
  6. 未处理sqlexception中value_plsql异常处理
  7. setfacl 权限导出_linux学习-setfacl设置特定目录用户权限
  8. python二级多少分过_python二级操作题与分析(2)
  9. 回调函数之Java/C++版本
  10. Windows 10 下 VS2017(+Clion) C/C++ 配置 OpenCV-4.4.0