字符迷阵[网易题目]
生活随笔
收集整理的這篇文章主要介紹了
字符迷阵[网易题目]
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
字符迷陣是一種經典的智力游戲。玩家需要在給定的矩形的字符迷陣中尋找特定的單詞。 在這題的規則中,單詞是如下規定的: 1. 在字符迷陣中選取一個字符作為單詞的開頭; 2. 選取右方、下方、或右下45度方向作為單詞的延伸方向; 3. 以開頭的字符,以選定的延伸方向,把連續得到的若干字符拼接在一起,則稱為一個單詞。 以圖1為例,如果要在其中尋找單詞"WORD",則綠色框所標示的都是合法的方案,而紅色框所標示的都是不合法的方案。 現在的問題是,給出一個字符迷陣,及一個要尋找的單詞,問能在字符迷陣中找到多少個該單詞的合法方案。注意合法方案是可以重疊的,如圖1所示的字符迷陣,其中單詞"WORD"的合法方案有4種。
#include <iostream> #include <stdio.h> #include <algorithm> #include <set> #include <cstring>using namespace std;int m, n; char wordmap[100][100]; char seekword[100]; int len_seekword;int x[] = {1, 0, 1}; int y[] = {0, 1, 1}; int ans;void deep_search_first() {int i, j, k;for(int i=0; i<m; i++) {for(int j=0; j<n; j++) {if(wordmap[i][j] == seekword[0]) {//探索三個方向是否存在合法單詞int cur_pos;int heng;int zong;for(k=0; k<3; k++) {heng = i + x[k];zong = j + y[k];cur_pos = 1;while(heng<m && zong<n && cur_pos<len_seekword && wordmap[heng][zong]==seekword[cur_pos]) {heng = heng + x[k];zong = zong + y[k];cur_pos++;}if(cur_pos == len_seekword) {ans++;}}}}} }int main() {int group;scanf("%d", &group);while(group--) {scanf("%d %d%*c", &m, &n);for(int i=0; i<m; i++) {scanf("%s", wordmap[i]);}scanf("%s", seekword);len_seekword = strlen(seekword);ans = 0;deep_search_first();printf("%d\n", ans);} }
輸入描述:
輸入的第一行為一個正整數T,表示測試數據組數。 接下來有T組數據。每組數據的第一行包括兩個整數m和n,表示字符迷陣的行數和列數。接下來有m行,每一行為一個長度為n的字符串,按順序表示每一行之中的字符。再接下來還有一行包括一個字符串,表示要尋找的單詞。 數據范圍: 對于所有數據,都滿足1<=T<=9,且輸入的所有位于字符迷陣和單詞中的字符都為大寫字母。要尋找的單詞最短為2個字符,最長為9個字符。字符迷陣和行列數,最小為1,最多為99。 對于其中50%的數據文件,字符迷陣的行列數更限制為最多為20。輸出描述:
對于每一組數據,輸出一行,包含一個整數,為在給定的字符迷陣中找到給定的單詞的合法方案數。輸入例子1:
3 10 10 AAAAAADROW WORDBBBBBB OCCCWCCCCC RFFFFOFFFF DHHHHHRHHH ZWZVVVVDID ZOZVXXDKIR ZRZVXRXKIO ZDZVOXXKIW ZZZWXXXKIK WORD 3 3 AAA AAA AAA AA 5 8 WORDSWOR ORDSWORD RDSWORDS DSWORDSW SWORDSWO SWORD輸出例子1:
4 16 5#include <iostream> #include <stdio.h> #include <algorithm> #include <set> #include <cstring>using namespace std;int m, n; char wordmap[100][100]; char seekword[100]; int len_seekword;int x[] = {1, 0, 1}; int y[] = {0, 1, 1}; int ans;void deep_search_first() {int i, j, k;for(int i=0; i<m; i++) {for(int j=0; j<n; j++) {if(wordmap[i][j] == seekword[0]) {//探索三個方向是否存在合法單詞int cur_pos;int heng;int zong;for(k=0; k<3; k++) {heng = i + x[k];zong = j + y[k];cur_pos = 1;while(heng<m && zong<n && cur_pos<len_seekword && wordmap[heng][zong]==seekword[cur_pos]) {heng = heng + x[k];zong = zong + y[k];cur_pos++;}if(cur_pos == len_seekword) {ans++;}}}}} }int main() {int group;scanf("%d", &group);while(group--) {scanf("%d %d%*c", &m, &n);for(int i=0; i<m; i++) {scanf("%s", wordmap[i]);}scanf("%s", seekword);len_seekword = strlen(seekword);ans = 0;deep_search_first();printf("%d\n", ans);} }
轉載于:https://www.cnblogs.com/yspworld/p/10502583.html
總結
以上是生活随笔為你收集整理的字符迷阵[网易题目]的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 计算机科学导论第五版第二章答案,计算机科
- 下一篇: 计算机 smb共享,文件大师SMB共享使