Enunciado
Escribir un procedimiento que, dada una matriz de nxn de caracteres y un vector de dimensión p<=n de caracteres, devuelva las coordenadas de fila y columna de la primera ocurrencia del vector en una columna de la matriz o ( 0 , 0 ) si no se encuentra. Escribir un pequeño programa de prueba.
Código
Python
def OCURRENCIA ( m, n, v, p ):
j = 0
encontro = False
while j < n and not encontro:
i = 0
while i < n and not encontro:
k = 0
while m[i][j] != v[k] and i < n-p+1:
i += 1
if m[i][j] == v[k]:
f = i
c = j
ii = i+1
k += 1
while k<p and ii<n and m[ii][j]==v[k]:
ii += 1
k += 1
if k == p:
encontro = True
i += 1
j += 1
if not encontro:
f = -1
c = -1
return f, c
def main():
while True:
print( "Matriz/matrix" )
n = int( input( "Ingresar/Enter dimension:" ) )
m = []
for i in range( n ):
m.append( [""] * n )
print( "Ingresar elementos/Enter element:\n" )
for i in range ( n ):
for j in range ( n ):
m[i][j] = input()
print( "Vector" )
p = int( input( "Ingresar/Enter dimension:" ) )
v = []
print( "Ingresar elementos/Enter element:\n" )
for i in range ( p ):
v.append( input() )
f, c = OCURRENCIA( m , n , v , p ) ;
if f == -1 and c == -1:
print( "No se encontro/No found\n" )
else:
print( "Encontrado en coordenadas", end="")
print( "/Found in coordinates:", f, c )
resp = input("Presionar S para salir/Press S to exit ").upper()
if resp == "S": break
main()
C++
#include <iostream>
#include <iomanip>
using namespace std ;
#include <conio.h>
void OCURRENCIA ( char m[][10], int n, char v[],
int p, int* f , int* c )
{
int i , ii , j , k ;
bool encontro ;
j = 0; encontro = false ;
while ( j < n && !encontro )
{
i = 0;
while ( i < n && !encontro )
{
k = 0 ;
while ( m[i][j] != v[k] && i < n-p+1 )
i++ ;
if( m[i][j] == v[k] )
{
*f = i ;
*c = j ;
ii=i+1 ;
k++ ;
while ( m[ii][j] == v[k] &&
k < p && ii < n )
{ ii++; k++; }
if ( k == p ) encontro = true ;
}
i++ ;
}
j++ ;
}
if (!encontro){ *f = -1 ; *c = -1 ; }
}
int main ()
{
char m[10][10], v[10] ;
int n, p, i, j, f, c ;
do
{
cout << "Ingresar dimension matriz/" ;
cout << "Enter matrix dimension: " ;
cin >> n ;
cout << "Ingresar elementos/" ;
cout << "Enter element: " << endl << endl ;
for( i=0 ; i<n ; i++ )
for( j=0 ; j<n ; j++ )
cin >> m[i][j] ;
cout << "Ingresar dimension vector/" ;
cout << "Enter vector dimension: " ;
cin >> p ;
cout << "Ingresar elementos/" ;
cout << "Enter element: " << endl << endl ;
for( i=0 ; i<p ; i++ )
cin >> v[i] ;
OCURRENCIA( m , n , v , p , &f , &c) ;
if ( f == -1 && c == -1 )
cout << "No se encontro/No found\n" ;
else
{
cout << "Encontrado en coordenadas/" ;
cout << "Found in coordinates: " ;
cout << f << setw(5) << c ;
}
cout << endl << "\nPresionar ESC para salir/" ;
cout << " Press ESC to exit\n" ;
}
while (getch() != char (27) ) ;
return 0 ;
}
Pascal
Program Problema15_14 ;
uses CRT , PRINTER ;
const nmax = 10 ;
type
dim = 0 ..nmax ;
matriz = array [ dim , dim ] of char ;
vector = array [ dim ] of char ;
var
m : matriz ; v : vector ;
n , p , i , j , f , c : dim ;
Procedure OCURRENCIA ( m : matriz ; n : dim ;
v : vector ; p : dim ;
var f , c : dim ) ;
var i , ii , j , k : dim ; encontro : boolean ;
begin
j := 1 ; encontro := false ;
while ( j <= n ) and ( not encontro ) do
begin
i := 1 ;
while ( i <= n ) and ( not encontro ) do
begin
k := 1 ;
while ( m[ i , j ] <> v [ k ] )
and ( i <= n-p+1 ) do
i := i + 1 ;
if m[ i , j ] = v [ k ]
then
begin
f := i ; c := j ;
ii := i + 1 ;
k := k + 1 ;
while ( m[ ii , j ] = v [ k ] ) and
( k <= p ) and ( ii <= n ) do
begin
ii := ii + 1 ; k := k + 1
end ;
if k > p then encontro := true
end;
i := i + 1;
end ;
j := j + 1
end;
if not encontro then begin f := 0 ; c := 0 end
end ;
begin
repeat
ClrScr ;
write( 'Ingresar dimension matriz/' ) ;
write( 'Enter dimension matrix: ' ) ;
readln( n ) ;
writeln( 'Ingresar elementos/Enter element: ' ) ;
for i := 1 to n do
begin
for j := 1 to n do
begin
write(i , ' ', j , ' ') ;
readln( m[ i , j ] )
end ;
writeln ;
end;
write( 'Ingresar dimension vector/' ) ;
write( 'Enter dimension vector: ' ) ;
readln( p ) ;
writeln( 'Ingresar elementos/Enter element: ' ) ;
for i := 1 to p do
begin
write( i , ' ' ) ;
readln( v [ i ] )
end ;
writeln ;
OCURRENCIA( m , n , v , p , f , c) ;
if ( f = 0 ) and ( c = 0 )
then writeln ( 'No se encontro/No found' )
else
begin
write( 'Encontrado en coordenadas/' ) ;
write( 'Found in coordinates: ' ) ;
writeln( f , ' ' , c , ' ' ) ;
end ;
writeln ;
write( 'Presionar ESC para salir/' ) ;
writeln( 'Press ESC to exit ' ) ;
until ReadKey=#27
end.
Diagramas
Subprograma



Programa Principal


