the painter Xiao Q began his artistic creation again. Xiao Q took out an artboard with NxM pixels. The initial state of the artboard is blank, represented by"X".
Xiao Q has his unique painting skills, each time Xiao Q will choose a slash, if the direction of the slash is like"/", that is, the slope is 1, Xiao Q will choose a grid in this slash, all painted blue, expressed with"B"; if the diagonal is shaped like"", that is, the slope is-1, Xiao Q will choose a grid in this slash, all painted yellow, expressed with"Y".
if a grid is painted both blue and yellow, the grid will turn green, denoted by"G".
Xiao Q already has the appearance of the work he wants to draw. Please help him calculate the minimum number of operations he needs to complete the painting.
NM(1 <= N, M <= 50),
NNM, "B","Y","G","X",,,,Q
< H2 > output description < / H2 >
, Q
< H2 > example 1 < / H2 >
enter
4 4
YXXB
XYGX
XBYY
BXXY
output
3
description
XXXX
XXXX
XXXX
XXXX
->
YXXX
XYXX
XXYX
XXXY
->
YXXB
XYBX
XBYX
BXXY
->
YXXB
XYGX
XBYY
BXXY
< H1 > sources of topics and their own ideas < / H1 >
< H2 > Link < / H2 >
https://www.nowcoder.com/ques.
< H2 > Source < / H2 >
< H2 > ideas < / H2 >
start traversing from the (0P0) position
if the traversal to (iMagnej) position is B br->, then continue traversing to the lower left (iLim 1) and the upper right (iMub 1)
, when you encounter setting B to X and G to (eliminating B) to Y
countPP;.
if traversing to the (iMagnej) position is Ymuri->, then continue traversing to the upper left (iMutel 1) and right
, when you encounter setting Y to X and G setting (eliminating Y) to B
countPP;
if traversing to (iLimel j) position is GLY->, execute 1Q 2
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String tem = scanner.nextLine();
int n = Integer.valueOf(tem.split(" ")[0]);
int m = Integer.valueOf(tem.split(" ")[1]);
char color[][] = new char[n][m];
for (int i = 0; i < n; iPP) {
tem = scanner.nextLine();
for (int j = 0; j < tem.length(); jPP) {
color[i][j] = tem.charAt(j);
}
}
getMinStep(n, m, color);
scanner.close();
}
private static void getMinStep(int n, int m, char color[][]) {
int step = 0;
for (int i = 0; i < n; iPP) {
for (int j = 0; j < m; jPP) {
if (color[i][j] == "Y") {
dray(i, j, n, m, color);
stepPP;
} else if (color[i][j] == "B") {
drab(i, j, n, m, color);
stepPP;
} else if (color[i][j] == "G") {
dray(i, j, n, m, color);
stepPP;
drab(i, j, n, m, color);
stepPP;
}
}
}
System.out.println(step);
}
private static void dray(int x, int y, int n, int m, char color[][]) {
if (x >= 0 && x < n && y >= 0 && y < m && (color[x][y] == "Y" || color[x][y] == "G")) {
if (color[x][y] == "G") {
color[x][y] = "B";
} else {
color[x][y] = "X";
}
dray(x - 1, y - 1, n, m, color);
dray(x + 1, y + 1, n, m, color);
}
}
private static void drab(int x, int y, int n, int m, char color[][]) {
if (x >= 0 && x < n && y >= 0 && y < m && (color[x][y] == "B" || color[x][y] == "G")) {
if (color[x][y] == "G") {
color[x][y] = "Y";
} else {
color[x][y] = "X";
}
drab(x + 1, y - 1, n, m, color);
drab(x - 1, y + 1, n, m, color);
}
}
}
< H2 > JavaScript implementation (failed! Time out! ) < / H2 >
while(line=readline()){
let lines = line.split(" ");
let n = parseInt(lines[0]);
let m = parseInt(lines[1]);
let arr = new Array();
for(let i = 0; i < n; iPP){
let line2 = readline();
arr[i] = new Array();
for(let j = 0; j < m; jPP){
arr[i][j] = line2[j];
//print(arr[i][j]);
}
}
let step = 0;
for(let i = 0; i < n; iPP){
for(let j = 0; j < m; jPP){
//print("----------sharp-sharp (" + i +" , "+ j + " ): " +arr[i][j]);
if(arr[i][j] == "Y"){
draw_y(arr, i, j, n, m);
stepPP;
}else if(arr[i][j] == "B"){
draw_b(arr, i, j, n, m);
stepPP;
}else if(arr[i][j] == "G"){
draw_y(arr, i, j, n, m);
stepPP;
draw_b(arr, i, j, n, m);
stepPP
}
}
}
print(step);
}
function draw_y(arr,x,y,n,m){
//print("draw_y:");
//print(x,y,n,m);
if(x >= 0 && x < n && y >=0 && y < m && (arr[x][y] == "Y" || arr[x][y] == "G")){
if(arr[x][y] == "Y"){
arr[x][y] == "X";
}else{
arr[x][y] == "B";
}
//print("("+x+","+y+")"+":");
draw_y(arr, x-1, y-1, n, m);
//print("("+x+","+y+")"+":");
draw_y(arr, x+1, y+1, n, m);
}
}
function draw_b(arr,i,j,n,m){
//print("draw_b:");
//print(i,j,n,m);
if(i >= 0 && i < n && j >=0 && j < m && (arr[i][j] == "G" || arr[i][j] == "B")){
if(arr[i][j] == "B"){
arr[i][j] == "X";
}else{
arr[i][j] == "Y";
}
//print("("+i+","+j+")"+":");
draw_b(arr, i-1, j+1, n, m);
//print("("+i+","+j+")"+":");
draw_b(arr, i+1,j-1,n,m);
}
}
< H2 > error message < / H2 >
has been debugging for many days, but it just can"t pass! Help!
a.v8js:36: RangeError: Maximum call stack size exceeded
function draw_y(arr,x,y,n,m){
^
RangeError: Maximum call stack size exceeded
at draw_y (a.v8js:36:16)
at draw_y (a.v8js:46:9)
at draw_y (a.v8js:49:9)
at draw_y (a.v8js:46:9)
at draw_y (a.v8js:49:9)
at draw_y (a.v8js:46:9)
at draw_y (a.v8js:49:9)
at draw_y (a.v8js:46:9)
at draw_y (a.v8js:49:9)
at draw_y (a.v8js:46:9)
:a.v8js:36: RangeError: Maximum call stack size exceeded
function draw_y(arr,x,y,n,m){
^
RangeError: Maximum call stack size exceeded
at draw_y (a.v8js:36:16)
at draw_y (a.v8js:46:9)
at draw_y (a.v8js:49:9)
at draw_y (a.v8js:46:9)
at draw_y (a.v8js:49:9)
at draw_y (a.v8js:46:9)
at draw_y (a.v8js:49:9)
at draw_y (a.v8js:46:9)
at draw_y (a.v8js:49:9)
at draw_y (a.v8js:46:9)