[Backtracking] C033_openj_LETTERS (wrong)

1. Title Description

Given a row × col capital letter matrix, the initial position is the upper left corner, you can move up, down, left and right in four directions, and you can not move to the letters that have passed. Ask a maximum of a few letters.

Input

In the first row, enter the number R of the letter matrix and the number S of columns, 1≤R, S≤20.
Then output the letter matrix of row R and column S.

Output

The maximum number of different letters that can be traversed.

3 6
HFDFFB
AJHGDH
DGAGEH

输出样例
6

Second, the solution

Method 1: dfs

Wrong idea: use a set to store the characters passed by each step directly. If the repeated characters are traversed, we skip and finally return the size of the set.

import java.util.*;
import java.math.*;
import java.io.*;
public class Main{
	static int R, C;
	static char[][] g;
	final static int[][] dir = { {1, 0}, {0, -1}, {-1, 0}, {0, 1}};
	static boolean[][] vis;
	static Set<Character> set;
	
	private static void dfs(int x, int y)  {
		for (int k = 0; k < 4; k++) {
			int tx = x + dir[k][0];
			int ty = y + dir[k][1];
			if (tx < 0 || tx >= R || ty < 0 || ty >= C || vis[tx][ty] || set.contains(g[tx][ty]))
				continue;
			vis[tx][ty] = true;
			set.add(g[tx][ty]);
			dfs(tx, ty);
		}
	}
    public static void main(String[] args) throws IOException {  
        Scanner sc = new Scanner(new BufferedInputStream(System.in));
        BufferedWriter w = new BufferedWriter(new OutputStreamWriter(System.out));
        
		R = sc.nextInt();
		C = sc.nextInt();
		g = new char[R][C];
		for(int i = 0; i < R; i++) {
			String s = sc.next();
			for(int j = 0; j < C; j++) {
				g[i][j] = s.charAt(j);
			}
		}
		vis = new boolean[R][C];
		set = new HashSet<>();
		vis[0][0] = true;
		set.add(g[0][0]);
		dfs(0, 0);
		System.out.println(set.size());
    }
}

Check for gaps: when it is possible to traverse, there are many characters in the set, which are larger than the size of the last set, but you did not go to record in real time. So I changed the code to this:

private static void dfs(int x, int y)  {
    max = Math.max(set.size(), max);
	for (int k = 0; k < 4; k++) {
		int tx = x + dir[k][0];
		int ty = y + dir[k][1];
		if (tx < 0 || tx >= R || ty < 0 || ty >= C || vis[tx][ty] || set.contains(g[tx][ty]))
			continue;
		vis[tx][ty] = true;	//注
		set.add(g[tx][ty]);
		dfs(tx, ty);
		set.remove(g[tx][ty]);
	}
}

Or WA (5 points): Why? This is a backtracking question ... It is already obvious that the question starts from the starting point and asks for the most different characters passed, that is, we have to run the entire grid from each position, rather than just run a series from the starting point , This complexity is O ( R × C ) O(R × C) , the traceback is O ( Finger number level do not ) O (index level)

The code is corrected as follows:

import java.util.*;
import java.math.*;
import java.io.*;
public class Main{
	static int R, C;
	static char[][] g;
	final static int[][] dir = { {1, 0}, {0, -1}, {-1, 0}, {0, 1}};
	static boolean[][] vis;
	static Set<Character> set;
	static int max;
	private static void dfs(int x, int y)  {
	    max = Math.max(set.size(), max);
		for (int k = 0; k < 4; k++) {
			int tx = x + dir[k][0];
			int ty = y + dir[k][1];
			if (tx < 0 || tx >= R || ty < 0 || ty >= C|| set.contains(g[tx][ty]))
				continue;
			set.add(g[tx][ty]);
			dfs(tx, ty);
			set.remove(g[tx][ty]);
		}
	}
    public static void main(String[] args) throws IOException {  
        Scanner sc = new Scanner(new BufferedInputStream(System.in));
        BufferedWriter w = new BufferedWriter(new OutputStreamWriter(System.out));
		R = sc.nextInt();C = sc.nextInt();
		g = new char[R][C];
		for(int i = 0; i < R; i++) {
			String s = sc.next();
			for(int j = 0; j < C; j++) {
				g[i][j] = s.charAt(j);
			}
		}
		set = new HashSet<>();
		set.add(g[0][0]);
		dfs(0, 0);
		System.out.println(max);
    }
}

Complexity analysis

  • time complexity: O ( Finger number level do not ) O (index level) ,
  • Space complexity: O ( R × C ) O(R × C)
Published 714 original articles · praised 199 · 50,000+ views

Guess you like

Origin blog.csdn.net/qq_43539599/article/details/105621224