日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Rocky(dfs)

發(fā)布時(shí)間:2024/10/12 编程问答 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Rocky(dfs) 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

題目描述

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;代表一個(gè)m*n的矩陣,其中有k個(gè)格子中有巖石,最后輸入一個(gè)入口坐標(biāo),問從入口進(jìn)入要走多少步可以出來,并輸出走出來時(shí)的坐標(biāo)。
前進(jìn)的規(guī)則是:若前方有路,就直走,否則,向右轉(zhuǎn)繼續(xù)直走,若右方也沒路就左轉(zhuǎn)繼續(xù)直走,若右方也沒路,則后轉(zhuǎn)往回走。優(yōu)先順序:直走,右轉(zhuǎn),左轉(zhuǎn),后轉(zhuǎn)。

思路:設(shè)置一個(gè)方向數(shù)組,先確定入口的行走方向,按優(yōu)先順序遞歸。注意最后判斷它走出去的方法是如果
按行走的方向到出口一直有路,說明該方向能走出去,遞歸結(jié)束。
因?yàn)槲医▓D和上圖的相反,橫坐標(biāo)較小的在上方,輸出的時(shí)候整的特暈,應(yīng)該是先輸出列再輸出行。。。
1 #include<stdio.h> 2 #include<string.h> 3 int map[30][30]; 4 int dir[4][2] = {{0,1},{-1,0},{0,-1},{1,0}};//0,1,2,3分別代表向右,向上,向左,向下走 5 int n,m,k; 6 int dfs(int r,int c,int d, int step) 7 { 8 //printf(",,%d %d %d %d\n",r,c,d,step); 9 int x,y,i; 10 //判斷是否能走出去 11 if(d == 0) 12 { 13 for(i = c; i <= n; i++) 14 if(map[r][i] == 1) 15 break; 16 if(i >= n+1) 17 { 18 printf("%d %d ",n,m+1-r); 19 return step + (n-c); 20 } 21 } 22 else if(d == 1) 23 { 24 for(i = r; i >= 1; i--) 25 if(map[i][c] == 1) 26 break; 27 if(i <= 0) 28 { 29 printf("%d %d ",c,m); 30 return step + r-1; 31 } 32 } 33 else if(d == 2) 34 { 35 for(i = c; i >= 1; i--) 36 if(map[r][i] == 1) 37 break; 38 if(i <= 0) 39 { 40 printf("%d %d ",1,m+1-r); 41 return step + c-1; 42 } 43 } 44 else 45 { 46 for( i = r; i <= m; i++) 47 if(map[i][c] == 1) 48 break; 49 if(i >= m+1) 50 { 51 printf("%d %d ",c,1); 52 return step+(m-r); 53 } 54 } 55 56 x = r+dir[d][0]; 57 y = c+dir[d][1]; 58 if(map[x][y] != 1)//直走 59 { 60 dfs(x,y,d,++step); 61 } 62 else 63 { 64 d = (d+3)%4; 65 if(map[ r+dir[d][0] ][ c+dir[d][1] ] != 1) 66 dfs(r+dir[d][0],c+dir[d][1],d,++step);//右轉(zhuǎn)有路,直走 67 else 68 { 69 d = (d+1)%4;//右邊沒路,變回原來的方向 70 d = (d+1)%4;//左轉(zhuǎn); 71 if(map[ r+dir[d][0] ][ c+dir[d][1] ] != 1) 72 dfs(r+dir[d][0],c+dir[d][1],d,++step);//左轉(zhuǎn)有路,直走 73 else 74 { 75 d = (d+1)%4;//左轉(zhuǎn)沒路,后轉(zhuǎn) 76 dfs(r+dir[d][0],c+dir[d][1],d,++step);//后轉(zhuǎn)直走 77 } 78 } 79 } 80 } 81 82 int main() 83 { 84 int item = 1; 85 while(~scanf("%d %d %d",&n,&m,&k)) 86 { 87 if(n == 0 && m == 0 && k == 0) 88 break; 89 memset(map,0,sizeof(map)); 90 int a,b; 91 int r,c,d; 92 for(int i = 0; i < k; i++) 93 { 94 scanf("%d %d",&a,&b); 95 map[m+1-b][a] = 1; 96 } 97 scanf("%d %d",&c,&r); 98 /*for(int i = 1; i <= m; i++) 99 { 100 for(int j = 1; j <= n; j++) 101 printf("%d ",map[i][j]); 102 printf("\n"); 103 }*/ 104 //確定入口直走的方向 105 if(r == m) 106 d = 3; 107 else if(r == 1) 108 d = 1; 109 else if(c == n) 110 d = 2; 111 else d = 0; 112 int ans; 113 printf("Case %d: ",item++); 114 ans = dfs(m+1-r,c,d,1); 115 printf("%d\n",ans); 116 117 } 118 return 0; 119 } View Code

?

? ?

轉(zhuǎn)載于:https://www.cnblogs.com/LK1994/p/3453490.html

總結(jié)

以上是生活随笔為你收集整理的Rocky(dfs)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。