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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

HDU-5706(DFS)

發布時間:2025/3/21 编程问答 9 豆豆
生活随笔 收集整理的這篇文章主要介紹了 HDU-5706(DFS) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

題目鏈接:acm.hdu.edu.cn/showproblem.php?pid=5706
GirlCat
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1823 Accepted Submission(s): 1091

Problem Description

As a cute girl, Kotori likes playing Hide and Seek'' with cats particularly. Under the influence of Kotori, many girls and cats are playingHide and Seek’’ together.
Koroti shots a photo. The size of this photo is n×m, each pixel of the photo is a character of the lowercase(from a' toz’).
Kotori wants to know how many girls and how many cats are there in the photo.

We define a girl as – we choose a point as the start, passing by 4 different connected points continuously, and the four characters are exactly girl'' in the order. We define two girls are different if there is at least a point of the two girls are different. We define a cat as -- we choose a point as the start, passing by 3 different connected points continuously, and the three characters are exactlycat’’ in the order.
We define two cats are different if there is at least a point of the two cats are different.

Two points are regarded to be connected if and only if they share a common edge.

Input

The first line is an integer T which represents the case number.

As for each case, the first line are two integers n and m, which are the height and the width of the photo.
Then there are n lines followed, and there are m characters of each line, which are the the details of the photo.

It is guaranteed that:
T is about 50.
1≤n≤1000.
1≤m≤1000.
∑(n×m)≤2×106.

Output

As for each case, you need to output a single line.
There should be 2 integers in the line with a blank between them representing the number of girls and cats respectively.

Please make sure that there is no extra blank.

Sample Input

3
1 4
girl
2 3
oto
cat
3 4
girl
hrlt
hlca

Sample Output

1 0
0 2
4 1

題目大意:輸入第一行表示多少個測試案例,然后跟著T組測試案例,每一組都先輸入多少行,多少列。然后緊跟著就是n行m列字符。然后讓你找這個圖中有多少個girl和cat,比如girl你就從g字符開始從它的上下左右找i,找到i后,又從i的上下左右找r。一次如果能組成一個完整的girl就算一個。cat也是這樣。

解題思路:

典型的dfs,bfs亦可。這里用的是dfs,用兩個字符數組將girl和cat存起來,需要注意的是每次遞歸傳值時,都要把字符數組的下標傳進去,遍歷四個方向時利用傳進來的下標取字符串中的字符來比較。思路雖然簡單,但還是要注意dfs的細節。

AC代碼:

#include<cstdio> int M[4][2] = {-1, 0, 1, 0, 0, 1, 0, -1}; char s1[] = "girl", s2[] = "cat"; char s[1010][1010]; int ans1, ans2; int n, m; void dfsgirl(int x, int y, int k) {int dx, dy;for(int i = 0; i < 4; ++i){dx = x + M[i][0];dy = y + M[i][1];if(dx >= 0 && dx < n && dy >= 0 && dy < m && s[dx][dy] == s1[k]){k++;if(k == 4){ans1++;}dfsgirl(dx, dy, k);k--;}} }void dfscat(int x, int y, int k) {int dx, dy;for(int i = 0; i < 4; ++i){dx = x + M[i][0];dy = y + M[i][1];if(dx >= 0 && dx < n && dy >= 0 && dy < m && s[dx][dy] == s2[k]){k++;if(k == 3){ans2++;k--;}else{dfscat(dx, dy, k);k--;}}} }int main() {int t;scanf("%d", &t);while(t--){ans1 = ans2 = 0;scanf("%d%d", &n, &m);for(int i = 0; i < n; ++i)scanf("%s", s[i]);for(int i = 0; i < n; ++i)for(int j = 0; j < m; ++j){if(s[i][j] == 'g'){dfsgirl(i, j, 1);}if(s[i][j] == 'c'){dfscat(i, j, 1);}}printf("%d %d\n", ans1, ans2);}return 0; }

總結

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

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。