程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> HDU3779(Railroad)

HDU3779(Railroad)

編輯:C++入門知識

[java] 
 
import java.util.*; 
 
public class Railroad { 
 
    static int[] arr1; 
    static int[] arr2; 
    static int[] arr; 
    static int n1, n2; 
    static boolean possible; 
    static boolean used[][]; 
 
    public static void main(String[] args) { 
        Scanner sc = new Scanner(System.in); 
        while (sc.hasNext()) { 
            n1 = sc.nextInt(); 
            n2 = sc.nextInt(); 
            if (n1 == 0 && n2 == 0) 
                break; 
 
            arr1 = new int[n1]; 
            arr2 = new int[n2]; 
            arr = new int[n1 + n2]; 
            used = new boolean[n1+1][n2+1]; 
            for(int i = 0;i<=n1;i++){ 
                for(int j = 0;j<=n2;j++) 
                    used[i][j] = false; 
            } 
            possible = false; 
 
            for (int i = 0; i < n1; i++) 
                arr1[i] = sc.nextInt(); 
            for (int i = 0; i < n2; i++) 
                arr2[i] = sc.nextInt(); 
            for (int i = 0; i < n1 + n2; i++) 
                arr[i] = sc.nextInt(); 
 
            sove(0, 0, 0); 
            if (possible)   www.2cto.com
                System.out.println("possible"); 
            else 
                System.out.println("not possible"); 
        } 
 
    } 
 
    private static void sove(int i, int j, int k) { 
 
        if(used[i][j])return; 
         
        if (k == (n1 + n2)) { 
            possible = true; 
            return; 
        } 
 
        // System.out.println(i + "-" + j + "-" + k); 
 
        if (i < n1 && arr1[i] == arr[k]) 
            sove(i + 1, j, k + 1); 
 
        if (j < n2 && arr2[j] == arr[k]) 
            sove(i, j + 1, k + 1); 
         
        used[i][j] = true; 
    } 
 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved