가중치 척도와 각 가중치의 무한한 공급이 있는 다양한 양의 가중치 배열이 제공됩니다. 우리의 임무는 팬이 무게가 놓인 쪽으로 이동하는 방식, 즉 저울 팬이 번갈아 가며 이동할 때마다 저울의 왼쪽 및 오른쪽 팬에 하나씩 무게를 두는 것입니다.
- 이 작업을 수행하는 데 필요한 또 다른 정수 '단계' 시간이 제공됩니다.
- 또 다른 제약은 동일한 무게를 연속적으로 넣을 수 없다는 것입니다. 즉, 무게 w를 가져오면 다음 단계에서 반대쪽 팬에 무게를 얹는 동안 w를 다시 가져올 수 없습니다.
예:
Let weight array is [7 11] and steps = 3 then 7 11 7 is the sequence in which weights should be kept in order to move scale alternatively. Let another weight array is [2 3 5 6] and steps = 10 then 3 2 3 5 6 5 3 2 3 is the sequence in which weights should be kept in order to move scale alternatively.
이 문제는 다음을 수행하여 해결될 수 있습니다. DFS 규모 상태 중.
- 각 DFS 상태가 왼쪽과 오른쪽 팬 간의 실제 차이 값과 현재 단계 수에 해당하는 솔루션을 위해 다양한 DFS 상태를 탐색합니다.
- 두 팬의 무게를 저장하는 대신 우리는 차이 잔여 값을 저장하고 매번 선택한 무게 값은 이 차이보다 커야 하며 이전에 선택한 무게 값과 같아서는 안 됩니다.
- 그렇다면 새로운 차이 값과 한 단계를 더 사용하여 DFS 메서드를 재귀적으로 호출합니다.
더 나은 이해를 위해 아래 코드를 참조하십시오.
C++// C++ program to print weights for alternating // the weighting scale #include using namespace std; // DFS method to traverse among states of weighting scales bool dfs(int residue int curStep int wt[] int arr[] int N int steps) { // If we reach to more than required steps // return true if (curStep > steps) return true; // Try all possible weights and choose one which // returns 1 afterwards for (int i = 0; i < N; i++) { /* Try this weight only if it is greater than current residueand not same as previous chosen weight */ if (arr[i] > residue && arr[i] != wt[curStep - 1]) { // assign this weight to array and recur for // next state wt[curStep] = arr[i]; if (dfs(arr[i] - residue curStep + 1 wt arr N steps)) return true; } } // if any weight is not possible return false return false; } // method prints weights for alternating scale and if // not possible prints 'not possible' void printWeightsOnScale(int arr[] int N int steps) { int wt[steps]; // call dfs with current residue as 0 and current // steps as 0 if (dfs(0 0 wt arr N steps)) { for (int i = 0; i < steps; i++) cout << wt[i] << ' '; cout << endl; } else cout << 'Not possiblen'; } // Driver code to test above methods int main() { int arr[] = {2 3 5 6}; int N = sizeof(arr) / sizeof(int); int steps = 10; printWeightsOnScale(arr N steps); return 0; }
Java // Java program to print weights for alternating // the weighting scale class GFG { // DFS method to traverse among // states of weighting scales static boolean dfs(int residue int curStep int[] wt int[] arr int N int steps) { // If we reach to more than required steps // return true if (curStep >= steps) return true; // Try all possible weights and // choose one which returns 1 afterwards for (int i = 0; i < N; i++) { /* * Try this weight only if it is * greater than current residue * and not same as previous chosen weight */ if (curStep - 1 < 0 || (arr[i] > residue && arr[i] != wt[curStep - 1])) { // assign this weight to array and // recur for next state wt[curStep] = arr[i]; if (dfs(arr[i] - residue curStep + 1 wt arr N steps)) return true; } } // if any weight is not possible // return false return false; } // method prints weights for alternating scale // and if not possible prints 'not possible' static void printWeightOnScale(int[] arr int N int steps) { int[] wt = new int[steps]; // call dfs with current residue as 0 // and current steps as 0 if (dfs(0 0 wt arr N steps)) { for (int i = 0; i < steps; i++) System.out.print(wt[i] + ' '); System.out.println(); } else System.out.println('Not Possible'); } // Driver Code public static void main(String[] args) { int[] arr = { 2 3 5 6 }; int N = arr.length; int steps = 10; printWeightOnScale(arr N steps); } } // This code is contributed by // sanjeev2552
Python3 # Python3 program to print weights for # alternating the weighting scale # DFS method to traverse among states # of weighting scales def dfs(residue curStep wt arr N steps): # If we reach to more than required # steps return true if (curStep >= steps): return True # Try all possible weights and choose # one which returns 1 afterwards for i in range(N): # Try this weight only if it is greater # than current residueand not same as # previous chosen weight if (arr[i] > residue and arr[i] != wt[curStep - 1]): # assign this weight to array and # recur for next state wt[curStep] = arr[i] if (dfs(arr[i] - residue curStep + 1 wt arr N steps)): return True # if any weight is not possible # return false return False # method prints weights for alternating scale # and if not possible prints 'not possible' def printWeightsOnScale(arr N steps): wt = [0] * (steps) # call dfs with current residue as 0 # and current steps as 0 if (dfs(0 0 wt arr N steps)): for i in range(steps): print(wt[i] end = ' ') else: print('Not possible') # Driver Code if __name__ == '__main__': arr = [2 3 5 6] N = len(arr) steps = 10 printWeightsOnScale(arr N steps) # This code is contributed by PranchalK
C# // C# program to print weights for alternating // the weighting scale using System; namespace GFG { class Program { // DFS method to traverse among states of weighting scales static bool dfs(int residue int curStep int[] wt int[] arr int N int steps) { // If we reach to more than required steps return true if (curStep >= steps) return true; // Try all possible weights and choose one which returns 1 afterwards for (int i = 0; i < N; i++) { /* * Try this weight only if it is greater than current residue * and not same as previous chosen weight */ if (curStep - 1 < 0 || (arr[i] > residue && arr[i] != wt[curStep - 1])) { // assign this weight to array and recur for next state wt[curStep] = arr[i]; if (dfs(arr[i] - residue curStep + 1 wt arr N steps)) return true; } } // if any weight is not possible return false return false; } // method prints weights for alternating scale and // if not possible prints 'not possible' static void printWeightOnScale(int[] arr int N int steps) { int[] wt = new int[steps]; // call dfs with current residue as 0 and current steps as 0 if (dfs(0 0 wt arr N steps)) { for (int i = 0; i < steps; i++) Console.Write(wt[i] + ' '); Console.WriteLine(); } else Console.WriteLine('Not Possible'); } static void Main(string[] args) { int[] arr = { 2 3 5 6 }; int N = arr.Length; int steps = 10; printWeightOnScale(arr N steps); } } }
JavaScript function dfs(residue curStep wt arr N steps) { // If we reach to more than required steps // return true if (curStep > steps) { return true; } // Try all possible weights and choose one which // returns 1 afterwards for (let i = 0; i < N; i++) { /* Try this weight only if it is greater than current residue and not same as previous chosen weight */ if (arr[i] > residue && arr[i] !== wt[curStep - 1]) { // assign this weight to array and recur for // next state wt[curStep] = arr[i]; if (dfs(arr[i] - residue curStep + 1 wt arr N steps)) { return true; } } } // if any weight is not possible return false return false; } function printWeightsOnScale(arr N steps) { const wt = new Array(steps); // call dfs with current residue as 0 and current // steps as 0 if (dfs(0 1 wt arr N steps)) { for (let i = 1; i <= steps; i++) { process.stdout.write(`${wt[i]} `); } console.log(); } else { console.log('Not possible'); } } const arr = [2 3 5 6]; const N = arr.length; const steps = 10; printWeightsOnScale(arr N steps); // This code is contributed by divyansh2212
산출:
2 3 2 3 5 6 5 3 2 3
퀴즈 만들기