Ultraviolet A Problem Solution: 10010-where's Waldorf

Source: Internet
Author: User

I use the naive string SEARCH method that scans for the eight directions ctions at each point in the matrix. Due to the relatively small input size, this method works 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. Int const wordsize = 52;
  12. Int const wordcount = 20;
  13. Void find_words (char text [] [wordsize], int row, int Col,
  14. Char words [] [wordsize], int nwords,
  15. Pair <int, int> positions [])
  16. {
  17. Bool found [wordcount] = {false };
  18. Int H [] = {0, 1, 1, 1, 0,-1,-1,-1 };
  19. Int V [] = {-1,-1, 0, 1, 1, 1, 0,-1 };
  20. For (INT r = 1; r <= row; ++ R ){
  21. For (INT c = 1; C <= Col; ++ c ){
  22. For (int w = 0; W <nwords; ++ W ){
  23. If (found [w] | words [w] [0]! = Text [r] [c]) continue;
  24. For (int d = 0; D <8; ++ d ){
  25. Int I = 1;
  26. For (INT y = R + V [d], x = C + H [d];
  27. Words [w] [I]! = '/0' & words [w] [I] = text [y] [x];
  28. ++ I, Y + = V [d], x + = H [d]);
  29. If (words [w] [I] = '/0 '){
  30. Positions [w]. First = R;
  31. Positions [w]. Second = C;
  32. Found [w] = true;
  33. Break;
  34. }
  35. }
  36. }
  37. }
  38. }
  39. }
  40. Int main (INT argc, char * argv [])
  41. {
  42. # Ifndef online_judge
  43. Filebuf In, out;
  44. Cin. rdbuf (in. Open (string (argv [0]) + ". In"). c_str (), ios_base: In ));
  45. Cout. rdbuf (out. Open (string (argv [0]) + ". Out"). c_str (), ios_base: Out ));
  46. # Endif
  47. Int ncases;
  48. Cin> ncases;
  49. While (ncases --> 0 ){
  50. Int row, Col;
  51. Cin> row> Col;
  52. Cin. Ignore (2048, '/N ');
  53. Char text [wordsize] [wordsize];
  54. For (INT r = 1; r <= row; ++ R ){
  55. Cin. Getline (Text [R] + 1, wordsize-1 );
  56. For (INT I = 1; text [r] [I]! = '/0'; ++ I ){
  57. Text [r] [I] = toupper (Text [r] [I]);
  58. }
  59. }
  60. Int nwords;
  61. Cin> nwords;
  62. Char words [wordcount] [wordsize];
  63. Cin. Ignore (2048, '/N ');
  64. For (int w = 0; W <nwords; ++ W ){
  65. Cin. Getline (words [w], wordsize );
  66. For (INT I = 1; words [w] [I]! = '/0'; ++ I ){
  67. Words [w] [I] = toupper (words [w] [I]);
  68. }
  69. }
  70. Pair <int, int> positions [wordcount];
  71. Find_words (text, row, Col, words, nwords, positions );
  72. For (int w = 0; W <nwords; ++ W ){
  73. Cout <positions [w]. First <''<positions [w]. Second <'/N ';
  74. }
  75. If (ncases> 0) cout <'/N ';
  76. }
  77. Return 0;
  78. }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.