Rocky(dfs)

时间:2023-03-09 06:57:55
Rocky(dfs)

题目描述

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).
Rocky(dfs)
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 ;
}