UVa Problem Solution: 10010 - Where's Waldorf
2008-11-13 15:04
1671 查看
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:
/*************************************************************************
* Copyright (C) 2008 by liukaipeng *
* liukaipeng at gmail dot com *
*************************************************************************/
/* @JUDGE_ID 00000 10010 C++ "Where's Waldorf" */
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
int const wordsize = 52;
int const wordcount = 20;
void find_words(char text[][wordsize], int row, int col,
char words[][wordsize], int nwords,
pair<int, int> positions[])
{
bool found[wordcount] = {false};
int h[] = {0, 1, 1, 1, 0, -1, -1, -1};
int v[] = {-1, -1, 0, 1, 1, 1, 0, -1};
for (int r = 1; r <= row; ++r) {
for (int c = 1; c <= col; ++c) {
for (int w = 0; w < nwords; ++w) {
if (found[w] || words[w][0] != text[r][c]) continue;
for (int d = 0; d < 8; ++d) {
int i = 1;
for (int y = r+v[d], x = c+h[d];
words[w][i] != '/0' && words[w][i] == text[y][x];
++i, y += v[d], x += h[d]);
if (words[w][i] == '/0') {
positions[w].first = r;
positions[w].second = c;
found[w] = true;
break;
}
}
}
}
}
}
int main(int argc, char *argv[])
{
#ifndef ONLINE_JUDGE
filebuf in, out;
cin.rdbuf(in.open((string(argv[0]) + ".in").c_str(), ios_base::in));
cout.rdbuf(out.open((string(argv[0]) + ".out").c_str(), ios_base::out));
#endif
int ncases;
cin >> ncases;
while (ncases-- > 0) {
int row, col;
cin >> row >> col;
cin.ignore(2048, '/n');
char text[wordsize][wordsize];
for (int r = 1; r <= row; ++r) {
cin.getline(text[r]+1, wordsize-1);
for (int i = 1; text[r][i] != '/0'; ++i) {
text[r][i] = toupper(text[r][i]);
}
}
int nwords;
cin >> nwords;
char words[wordcount][wordsize];
cin.ignore(2048, '/n');
for (int w = 0; w < nwords; ++w) {
cin.getline(words[w], wordsize);
for (int i = 1; words[w][i] != '/0'; ++i) {
words[w][i] = toupper(words[w][i]);
}
}
pair<int, int> positions[wordcount];
find_words(text, row, col, words, nwords, positions);
for (int w = 0; w < nwords; ++w) {
cout << positions[w].first << ' ' << positions[w].second << '/n';
}
if (ncases > 0) cout << '/n';
}
return 0;
}
Code:
/*************************************************************************
* Copyright (C) 2008 by liukaipeng *
* liukaipeng at gmail dot com *
*************************************************************************/
/* @JUDGE_ID 00000 10010 C++ "Where's Waldorf" */
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
int const wordsize = 52;
int const wordcount = 20;
void find_words(char text[][wordsize], int row, int col,
char words[][wordsize], int nwords,
pair<int, int> positions[])
{
bool found[wordcount] = {false};
int h[] = {0, 1, 1, 1, 0, -1, -1, -1};
int v[] = {-1, -1, 0, 1, 1, 1, 0, -1};
for (int r = 1; r <= row; ++r) {
for (int c = 1; c <= col; ++c) {
for (int w = 0; w < nwords; ++w) {
if (found[w] || words[w][0] != text[r][c]) continue;
for (int d = 0; d < 8; ++d) {
int i = 1;
for (int y = r+v[d], x = c+h[d];
words[w][i] != '/0' && words[w][i] == text[y][x];
++i, y += v[d], x += h[d]);
if (words[w][i] == '/0') {
positions[w].first = r;
positions[w].second = c;
found[w] = true;
break;
}
}
}
}
}
}
int main(int argc, char *argv[])
{
#ifndef ONLINE_JUDGE
filebuf in, out;
cin.rdbuf(in.open((string(argv[0]) + ".in").c_str(), ios_base::in));
cout.rdbuf(out.open((string(argv[0]) + ".out").c_str(), ios_base::out));
#endif
int ncases;
cin >> ncases;
while (ncases-- > 0) {
int row, col;
cin >> row >> col;
cin.ignore(2048, '/n');
char text[wordsize][wordsize];
for (int r = 1; r <= row; ++r) {
cin.getline(text[r]+1, wordsize-1);
for (int i = 1; text[r][i] != '/0'; ++i) {
text[r][i] = toupper(text[r][i]);
}
}
int nwords;
cin >> nwords;
char words[wordcount][wordsize];
cin.ignore(2048, '/n');
for (int w = 0; w < nwords; ++w) {
cin.getline(words[w], wordsize);
for (int i = 1; words[w][i] != '/0'; ++i) {
words[w][i] = toupper(words[w][i]);
}
}
pair<int, int> positions[wordcount];
find_words(text, row, col, words, nwords, positions);
for (int w = 0; w < nwords; ++w) {
cout << positions[w].first << ' ' << positions[w].second << '/n';
}
if (ncases > 0) cout << '/n';
}
return 0;
}
相关文章推荐
- UVA 10010 Where's Waldorf?
- UVA 10010 Where's Waldorf?
- UVa 10010 - Where's Waldorf?
- UVA - 10010 Where's Waldorf?
- UVa: 10010 - Where's Waldorf?
- UVA - 10010 Where's Waldorf?
- UVA 10010 Where's Waldorf?
- UVA - 10010 Where's Waldorf?
- uva - 10010 - Where's Waldorf?
- uva 10010 Where's Waldorf?
- UVA 10010 - Where's Waldorf?(字符串)
- uva 10010 Where's Waldorf?
- UVa 10010 - Where's Waldorf?
- uva_10010_Where's Waldorf?
- uva 10010 Where's Waldorf?
- UVA 10010 Where's Waldorf?
- UVA 10010- Where's Waldorf?(八方向寻找字符串)
- uva 10010 Where's Waldorf?(字符串处理)
- uva_10010-Where's Waldorf?
- UVa 10010 - Where's Waldorf?