题目描述

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. node.js 小爬虫 imooc 2016.03.06

    爬虫目标:获取http://www.imooc.com/learn/348网页中的章节标题和视频信息. var http = require('http'); var cheerio = requir ...

  2. DAG模型——硬币问题

    硬币问题 有n种硬币,面值分别为V1,V2,...,Vn,每种都有无限多.给定非负整数S,可以选用多少个硬币,使得面值之和恰好为S?输出硬币数目的最小值和最大值.1<=n<=100, 0& ...

  3. 浪潮MegaCli

    再总结: 一般,清RAID再做RAID,安装完毕后: ./MegaCli64 -PDList -aALL | egrep 'Slot|Enclosure Device'                 ...

  4. PAT_1016 部分A+B

    问题描述: 正整数A的“DA(为1位整数)部分”定义为由A中所有DA组成的新整数PA.例如:给定A = 3862767,DA = 6,则A的“6部分”PA是66,因为A中有2个6. 现给定A.DA.B ...

  5. AOP学习过程中遇到的问题汇总

    jdk版本问题:     在spring较低的版本中,仅支持jdk1.5到1.7版本,由于我本机安装的是jdk1.8,所以在调试的时候就会提示jdk版本要高于1.5.于是换成spring4.0,在co ...

  6. 计算字符串和文件的MD5值

    //计算字符串的MD5值 public string GetMD5(string sDataIn) { MD5CryptoServiceProvider md5 = new MD5CryptoServ ...

  7. Redis — CentOS6.4安装Redis以及安装PHP客户端phpredis

    一.安装Redis 1.下载安装包 wget http://download.redis.io/releases/redis-2.8.6.tar.gz 2.解压包 tar xzf redis-2.8. ...

  8. python 循环while和for in

    #!/uer/bin/env python # _*_ coding: utf-8 _*_ lucknumber = 5 b = 0 while b <3: print('guss count: ...

  9. c/c++多级指针

    c/c++多级指针 如图: # include <stdio.h> int main(void) { ; int * p = &i; //p只能存放int类型变量的地址 int * ...

  10. python is == 的区别

    要点: is 判断是否是同一个对象.是通过id来判断的    == 是通过值来判断的    为了提高内存利用率对一些简单的对象,如一些数值较小的int对象,python采用重用对象内存的方法 例如指向 ...