루미 알고리즘

#1397플래티넘Surveillance

PO 2016 훈련 캠프코딩출제 Aron Granberg, Johan Sannemo영어 원문이 문제가 어렵다면 — 배우는 순서 보기

영어 원문입니다. 원문의 수식·표를 글자로 옮겨 모양이 다를 수 있습니다. 정확한 내용은 원문 페이지에서 확인하세요. 원문 페이지 열기 (새 창)

Problem

A crime has been committed in the city of <insert name here>! All of the donuts in the bakery next to the police station has mysteriously disappeared. Since this is the favourite bakery of the police, every resource available will be used to find the thief.

The police has made a list of suspected donut thieves, but there is no evidence against anyone yet. Luckily, there is security footage from the scene of the crime. Unfortunately it takes way too long time to watch all of the footage to be done before the expiry date of the donuts has passed.

Therefore, you have been tasked with writing a program to find the cookie thieves in the images. Your program will be given a W × W image of a suspected cookie thief, and a B × B image from the security footage. An image consists of a rectangular array of pixels, which we represent as integers.

Your program should count the number of occurances of the cookie thief image inside the security footage image. We say that a W × W subrectangle of the security footage contains the cookie thief image if there exists some constant C such that every pixel in the subrectangle equals the corresponding pixel in the cookie thief image plus C. This is because the images may have been taken using different exposure settings, meaning one of the images can be lighter than the other.

Example

Let the security footage be

1 2 3 4 1 2 3 4 4 3 3 4 4 3 3 4

and the cookie thief image is

0 1 0 1

Then there are five matches in the security footage: those areas with upper left corners in (0, 0), (0, 1), (0, 2), (1, 2), (2, 2) (denoted by (row, column)). The constants C are 1, 2, 3, 3, 3 respectively.

Task

Your task is to compute how many times the cookie theif image appears in the security footage image. You should implement a function surveillance(B, W, S, T).

• surveillance(B, W, S, T) - this function will be called exactly once by the judge.

  • B: an integer - the size of the security footage image.
  • W: an integer - the size of the cookie thief image.
  • W ≤ B
  • S: a two-dimensional array with B × B integers. S[i][j] (0 ≤ i, j < B) contains the value of the pixel on row i and column j in the security footage image.
  • T: a two-dimensional array with W × W integers. T[i][j] (0 ≤ i, j < W) contains the value of the pixel on row i and column j in the cookie thief.
  • 0 ≤ S[i][j], T[i][j] ≤ 10^9
  • The function should return an integer, the number of matches in the image.

A code skeleton containing the function to be implemented, together with a sample grader, can be found at http://progolymp.se/uploads/kattis-attachments/surveillance.zip.

Subtasks

The problem consists of a number of subtasks. Each subtask gives some amount of points, and to pass the subtask you must pass all the test cases in the subtask.

SubtaskPointsLimits
171 ≤ W = B ≤ 1000
2141 ≤ B ≤ 1000, 0 ≤ T[i][j], S[i][j] ≤ 1
3241 ≤ B ≤ 1000, 0 ≤ T[i][j], S[i][j] < 16
4551 ≤ B ≤ 1000

Input format

The sample judge reads input in the following format:

  • line 1: B W
  • line i = 2 to 2 + B - 1: B[i][0] B[i][1] ... B[i][B - 1]
  • line i = 2 + B to 2 + B + W - 1: W[i][0] W[i][1] ... W[i][B - 1]

Output format

The judge writes a single line containing the return value of surveillance(B, W, S, T).

예제

예제 1

입력
4 2
1 2 3 4
1 2 3 4
4 3 3 4
4 3 3 4
0 1
0 1
출력
5

출처 표기

출처: Programmeringsolympiaden 2016년 훈련 캠프 · 원문 페이지 · 이용 조건 CC BY-SA · LaTeX 원문을 화면용 글로 옮겼습니다(수식·표의 모양이 다를 수 있습니다). 내용은 고치지 않았습니다.

크리에이티브 커먼즈 저작자표시-동일조건변경허락.

문제 출처: 한국정보올림피아드(KOI) (CC BY-NC-SA 4.0) · International Olympiad in Informatics (IOI) (CC BY) · Centre for Education in Mathematics and Computing (CEMC), University of Waterloo (CC BY-NC 4.0) · Programmeringsolympiaden (CC BY-SA) · CSES Problem Set (Antti Laaksonen) (CC BY-NC-SA 4.0). 루미 알고리즘은 위 단체들과 관계없는 별도의 서비스이며, 자료는 원문 그대로 싣습니다. 문제마다 출처와 이용 조건을 따로 표기합니다.