题目描述

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个格子中有岩石,最后输入一个入口坐标,问从入口进入要走多少步可以出来,并输出走出来时的坐标。
前进的规则是:若前方有路,就直走,否则,向右转继续直走,若右方也没路就左转继续直走,若右方也没路,则后转往回走。优先顺序:直走,右转,左转,后转。 思路:设置一个方向数组,先确定入口的行走方向,按优先顺序递归。注意最后判断它走出去的方法是如果按行走的方向到出口一直有路,说明该方向能走出去,递归结束。
因为我建图和上图的相反,横坐标较小的在上方,输出的时候整的特晕,应该是先输出列再输出行。。。
 #include<stdio.h>
#include<string.h>
int map[][];
int dir[][] = {{,},{-,},{,-},{,}};//0,1,2,3分别代表向右,向上,向左,向下走
int n,m,k;
int dfs(int r,int c,int d, int step)
{
//printf(",,%d %d %d %d\n",r,c,d,step);
int x,y,i;
//判断是否能走出去
if(d == )
{
for(i = c; i <= n; i++)
if(map[r][i] == )
break;
if(i >= n+)
{
printf("%d %d ",n,m+-r);
return step + (n-c);
}
}
else if(d == )
{
for(i = r; i >= ; i--)
if(map[i][c] == )
break;
if(i <= )
{
printf("%d %d ",c,m);
return step + r-;
}
}
else if(d == )
{
for(i = c; i >= ; i--)
if(map[r][i] == )
break;
if(i <= )
{
printf("%d %d ",,m+-r);
return step + c-;
}
}
else
{
for( i = r; i <= m; i++)
if(map[i][c] == )
break;
if(i >= m+)
{
printf("%d %d ",c,);
return step+(m-r);
}
} x = r+dir[d][];
y = c+dir[d][];
if(map[x][y] != )//直走
{
dfs(x,y,d,++step);
}
else
{
d = (d+)%;
if(map[ r+dir[d][] ][ c+dir[d][] ] != )
dfs(r+dir[d][],c+dir[d][],d,++step);//右转有路,直走
else
{
d = (d+)%;//右边没路,变回原来的方向
d = (d+)%;//左转;
if(map[ r+dir[d][] ][ c+dir[d][] ] != )
dfs(r+dir[d][],c+dir[d][],d,++step);//左转有路,直走
else
{
d = (d+)%;//左转没路,后转
dfs(r+dir[d][],c+dir[d][],d,++step);//后转直走
}
}
}
} int main()
{
int item = ;
while(~scanf("%d %d %d",&n,&m,&k))
{
if(n == && m == && k == )
break;
memset(map,,sizeof(map));
int a,b;
int r,c,d;
for(int i = ; i < k; i++)
{
scanf("%d %d",&a,&b);
map[m+-b][a] = ;
}
scanf("%d %d",&c,&r);
/*for(int i = 1; i <= m; i++)
{
for(int j = 1; j <= n; j++)
printf("%d ",map[i][j]);
printf("\n");
}*/
//确定入口直走的方向
if(r == m)
d = ;
else if(r == )
d = ;
else if(c == n)
d = ;
else d = ;
int ans;
printf("Case %d: ",item++);
ans = dfs(m+-r,c,d,);
printf("%d\n",ans); }
return ;
}

 
 

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. BZOJ 3083: 遥远的国度 [树链剖分 DFS序 LCA]

    3083: 遥远的国度 Time Limit: 10 Sec  Memory Limit: 1280 MBSubmit: 3127  Solved: 795[Submit][Status][Discu ...

  3. BZOJ 1103: [POI2007]大都市meg [DFS序 树状数组]

    1103: [POI2007]大都市meg Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 2221  Solved: 1179[Submit][Sta ...

  4. BZOJ 4196: [Noi2015]软件包管理器 [树链剖分 DFS序]

    4196: [Noi2015]软件包管理器 Time Limit: 10 Sec  Memory Limit: 512 MBSubmit: 1352  Solved: 780[Submit][Stat ...

  5. 图的遍历(搜索)算法(深度优先算法DFS和广度优先算法BFS)

    图的遍历的定义: 从图的某个顶点出发访问遍图中所有顶点,且每个顶点仅被访问一次.(连通图与非连通图) 深度优先遍历(DFS): 1.访问指定的起始顶点: 2.若当前访问的顶点的邻接顶点有未被访问的,则 ...

  6. BZOJ 2434: [Noi2011]阿狸的打字机 [AC自动机 Fail树 树状数组 DFS序]

    2434: [Noi2011]阿狸的打字机 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 2545  Solved: 1419[Submit][Sta ...

  7. POJ_2386 Lake Counting (dfs 错了一个负号找了一上午)

    来之不易的2017第一发ac http://poj.org/problem?id=2386 Lake Counting Time Limit: 1000MS   Memory Limit: 65536 ...

  8. 深度优先搜索(DFS)

    [算法入门] 郭志伟@SYSU:raphealguo(at)qq.com 2012/05/12 1.前言 深度优先搜索(缩写DFS)有点类似广度优先搜索,也是对一个连通图进行遍历的算法.它的思想是从一 ...

  9. 【BZOJ-3779】重组病毒 LinkCutTree + 线段树 + DFS序

    3779: 重组病毒 Time Limit: 20 Sec  Memory Limit: 512 MBSubmit: 224  Solved: 95[Submit][Status][Discuss] ...

随机推荐

  1. nyoj349 poj1094 Sorting It All Out(拓扑排序)

    nyoj349   http://acm.nyist.net/JudgeOnline/problem.php?pid=349poj1094   http://poj.org/problem?id=10 ...

  2. C#微信公众号开发 -- (二)验证成为开发者

    接下来就是验证成为开发者了.先来看一下验证的界面及需要填写的信息 在接口配置信息中填写需要处理验证信息的页面或者一般性处理文件,这里以aspx页面为例 URl中的格式为:http://XXX.com/ ...

  3. STORM 免费且开源的WebSerivce测试工具

    一.名称 STORM 是一款免费且开源的WebSerivce测试工具 二.使用方式 1.发布自己的webservice服务 例如:http://www.webxml.com.cn/WebService ...

  4. OC - 1.面向过程和面向对象的思想对比

    一.面向过程 1> 思想 面向过程是一种以过程为中心的最基础编程思想,不支持面向对象的特性. 面向过程是一种模块化程序设计方法 2> 开发方法 面向过程的开发方法是以过程(也可以说是模块) ...

  5. UItextField常用方法

    - (void)viewDidLoad {     [super viewDidLoad];     // Do any additional setup after loading the view ...

  6. sharepoint2013 新建母板页 新建页面布局 关联母板页和页面布局

    1     母板页的应用和layout(页面布局)的创建和应用 母板页上传:将准备好的html和样式 通过spd中的导入方式导入模版html, 导入后: 然后在网站设置中进行转换为母板页.  随后编辑 ...

  7. 贪心算法:旅行商问题(TSP)

    TSP问题(Traveling Salesman Problem,旅行商问题),由威廉哈密顿爵士和英国数学家克克曼T.P.Kirkman于19世纪初提出.问题描述如下: 有若干个城市,任何两个城市之间 ...

  8. C++里容易忽视却不能忽视的

    1 define 只是简单地文本替换. 2 每个机器的字长不同. 3 每个类型在不同的机器上,所占用的内存空间不同. 4 每个机器内部的字节大小端不同. 5 并不是所有的编译器或机器都支持最新的C++ ...

  9. ubuntu 安装qt 5.1的各种错误

    错误太多了,我就不一一说 了,直接一条命令搞定 sudo apt-:i386 libx11-:i386 libglib2.-:i386 libfreetype6:i386 libSM6:i386 li ...

  10. (转)Libevent(5)— 连接监听器

    转自:http://name5566.com/4220.html 参考文献列表:http://www.wangafu.net/~nickm/libevent-book/ 此文编写的时候,使用到的 Li ...