Short c++ answer


  • 0
    J
    class Solution {
    int mn, mx;
    
    public:
    void numberOfPatterns(int r, int c, int cnt, int &res, vector<bool>& v) {
    	if (mn <= cnt && cnt <= mx)
    		res++;
    
    	if (cnt == mx)
    		return;
    
    	v[r * 3 + c] = true;
    
    	for (int i = 0; i < 3; i++) {
    		for (int j = 0; j < 3; j++) {
    			if (v[i * 3 + j]) continue;
    
    			if (abs(r - i) <= 1 && abs(c - j) <= 1 || (abs(r - i) == 2 && abs(c - j) == 1) || (abs(r - i) == 1 && abs(c - j) == 2))
    				numberOfPatterns(i, j, cnt + 1, res, v);
    			else if ((abs(r - i) == 2 && abs(c - j) == 2) && v[4]) 
    				numberOfPatterns(i, j, cnt + 1, res, v);
    			else if (abs(r - i) == 2 && abs(c - j) == 0 && v[3 + j]) 
    				numberOfPatterns(i, j, cnt + 1, res, v);
    			else if (abs(r - i) == 0 && abs(c - j) == 2 && v[i * 3 + 1]) 
    				numberOfPatterns(i, j, cnt + 1, res, v);
    		}
    	}
    
    	v[r * 3 + c] = false;
    }
    
    int numberOfPatterns(int m, int n) {
    	mn = m, mx = n;
    	vector<bool> v1(9, false);
    	int res1 = 0; numberOfPatterns(0, 0, 1, res1, v1);
    
    	vector<bool> v2(9, false);
    	int res2 = 0; numberOfPatterns(0, 1, 1, res2, v2);
    
    	vector<bool> v3(9, false);
    	int res3 = 0; numberOfPatterns(1, 1, 1, res3, v3);
    
    	return res1 * 4 + res2 * 4 + res3;
    }
    };

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.