私はPythonまたはC++を使用するかどうかにかかわらず、それぞれの問題に対して選択肢があるプログラミング競争のために練習しています。したがって、どちらの言語でもこの問題に最も適した解決策を見つけることができます。ASCIIアート内のASCIIアートセグメントのマッチング方法は?
私が直面している過去の問題のURLはhttp://progconz.elena.aut.ac.nz/attachments/article/74/10%20points%20Problem%20Set%202012.pdf、問題F(「マップ」)です。
基本的には、大きなアートワークの中で小さなアスキーアートの出現と一致します。 C++では、ASCIIアートの各部分にベクトルを作ることができます。問題は、小さい方のピースが複数行の場合にどのようにマッチさせるかです。
私はそれについてどう考えてもわかりません。私はすべてのコードが私のために書かれたのではなく、問題に必要な論理のアイデアだけを望んでいます。
ありがとうございました。
は、ここで私はこれまで持っているものです:
#include <cstdlib>
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
using namespace std;
int main(int argc, char** argv)
{
int nScenarios, areaWidth, areaHeight, patternWidth, patternHeight;
cin >> nScenarios;
for(int a = 0; a < nScenarios; a++)
{
//get the pattern info and make a vector
cin >> patternHeight >> patternWidth;
vector< vector<bool> > patternIsBuilding(patternHeight, vector<bool>(patternWidth, false));
//populate data
for(int i = 0; i < patternHeight; i++)
{
string temp;
cin >> temp;
for(int j = 0; j < patternWidth; j++)
{
patternIsBuilding.at(i).at(j) = (temp[ j ] == 'X');
}
}
//get the area info and make a vector
cin >> areaHeight >> areaWidth;
vector< vector<bool> > areaIsBuilding(areaHeight, vector<bool>(areaWidth, false));
//populate data
for(int i = 0; i < areaHeight; i++)
{
string temp;
cin >> temp;
for(int j = 0; j < areaWidth; j++)
{
areaIsBuilding.at(i).at(j) = (temp[ j ] == 'X');
}
}
//now the vectors contain a `true` for a building and a `false` for snow
//need to find the matches for patternIsBuilding inside areaIsBuilding
//how?
}
return 0;
}
編集:私はJ.F. Sebastian
からPythonで解決策を持っている以下のコメントから。それは動作しますが、私はそれをすべて理解していません。私は何ができるのかをコメントしましたが、の文をcount_pattern
関数で理解するのに役立つ必要があります。
#function to read a matrix from stdin
def read_matrix():
#get the width and height for this matrix
nrows, ncols = map(int, raw_input().split())
#get the matrix from input
matrix = [ raw_input() for _ in xrange(nrows) ]
#make sure that it all matches up
assert all(len(row) == ncols for row in matrix)
#return the matrix
return matrix
#perform the check, given the pattern and area map
def count_pattern(pattern, area):
#get the number of rows, and the number of columns in the first row (cause it's the same for all of them)
nrows = len(pattern)
ncols = len(pattern[0])
#how does this work?
return sum(
pattern == [ row[ j:j + ncols ] for row in area[ i:i + nrows ] ]
for i in xrange(len(area) - nrows + 1)
for j in xrange(len(area[i]) - ncols + 1)
)
#get a number of scenarios, and that many times, operate on the two inputted matrices, pattern and area
for _ in xrange(int(raw_input())):
print count_pattern(read_matrix(), read_matrix())
出発点として、私は」大部分と小部分の両方を2D配列として定義することをお勧めします。真偽は建物を示し、falseは雪を示します。次に、大規模な行列内の小さな行列を見つけるために、いくつかのloop-fuを使用する必要があります。 – Vulcan
@ Vulcanありがとう、それはうまくいくようだ。私はそれに行くだろう。たぶん私はそれを受け入れることができるように答えとして追加します:) – stackunderflow
私は答えとして私のコメントに満足していません(それは理由です)コメントですが、私は実際のマトリクス内の行列の場所を書くのショットを取るかもしれませんアルゴリズム。私はひどくC++やPythonのどちらかに精通していないので、私にとって素晴らしい演習になりますが、コードで自分の質問にいつも答えることができることを忘れないでください! – Vulcan