Advertisement
knyazer

Untitled

Apr 29th, 2020
353
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.11 KB | None | 0 0
  1. package com.company;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class Main {
  6.  
  7.     static int fact(int a) {
  8.         int ans = 1;
  9.         for (int i = 1; i <= a; i++) ans *= i;
  10.         return ans;
  11.     }
  12.  
  13.     static int[][] copyArr(int[][] arr, int n, int m) {
  14.         int newArr[][] = new int [n][m];
  15.         for (int i = 0;i < n; i++) {
  16.             for (int j = 0; j < m; j++) {
  17.                 newArr[i][j] = arr[i][j];
  18.             }
  19.         }
  20.         return newArr;
  21.     }
  22.  
  23.     static int algo(int[][] arr, int w, int b, int m, int n) {
  24.         if (w == 0 && b == 0) return 1;
  25.         int ans = 0;
  26.         if (w != 0) {
  27.             for (int i = 0; i < m; i++) {
  28.                 for (int j = 0; j < n; j++) {
  29.                     int[][] copied = copyArr(arr, m, n);
  30.                     if (copied[i][j] != 'w') {
  31.                         copied[i][j] = 'w';
  32.                         ans += algo(copied, w - 1, b, m, n);
  33.                     }
  34.                 }
  35.             }
  36.         }
  37.         else {
  38.             boolean available;
  39.  
  40.             for (int i = 0; i < m; i++) {
  41.                 for (int j = 0; j < n; j++) {
  42.                     available = true;
  43.                     int[][] copied = copyArr(arr, m, n);
  44.  
  45.  
  46.                     for (int k = 0; k < n; k++)
  47.                         if (copied[i][k] == 'w') available = false;
  48.  
  49.                     for (int k = 0; k < m; k++)
  50.                         if (copied[k][j] == 'w') available = false;
  51.  
  52.                     if ((copied[i][j] == 0) && available) {
  53.                         copied[i][j] = 'b';
  54.                         ans += algo(copied, w, b - 1, m, n);
  55.                     }
  56.                 }
  57.             }
  58.         }
  59.         return ans;
  60.     }
  61.  
  62.     public static void main(String[] args) {
  63.         Scanner in = new Scanner(System.in);
  64.  
  65.         int m = in.nextInt(), n = in.nextInt();
  66.         int arr[][] = new int[m][n];
  67.  
  68.         for (int i = 0; i < m; i++) for (int j = 0; j < n; j++) arr[i][j] = 0;
  69.  
  70.         int w = in.nextInt(), b = in.nextInt();
  71.  
  72.         System.out.println(algo(arr, w, b, m, n) / (fact(w) * fact(b)));
  73.     }
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement