题目描述

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. 解析包时出现错误,用代码安装apk出现问题

    Intent intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(Uri.fromFile(file),"appli ...

  2. 时空分割的画面--用xcode命令行回忆turbo c

    大学时期曾经玩过turbo c的同学,可以用xcode命令行写写c程序,回味一下吧:) 1. 首先在终端输入,touch main.c 新建文件 2. 编辑main.c内容,写一段简单代码 #incl ...

  3. c#接口深入一步探究其作用,适合新人了解

    前言 前一篇浅显的述说了一下c#接口的作用,并用了一个不怎么好的例子述说了一下.时隔一天,在看完大家的评论后我在论坛中查看了很多前辈们对c#接口的描述,发现大家对例子的说明不是太容易让我们这些新人理解 ...

  4. 7-2 DBA顾问第一次上次操作考试

    SQLPLUS执行:     1--@?/rdbms/admin/awrrpt 生产snapshot, 一个时间点, 再执行下一个时间点.     2--   附件作业第一次执行步骤: 1) SQLP ...

  5. 九度OJ 1468 Sharing -- 哈希

    题目地址:http://ac.jobdu.com/problem.php?pid=1468 题目描述: To store English words, one method is to use lin ...

  6. python—cookielib模块对cookies的操作

    最近用python写爬虫爬了点数据,确实是很好用的东西,今天对python如何操作cookie进行一下总结. python内置有cookielib模块操作cookie,配合urllib模块就可以了很轻 ...

  7. python数据库操作之pymysql模块和sqlalchemy模块(项目必备)

    pymysql pymsql是Python中操作MySQL的模块,其使用方法和MySQLdb几乎相同. 1.下载安装 pip3 install pymysql 2.操作数据库 (1).执行sql #! ...

  8. 微软CSS面试全记录

    先是会有一轮简单的电话技术面试,聊的比较随意,什么都会问,跟职位相关的都有.然后会发一些材料说是要学习,是windows内存管理相关的东西. 完了就是一轮oral test,和技术没有任何关系,问问为 ...

  9. Memcached(二)Memcached Java API基础之MemcachedClient

    1. 构造函数 public MemcachedClient(InetSocketAddress[] ia) throws IOException; public MemcachedClient(Li ...

  10. Java入门-浅析Java学习从入门到精通【转】

    一. JDK (Java Development Kit)  JDK是整个Java的核心,包括了Java运行环境(Java Runtime Envirnment),一堆Java工具和Java基础的类库 ...