题目描述

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. ACM——圆柱体的表面积

    lems 1092 圆柱体的表面积 时间限制(普通/Java):1000MS/3000MS          运行内存限制:65536KByte总提交:2697            测试通过:414 ...

  2. MVC中如何在controller的action中输出JS到页面上

    MVC中如何在controller的action中输出JS到页面上 可以通过Http上下文对象(httpContext)就可以了,在Action中的HttpContext就是这个Action所指向的页 ...

  3. web服务器压力测试工具

    http_load 是运行在linux操作系统上的命令行测试工具, 用来对网站做压力测试. 下载地址:http://www.acme.com/software/http_load/http_load- ...

  4. OS X EL Capitan安装Cocoapods 报错ERROR

    升级OS X EL Capitan10.11之后,原来的pod不能用了,重新安装cocoapods,发现 在运行 “sudo gem install cocoapods” 的时候出现问题: ERROR ...

  5. c# 串口发送接收数据

    /********************** 串口数据接收事件 *****************************/ private void SerialPort_DataReceived ...

  6. 对REST的一些理解

    昨天学习REST,发现有篇文章写的真心不错,看了一遍,并没有完全理解,将一些感觉比较重要的做个记录.  文章链接:REST简介 定义 Representational State Transfer ( ...

  7. (转)所有iOS设备的屏幕分辨率

    Phone: iPhone 1G 320x480 iPhone 3G 320x480 iPhone 3GS 320x480 iPhone 640x960 iPhone 4S 640x960 iPhon ...

  8. XSS漏洞(跨站脚本)

    不要轻信用户提交上来的数据alert消息太难看,因此开发一个aspx页面用来统一展示消息ShowMessage.ashx //主页将判断重定向到另一个页面 if (TextBox1.Text != & ...

  9. maven一些问题

    maven一些问题 - ljhzzyx的日志 - 网易博客 1. The container 'Maven Dependencies' references non existing library ...

  10. 隐性改变display类型

    有一个有趣的现象就是当为元素(不论之前是什么类型元素,display:none 除外)设置以下 2 个句之一: position : absolutefloat : left 或 float:righ ...