UVa Problem Solution: 10010 - Where's Waldorf

本文介绍了一种在矩阵中查找特定单词的方法,通过扫描八个方向来定位单词的位置。使用C++实现了一个示例程序,该程序能够处理输入的矩阵和单词列表,并返回每个单词在矩阵中的坐标。

I use the naive string search method that scans for the eight directions at each point in the matrix. Due to the relatively small input size, this method work well enough to get the top 20% ranking.

Code:
  1. /*************************************************************************
  2.  * Copyright (C) 2008 by liukaipeng                                      *
  3.  * liukaipeng at gmail dot com                                           *
  4.  *************************************************************************/
  5. /* @JUDGE_ID 00000 10010 C++ "Where's Waldorf" */
  6. #include <iostream>
  7. #include <fstream>
  8. #include <string>
  9. #include <vector>
  10. using namespace std;
  11.      
  12. int const wordsize = 52;
  13. int const wordcount = 20;          
  14. void find_words(char text[][wordsize], int row, int col, 
  15.                 char words[][wordsize], int nwords, 
  16.                 pair<intint> positions[])
  17. {
  18.   bool found[wordcount] = {false};
  19.   int h[] = {0, 1, 1, 1, 0, -1, -1, -1};
  20.   int v[] = {-1, -1, 0, 1, 1, 1, 0, -1};
  21.   for (int r = 1; r <= row; ++r) {
  22.     for (int c = 1; c <= col; ++c) {
  23.       for (int w = 0; w < nwords; ++w) {
  24.         if (found[w] || words[w][0] != text[r][c]) continue;
  25.         for (int d = 0; d < 8; ++d) {
  26.           int i = 1;
  27.           for (int y = r+v[d], x = c+h[d]; 
  28.                words[w][i] != '/0' && words[w][i] == text[y][x]; 
  29.                ++i, y += v[d], x += h[d]);
  30.           if (words[w][i] == '/0') {
  31.             positions[w].first = r;
  32.             positions[w].second = c;
  33.             found[w] = true;
  34.             break;
  35.           }
  36.         }
  37.       }
  38.     }
  39.   }
  40. }
  41. int main(int argc, char *argv[])
  42. {
  43. #ifndef ONLINE_JUDGE
  44.   filebuf in, out;
  45.   cin.rdbuf(in.open((string(argv[0]) + ".in").c_str(), ios_base::in));
  46.   cout.rdbuf(out.open((string(argv[0]) + ".out").c_str(), ios_base::out));
  47. #endif
  48.   int ncases;
  49.   cin >> ncases;
  50.   while (ncases-- > 0) {
  51.     int row, col;
  52.     cin >> row >> col;
  53.     cin.ignore(2048, '/n');
  54.     char text[wordsize][wordsize];
  55.     for (int r = 1; r <= row; ++r) {
  56.       cin.getline(text[r]+1, wordsize-1);
  57.       for (int i = 1; text[r][i] != '/0'; ++i) {
  58.         text[r][i] = toupper(text[r][i]);
  59.       }
  60.     }
  61.     int nwords;
  62.     cin >> nwords;
  63.     char words[wordcount][wordsize];
  64.     cin.ignore(2048, '/n');
  65.     for (int w = 0; w < nwords; ++w) {
  66.       cin.getline(words[w], wordsize);
  67.       for (int i = 1; words[w][i] != '/0'; ++i) {
  68.         words[w][i] = toupper(words[w][i]);
  69.       }
  70.     }
  71.     pair<intint> positions[wordcount];
  72.     find_words(text, row, col, words, nwords, positions);
  73.     for (int w = 0; w < nwords; ++w) {
  74.       cout << positions[w].first << ' ' << positions[w].second << '/n';
  75.     }
  76.     
  77.     if (ncases > 0) cout << '/n';
  78.   }
  79.   return 0;
  80. }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值