poj 2458 Rigging the Bovine Election

已醉。。。

在聯通性的判斷上搞錯了一下,在只能橫豎走的情況下,只要最後的模塊聯通就行了,並不需要在7層循環每層都聯通

/*
ID: daniel.20
LANG: JAVA
TASK: tour
 */

import java.util.*;
import java.io.*;

class pb1{
    int arr[];
    void solver() throws IOException{
        //Scanner scan = new Scanner(System.in);
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));        
        arr = new int[25];
        for(int i=0;i<5;i++){
            String tmp = reader.readLine();
            for(int j=0;j<5;j++){
                if(tmp.charAt(j)=='H') arr[i*5+j]=1;
                else arr[i*5+j]=0;
            }
        }
        int count = 0;
        for(int a1=0;a1<25;a1++){
            int b1=arr[a1];
            for(int a2=a1+1;a2<25;a2++){
                int b2=b1+arr[a2];
                for(int a3=a2+1;a3<25;a3++){
                    int b3=b2+arr[a3];
                    for(int a4=a3+1;a4<25;a4++){
                        int b4=b3+arr[a4]; if(b4>=4) continue;
                        for(int a5=a4+1;a5<25;a5++){
                        int b5=b4+arr[a5]; if(b5>=4) continue;
                            for(int a6=a5+1;a6<25;a6++){
                                int b6=b5+arr[a6]; if(b6>=4) continue;
                                for(int a7=a6+1;a7<25;a7++){
                                    int b7=b6+arr[a7]; if(b7>=4) continue;
                                    //System.out.println(a1+" "+a2+" "+a3+" "+a4+" "+a5+" "+a6+" "+a7);
                                    if(is_connected(a1,a2,a3,a4,a5,a6,a7)) count++;
                                }
                            }
                        }
                    }
                }
            }
        }
        System.out.println(count);
    }
    boolean is_connected(int a, int b, int c, int d, int e, int f, int g){
        boolean is_target[] = new boolean[25];
        is_target[a]=is_target[b]=is_target[c]=is_target[d]=is_target[e]=is_target[f]=is_target[g]=true;
        boolean vis[] = new boolean[25];
        int level[] = new int[25];
        int count = 0;
        int dir[][] = {{-1,0},{1,0},{0,-1},{0,1}};
        LinkedList<Integer> q = new LinkedList<Integer>();
        q.add(a);vis[a]=true;
        while(!q.isEmpty()){
            int tmp = q.poll();
            if(is_target[tmp]) count++;
            if(count==7) return true;
            int x=tmp/5; int y=tmp%5;
            for(int i=0;i<4;i++){
                int nx = x+dir[i][0];
                int ny = y+dir[i][1];
                if(nx>=0&&nx<5&&ny>=0&&ny<5&&!vis[nx*5+ny]&&level[nx*5+ny]<=6&&is_target[nx*5+ny]){
                    vis[nx*5+ny]=true;
                    level[nx*5+ny]=level[tmp]+1;
                    q.push(nx*5+ny);
                }
            }
        }
        return false;
    }
}
public class tour {
    public static void main(String[] args) throws Exception {
        pb1 p = new pb1();
        p.solver();        
    }
}
 


發佈了245 篇原創文章 · 獲贊 0 · 訪問量 10萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章