UVa 10010 - Where's Waldorf?

本文介绍了一个简单的算法,用于在一个字符矩阵中查找特定的字符串,并输出该字符串起始位置的坐标。通过枚举八个方向,该算法能有效地解决这一问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

给一个字符组成的矩阵,在矩阵中找到要寻找的字符串,输出首字母在矩阵中的位置。只要枚举8个方向即可,代码很长,题很简单。


/*************************************************************************
    > File Name: 10010.cpp
    > Author: Toy
    > Mail: ycsgldy@163.com 
    > Created Time: 2013年05月21日 星期二 13时47分50秒
 ************************************************************************/

#include <algorithm>
#include <iostream>
#include <iomanip>
#include <cstring>
#include <cstdlib>
#include <climits>
#include <sstream>
#include <fstream>
#include <cstdio>
#include <string>
#include <vector>
#include <queue>
#include <cmath>
#include <stack>
#include <map>
#include <set>

using namespace std;
const int INF = 0x7fffffff;
int Case, n, m, num;
char s[55][55], ch[55];

void solve ( int len ) {
	int row, cul, sum, tmp, x, y, k;
	for ( int i = 0; i < n; ++i ) {
		for ( int j = 0; j < m; ++j ) {
			if ( s[i][j] == ch[0] ) {
				for ( sum = 1, tmp = 1, k = j + 1; k < m && tmp < len; ++k, ++tmp ) //向右 
					if ( s[i][k] == ch[tmp] ) sum++;
				if ( sum == len ) {
				    printf ( "%d %d\n", i + 1, j + 1 );
					return;
				}

				for ( sum = 1, tmp = 1, k = j - 1; k >= 0 && tmp < len; --k, ++tmp ) //向左 
					if ( s[i][k] == ch[tmp] ) sum++;
				if ( sum == len ) {
				    printf ( "%d %d\n", i + 1, j + 1 );
					return;
				}

				for ( sum = 1, tmp = 1, k = i - 1; k >= 0 && tmp < len; --k, ++tmp ) //向上
					if ( s[k][j] == ch[tmp] ) sum++;
				if ( sum == len ) {
				    printf ( "%d %d\n", i + 1, j + 1 );
					return;
				}

				for ( sum = 1, tmp = 1, k = i + 1; k < n && tmp < len; ++k, ++tmp ) //向下
					if ( s[k][j] == ch[tmp] ) sum++;
				if ( sum == len ) {
				    printf ( "%d %d\n", i + 1, j + 1 );
					return;
				}

				for ( sum = 1, tmp = 1, x = i + 1, y = j + 1; x < n && y < m && tmp < len; ++x, ++y, ++tmp ) //向右下
					if ( s[x][y] == ch[tmp] ) sum++;		
				if ( sum == len ) {
				    printf ( "%d %d\n", i + 1, j + 1 );
					return;
				}

				for ( sum = 1, tmp = 1, x = i + 1, y = j - 1; x < n && y >= 0 && tmp < len; ++x, --y, ++tmp ) //向左下
					if ( s[x][y] == ch[tmp] ) sum++;
				if ( sum == len ) {
				    printf ( "%d %d\n", i + 1, j + 1 );
					return;
				}

				for ( sum = 1, tmp = 1, x = i - 1, y = j + 1; x >= 0 && y < m && tmp < len; --x, ++y, ++tmp ) //向右上
					if ( s[x][y] == ch[tmp] ) sum++;
				if ( sum == len ) {
				    printf ( "%d %d\n", i + 1, j + 1 );
					return;
				}

				for ( sum = 1, tmp = 1, x = i - 1, y = j - 1; x >= 0 && y >= 0 && tmp < len; --x, --y, ++tmp ) //向左上
					if ( s[x][y] == ch[tmp] ) sum++;
				if ( sum == len ) {
				    printf ( "%d %d\n", i + 1, j + 1 );
					return;
				}
			
			}
		}
	}
}

int main ( ) {
	scanf ( "%d", &Case );
	while ( Case-- ) {
		scanf ( "%d%d", &n, &m );
		getchar ( );
		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 ( isupper ( s[i][j] ) ) s[i][j] = tolower ( s[i][j] );
		scanf ( "%d", &num );
		getchar ();
		for ( int cnt = 1; cnt <= num; ++cnt ) {
			scanf ( "%s", ch );
			int len = strlen ( ch );
			for ( int i = 0; i < len; ++i ) 
				if ( isupper ( ch[i] ) ) ch[i] = tolower ( ch[i] );
			solve( len );
		}
		if ( Case != 0 ) printf ( "\n" );
	}
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值