In the n*m grass, mention mo planted k mushrooms, mushroom explosion of great power, Rambo do not want to rush to rush, and the mushroom is invisible. Only one item called a scanning lens can scan the invisible mushroom, so he goes back to the War Academy, buys 2 scanning lenses, and a scanning lens can be scanned (3*3) All the mushrooms in the box, and Rambo can clean up some of the invisible mushrooms. Q: How many mushrooms can rambo clean up?
Input Description:
The first row of three integers: n,m,k, (1≤n,m≤20,k≤100), n,m represents the size of the grassland; next K lines, two integers per line, x, Y (1≤x≤n,1≤y≤m). A mushroom is planted on behalf of (X, y). A square can grow an infinite number of mushrooms.
Output Description:
Output one line, and output an integer in this line, representing the maximum number of mushrooms that Rambo can clean up.
Java
1 ImportJava.util.Scanner;2 ImportJava.util.regex.Matcher;3 ImportJava.util.regex.Pattern;4 Public classex3{5 ex3 () {}6 Public Static voidMain (string[] Argvs) {7Scanner Scanner =NewScanner (system.in);8String str =scanner.nextline ();9 Ten //extracting numbers from a string OneString regEx = "\\d{1,}"; APattern p =Pattern.compile (regEx); -Matcher m =P.matcher (str); - intIII =-1; the int[] t =New int[3]; - while(M.find ()) { -iii++; - if(iii>2) + Break; -T[III] =NewInteger (M.group ()). Intvalue (); + } A intN =t[0]; at intM =t[1]; - intK =t[2]; - intx, y; - int[] Map =New int[N+1] [M+1]; - for(inti=0;i<k;i++){ -str =scanner.nextline (); inm =P.matcher (str); -III =-1; to while(M.find ()) { +iii++; - if(iii>1) the Break; *T[III] =NewInteger (M.group ()). Intvalue (); $ }Panax Notoginsengx = t[0]; -y = t[1]; theMap[x][y] = 1; + } A int[] book =New int[N+1] [M+1]; the intMax = 0; + ints1,s2; - for(inti=1;i<n-1;i++){ $ for(intj=2;j<m;j++){ $S1 = 0; - for(intk=-1;k<2;k++) {//First Mirror Scan - for(intL =-1; L < 2; l++){ theS1 + = map[i+k][j+l]; -BOOK[I+K][J+L] = 2;Wuyi } the } - for(intII = 1; II < N-1; ii++){ Wu for(intJJ = 2; JJ < M; jj++){ - if(ii! = I | | | JJ! =j) { AboutS2 = 0; $ for(intK =-1; K < 2; k++){ - for(intL =-1; l < 2; l++){ - if(Book[ii+k][jj+l] = = 0){ -S2 + = map[ii+k][jj+l]; A } + } the } - if((S1 + s2) >max) { $max = S1 +S2; the } the } the } the } - for(intk=-1;k<2;k++) {//First mirror scan book restoration in for(intL =-1; L < 2; l++){ theBook[i+k][j+l] = 0; the } About } the } the } the System.out.println (max); + return; - } the}
View Code
Python
#-*-coding:utf-8-*-str =raw_input () Li= Str.split (' ') M=Li[0]n= Li[1]k= Li[2]m,n,k=Int (M), int (N), int (K) map= [I forIinchRange (m*n+1)]book= [I forIinchRange (m*n+1)] forIinchRange (0,n): forJinchRange (1,m+1): Map[i*M+J] =0 forIinchRange (K): Str=raw_input () Li= Str.split (' ') x, y= Li[0],li[1] x, y=int (x), int (y) map[(x-1) *m+y] = 1 forIinchRange (1,m*n+1): Book[i]=-1Max=0 forIinchRange (1,n-1): forJinchRange (2, M): S1=0 forKinchRange ( -1,2):#First Mirror Scan forLinchRange ( -1,2): S1+ = map[(i+k) *m+j+L] book[(i+K) *m+j+l] = 2#represents a visit to a forIiinchRange (1,n-1): forJjinchRange (2, M):ifIi*m+jj! = i*m +J:s2=0 forKinchRange ( -1,2):#a second mirror scan forLinchRange ( -1,2): ifbook[(ii+k) *m+jj+l] = =-1: S2+ = map[(ii+k) *m+jj+L]ifS1 + s2 >Max:max= S1 +S2 forTinchRange (1,m*n+1): Book[t]=-1Print 'Max:', Max
View Code
[Programming question] scanning lens (also involves extracting numbers from a string)