Flood-it!

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4127/http://acm.split.hdu.edu.cn/showproblem.php?pid=4127

IDA*

这题题意有点不清,我去找了这个游戏玩了会才明白什么回事= =

(游戏链接:http://unixpapa.com/floodit/?sz=26&nc=6

刚开始我将状态压缩成2^64(用unsigned long long存储,当n=8全部都是一种颜色时用(unsigned long long)(-1)特判),用map去重,dfs判断每次互通的格子,A*是该状态下的颜色种类-1,一次次迭代加深,结果是TLE...

代码如下:

 1 #include<cstdio>
 2 #include<cmath>
 3 #include<iostream>
 4 #include<set>
 5 #include<map>
 6 #include<cstring>
 7 #define N 8
 8 using namespace std;
 9 typedef unsigned long long LL;
10 int mp[N][N],n,deep;
11 bool vis[N][N],ok;
12 map<LL,bool>state;
13 int dx[]={-1,1,0,0};
14 int dy[]={0,0,-1,1};
15 LL dfs(int px,int py,int color,LL s){
16     LL sta=s;
17     vis[px][py]=1;
18     for(int i=0;i<4;++i){
19         int x=px+dx[i];
20         int y=py+dy[i];
21         if(0<=x&&x<n&&0<=y&&y<n)
22         if(color==mp[x][y]||(sta&((LL)1<<(x*n+y)))>0)
23         if(!vis[x][y]){
24             sta|=((LL)1<<(x*n+y));
25             sta|=dfs(x,y,color,sta);
26         }
27     }
28     return sta;
29 }
30 LL init(){
31     ok=0;
32     state.clear();
33     memset(vis,0,sizeof(vis));
34     return dfs(0,0,mp[0][0],1);
35 }
36 int Astar(int color,LL s){
37     set<int>st;
38     st.insert(color);
39     for(int i=0;i<n;++i)
40     for(int j=0;j<n;++j)
41     if((s&(1<<(i*n+j)))==0)
42     if(st.count(mp[i][j])==0)
43         st.insert(mp[i][j]);
44     return (st.size()-1);
45 }
46 void IDAstar(int color,LL sta,int step){
47     if(ok)return;
48     if(n<8){
49         if(sta==((LL)1<<n*n)-1){
50             ok=1;
51             return;
52         }
53     }else{
54         if(sta==(LL)-1){
55             ok=1;
56             return;
57         }
58     }
59     int h=Astar(color,sta);
60     if(step+h>deep)return;
61     for(int i=0;i<6;++i){
62         memset(vis,0,sizeof(vis));
63         LL s=dfs(0,0,i,sta);
64         if(!state[s]){
65             state[s]=1;
66             IDAstar(i,s,step+1);
67             state[s]=0;
68         }
69         if(ok)return;
70     }
71 }
72 int main(void){
73     while(scanf("%d",&n)){
74         if(n==0)break;
75         for(int i=0;i<n;++i)
76         for(int j=0;j<n;++j)
77             scanf("%d",&mp[i][j]);
78         LL s=init();
79         if(n<8){
80             if(s==((LL)1<<(n*n))-1){
81                 printf("0\n");
82                 continue;
83             }
84         }else{
85             if(s==(LL)-1){
86                 printf("0\n");
87                 continue;
88             }
89         }
90         for(deep=1;!ok;deep++){
91             state.clear();
92             state[s]=1;
93             IDAstar(mp[0][0],s,0);
94         }
95         printf("%d\n",deep-1);
96     }
97 }
View Code

相关文章:

  • 2022-01-30
  • 2021-10-17
  • 2021-10-07
  • 2021-07-30
  • 2022-12-23
  • 2021-10-18
  • 2021-10-28
  • 2021-08-02
猜你喜欢
  • 2022-12-23
  • 2021-10-20
  • 2021-09-26
  • 2021-12-27
  • 2021-10-06
  • 2022-12-23
  • 2022-03-07
相关资源
相似解决方案