pku 1198 Solitaire 搜索+剪枝

题意:
      应该是跳棋游戏(我奶奶经常在家玩。。),一个8*8棋盘,棋子可以在棋盘上前后左右挪一格或者跳一格(如果相邻格子有棋子的话),问初始状态在8步内能否达到给定的终止状态。
下限函数仍然选择不在位置上的棋子个数,然后减枝即可。。
话说POJ卡常数,觉得复杂度应该可以了,就是TLE,然后到TOJ上尝试提交了下,1A,然后只好回来优化常数,把判重换成数组判重,以6S的时间过了。。哎,JAVA就是可怜啊。。
  1 import java.util.*;
  2 import java.io.*;
  3 public class Main {
  4 
  5     /**
  6      * @param args
  7      */
  8     static point p1[]=new point[4],p2[]=new point[4];
  9     static boolean map[][]=new boolean[10][10];
 10     static boolean map1[][]=new boolean[10][10];
 11     static class point
 12     {
 13         int r,c;
 14         public point(int rr,int cc)
 15         {
 16             r=rr;
 17             c=cc;
 18         }
 19         public boolean equals(point pos)
 20         {
 21             return r==pos.r&&c==pos.c;
 22         }
 23     }
 24     static final boolean isnotin(int r,int c,point p[])
 25     {
 26         for(int i=0;i<4;i++)
 27             if(p[i].r==r&&p[i].c==c)
 28                 return false;
 29         return true;
 30     }
 31     static boolean dfs(point p[],int diff,int layer)
 32     {
 33         if(layer+diff>8return false;
 34         else if(diff==0
 35         {
 36             return true;
 37         
 38         }
 39         else
 40         {
 41             for(int i=0;i<4;i++)
 42             {
 43                 if(p[i].r+1<8&&isnotin(p[i].r+1,p[i].c,p))
 44                 {
 45                     p[i].r++;
 46                     if(dfs(p,diff+(isnotin(p[i].r-1,p[i].c,p2)?0:1)-(map[p[i].r][p[i].c]?1:0),layer+1)) 
 47                         {
 48                     
 49                             return true;
 50                         }
 51                     p[i].r--;
 52                 }
 53                 if(p[i].c+1<8&&isnotin(p[i].r,p[i].c+1,p))
 54                 {
 55                     p[i].c++;
 56                     if(dfs(p,diff+(isnotin(p[i].r,p[i].c-1,p2)?0:1)-(map[p[i].r][p[i].c]?1:0),layer+1)) 
 57                     {
 58                     
 59                         return true;
 60                     }
 61                     p[i].c--;
 62                 }
 63                 if(p[i].c-1>=0&&isnotin(p[i].r,p[i].c-1,p))
 64                 {
 65                     p[i].c--;
 66                     if(dfs(p,diff+(isnotin(p[i].r,p[i].c+1,p2)?0:1)-(map[p[i].r][p[i].c]?1:0),layer+1)) 
 67                     {
 68                         
 69                         return true;
 70                     }
 71                     p[i].c++;
 72                 }
 73                 if(p[i].r-1>=0&&isnotin(p[i].r-1,p[i].c,p))
 74                 {
 75                     p[i].r--;
 76                     if(dfs(p,diff+(isnotin(p[i].r+1,p[i].c,p2)?0:1)-(map[p[i].r][p[i].c]?1:0),layer+1)) 
 77                     {
 78                         
 79                         return true;
 80                     }
 81                     p[i].r++;
 82                 }
 83                 
 84                 if(p[i].r+2<8&&isnotin(p[i].r+2,p[i].c,p)&&!isnotin(p[i].r+1,p[i].c,p))
 85                 {
 86                     p[i].r+=2;
 87                     if(dfs(p,diff+(isnotin(p[i].r-2,p[i].c,p2)?0:1)-(map[p[i].r][p[i].c]?1:0),layer+1))
 88                     {
 89                         
 90                         return true;
 91                     }
 92                     p[i].r-=2;
 93                     
 94                 }
 95                 if(p[i].c+2<8&&isnotin(p[i].r,p[i].c+2,p)&&!isnotin(p[i].r,p[i].c+1,p))
 96                 {
 97                     p[i].c+=2;
 98                     if(dfs(p,diff+(isnotin(p[i].r,p[i].c-2,p2)?0:1)-(map[p[i].r][p[i].c]?1:0),layer+1)) 
 99                     {
100                         
101                         return true;
102                     }
103                     p[i].c-=2;
104                 }
105                 if(p[i].c-2>=0&&isnotin(p[i].r,p[i].c-2,p)&&!isnotin(p[i].r,p[i].c-1,p))
106                 {
107                     p[i].c-=2;
108                     if(dfs(p,diff+(isnotin(p[i].r,p[i].c+2,p2)?0:1)-(map[p[i].r][p[i].c]?1:0),layer+1)) 
109                     {
110                         
111                         return true;
112                     }
113                     p[i].c+=2;
114                 }
115                 if(p[i].r-2>=0&&isnotin(p[i].r-2,p[i].c,p)&&!isnotin(p[i].r-1,p[i].c,p))
116                 {
117                     p[i].r-=2;
118                     if(dfs(p,diff+(isnotin(p[i].r+2,p[i].c,p2)?0:1)-(map[p[i].r][p[i].c]?1:0),layer+1)) 
119                     {
120                         
121                         return true;
122                     }
123                     p[i].r+=2;
124                 }
125                 
126                 
127             }
128             return false;
129         }
130     }
131     public static void main(String[] args) throws IOException{
132         Scanner in=new Scanner(new BufferedReader(new InputStreamReader(System.in)));
133         for(int i=0;i<4;i++)
134            p1[i]=new point(in.nextInt()-1,in.nextInt()-1);
135         for(int i=0;i<4;i++)
136            p2[i]=new point(in.nextInt()-1,in.nextInt()-1);
137         for(int i=0;i<8;i++)
138         {
139             Arrays.fill(map[i], false);
140             Arrays.fill(map1[i],false);
141         }
142         for(int i=0;i<4;i++)
143         {
144             map[p2[i].r][p2[i].c]=true;
145             map1[p1[i].r][p1[i].c]=true;
146         }
147         int diff=0;
148         for(int i=0;i<4;i++)
149         {
150             boolean flag=false;
151             for(int j=0;j<4&&!flag;j++)
152                 if(p1[i].equals(p2[j]))
153                     flag=true;
154             if(!flag) diff++;
155         }
156         if(dfs(p1,diff,0)) System.out.println("YES");
157         else System.out.println("NO");
158 
159     }
160 
161 }
162 


posted on 2010-10-16 01:50 yzhw 阅读(180) 评论(0)  编辑 收藏 引用 所属分类: search


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


<2010年10月>
262728293012
3456789
10111213141516
17181920212223
24252627282930
31123456

导航

统计

公告

统计系统

留言簿(1)

随笔分类(227)

文章分类(2)

OJ

最新随笔

搜索

积分与排名

最新评论

阅读排行榜