Q small painter

 

Links: https://www.nowcoder.com/questionTerminal/6acc6504df67406c98a75f5575e4b94a?orderByHotValue=1&page=1&onlyReference=false
Source: Cattle-off network

Q small painter began his artistic creation. Have come up with a small Q NxM pixel grid slate, Artboards initial state is empty, with the 'X' represents.
Q has his own small painting techniques, each Q is selected a little oblique, if the direction of the oblique lines of the form '/', i.e. 1, Q will choose some small lattice oblique lines in this slope, are coated Videos blue, with 'B'; if one diagonal direction of the form '\', i.e. the slope of -1, Q is selected smaller lattice period is in this shaded, are painted yellow, with ' Y 'represents.
If a grid is both over-painted blue and yellow paint is off, then the grid will turn green, with 'G' representation.
Small Q has drawn look like to work, you help him calculate how many operations he had at least need to complete the picture.

Enter a description:

Each input comprises a test. 
The first line of each test case contains two positive integers N and M (1 <= N, M <= 50), indicates the length and width drawing board. 
The next line N contains N strings of length M, which contains the character 'B', 'Y', 'G', 'X', respectively, blue, yellow, green, white. Q represents the entire small works to be completed.

Output Description:

Output a positive integer representing the number of operations required to complete a minimum of small Q painting.

Example 1

Entry

4 4
YXXB
XYGX
XBYY
BXXY

Export

3

Explanation

XXXX
XXXX
XXXX
XXXX
->
YXXX
XYXX
XXYX
XXXY
->
YXXB
XYBX
XBYX
BXXY
->
YXXB
XYGX
XBYY
BXXY

 

First on problem-solving ideas, people feel this question is actually not difficult, is very annoying, such a sum, "\", painted

YXXX

XYXX

XXYX

XXXY

Ye Hao, which can be considered a fortune.

If painted

YXXX

XXXX

XXYX

XXXY would be two pens.

Read some other people wrote answers, I carefully thought about this question carefully, this place is probably the most annoying problem this matrix, no way to check sideways or vertically investigation, the investigation can only be placed at an angle, but also consider the middle off the problem, and what BGX also affects judgment, can only be used to compare the characters.

Here I conversion matrix inside a bit symbols, X is converted to 1, B is converted to 2, Y is converted into. 4, 6 is converted to G, I considered here is superimposed with further adding two numbers G, specifically how to distinguish You can see the code, suddenly thought of writing this article, multiplication will be a little better, which is set to 1,2,3,6, we are interested can try, can reduce judgment conditions.

XBGBX
YBBYB
BGGXX
XYYBG
XYBGG
YYXYX

Converted to him:
. 1 2. 1 2. 6 
. 4 2 2 2. 4 
2. 6 . 6 . 1. 1 
. 1. 4. 4 2 . 6 
. 1. 4 2. 6 . 6 
. 4. 4. 1. 1. 4

This operation has been very smooth, because the number to be placed at an angle, so I do first a change is in accordance with the array of slashes to his stand up. The array is rotated clockwise 45 ° (see those bold)

Become like (see black bold position)

0 0 0 0 0 . 1 0 0 0 0 
0 0 0 0. 4 0 2 0 0 0 
0 0 0 2 0 2 0. 6 0 0 
0 0. 1 0. 6 0 2 0 2 0 
0. 1 0. 4 0 . 6 0. 4 0. 1 
. 4 0. 4 0. 4 0. 1 0 2 0 
0. 4 0 2 0 2 0. 1 0 0 
0 0. 1 0. 6 0. 6 0 0 0 
0 0 0. 4 0 . 6 0 0 0 0 
0 0 0 0. 1 0 0 0 0 0 
then is bristling find 2,6. 4,6 sideways look, consider whether the data center has broken, the data ultimately pay more to make 1.

Code is as follows: all have AC.

import java.util.Scanner;
public class Main {
	public static void main(String[] args){
		Scanner sc = new Scanner(System.in); 
		int  n=sc.nextInt();
		int  m=sc.nextInt();
		String tem;
		tem = sc.nextLine();
		int  color[][] = new int [n][m];
			        for (int i = 0; i < n; i++) {//转换BYGX为1246
			            tem = sc.nextLine();
			            for (int j = 0; j < tem.length(); j++) {
			            	if(tem.charAt(j)=='B')
			            		color[i][j] = 2;
			            	if(tem.charAt(j)=='Y')
				                color[i][j] = 4;
			            	if(tem.charAt(j)=='G')
					            color[i][j] = 6;
			            	if(tem.charAt(j)=='X')
				                color[i][j] = 1;
			            }
			        } 
			        int [][] array= new int [m+n-1][m+n-1];
			        for(int i=0;i<n;i++)//数组给他立起来,顺时针转45°
			        	for(int j=0;j<m;j++)
			        		array[i+j][n-1-i+j]=color[i][j];
			       	int sum=0;
			       	int tag=0;
			       	for(int i=0;i<m+n-1;i++)
			       		{
			       		tag=0;
			       		for(int j=0;j<m+n-1;j++)//横着找
			       		
			       			{if(array[i][j]%4==2 && tag==0)
			       			{
			       				sum++;
			       				tag=1;
			       			}
			       			if(array[i][j]!=0&&array[i][j]%4!=2 && tag==1)//中间不连续情况
			       			{
			       				tag=0;
			       			}
			       			}
			       		}
			       	for(int i=0;i<m+n-1;i++)//竖着找
			       		{tag=0;
			       		for(int j=0;j<m+n-1;j++)
			       			{	
			       			if(array[j][i]/4==1 && tag==0)
			       		
			       			{
			       				sum++;
			       				tag=1;
			       			}
			       			if(array[j][i]!=0 && array[j][i]/4!=1&& tag==1)//中间不连续情况
			       			{
			       				tag=0;
			       			}
			       			}
			       		}
 System.out.println(sum);
}
}

 

 

 

 

 

 

 

 

Guess you like

Origin blog.csdn.net/mad_sword/article/details/91891381