博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
FZU 2092 收集水晶 BFS记忆化搜索
阅读量:4326 次
发布时间:2019-06-06

本文共 2689 字,大约阅读时间需要 8 分钟。

用了两个pii代码有点长……

记忆化搜索主要还是用用dp数组去记录并更新状态

 

1 #include
2 #include
3 #include
4 #include
5 #include
6 #include
7 #include
8 #include
9 #include
10 #include
11 #include
12 #include
13 #include
14 #include
15 #include
16 #include
17 #include
18 #define cl(a,b) memset(a,b,sizeof(a)) 19 #define debug(x) cerr<<#x<<"=="<<(x)<
pii; 24 25 const int inf=0x3f3f3f3f; 26 const int maxn=12; 27 const int mod=1e7+7; 28 const double eps=1e-8; 29 const double pi=acos(-1); 30 31 int dx[8]= {-1,0,1,0,0}; 32 int dy[8]= { 0,1,0,-1,0}; 33 34 int n,m,ans; 35 bool mp[maxn][maxn]; 36 int value[maxn][maxn][205]; 37 int dp[maxn][maxn][maxn][maxn][205]; 38 39 struct people 40 { 41 int time; 42 pii x,y; 43 }; 44 45 bool check(people x) 46 { 47 if(x.x.first<1||x.x.first>n) return false; 48 if(x.x.second<1||x.x.second>m) return false; 49 if(x.y.first<1||x.y.first>n) return false; 50 if(x.y.second<1||x.y.second>m) return false; 51 if(!mp[x.x.first][x.x.second]) return false; 52 if(!mp[x.y.first][x.y.second]) return false; 53 return true; 54 } 55 56 void init() 57 { 58 ans=0; 59 cl(mp,0); 60 cl(dp,-1); 61 cl(value,0); 62 } 63 64 void bfs() 65 { 66 queue
q; 67 while(!q.empty()) q.pop(); 68 people st; 69 st.x.first=1,st.x.second=1; 70 st.y.first=1,st.y.second=1; 71 st.time=0; 72 dp[st.x.first][st.x.second][st.y.first][st.y.second][st.time]=0; 73 q.push(st); 74 while(!q.empty()) 75 { 76 people tmp=q.front(); 77 q.pop(); 78 if(tmp.time>200) break; 79 for(int i=0;i<5;i++) 80 { //人和影子分别走 81 for(int j=0;j<5;j++) 82 { 83 people use; 84 use.x.first=tmp.x.first+dx[i]; 85 use.x.second=tmp.x.second+dy[i]; 86 use.y.first=tmp.y.first+dx[j]; 87 use.y.second=tmp.y.second+dy[j]; 88 use.time=tmp.time+1; 89 if(check(use)) 90 { 91 int now=dp[tmp.x.first][tmp.x.second][tmp.y.first][tmp.y.second][tmp.time]; 92 now+=value[use.x.first][use.x.second][use.time]; 93 now+=value[use.y.first][use.y.second][use.time]; 94 if(use.x.first==use.y.first&&use.x.second==use.y.second) 95 now-=value[use.x.first][use.x.second][use.time]; 96 if(now>dp[use.x.first][use.x.second][use.y.first][use.y.second][use.time]) 97 { 98 if(dp[use.x.first][use.x.second][use.y.first][use.y.second][use.time]==-1) 99 { //判断是否更新过100 q.push(use);101 }102 dp[use.x.first][use.x.second][use.y.first][use.y.second][use.time]=now;103 ans=max(now,ans);104 }105 }106 }107 }108 }109 }110 111 int main()112 {113 int T;114 scanf("%d",&T);115 while(T--)116 {117 scanf("%d%d",&n,&m);118 init();119 char str[15];120 for(int i=1;i<=n;i++)121 {122 scanf("%s",str);123 for(int j=0;j

 

转载于:https://www.cnblogs.com/general10/p/6497918.html

你可能感兴趣的文章
206. Reverse Linked List(LeetCode)
查看>>
day 04 Java并发多线程
查看>>
Java定时任务调度工具Timer Quartz
查看>>
WPF,Silverlight与XAML读书笔记第三十五 - 可视化效果之图像
查看>>
Nginx中location配置[转]
查看>>
编程重点
查看>>
00-A-springmvc分布式项目项目结构
查看>>
vs调试时报503错误
查看>>
SVN使用&CVS使用
查看>>
redis
查看>>
Oracle存储过程中如何使用游标
查看>>
揭开NodeJS的神秘面纱!
查看>>
Number Triangles
查看>>
Ext分页实现(前台与后台)
查看>>
转 迭代器模式
查看>>
CYQ.Data V5 MAction新增加SetExpression方法说明
查看>>
数据安全&MD5加密
查看>>
bzoj 2594: 水管局长数据加强版 Link-Cut-Tree
查看>>
世界是数字的观后感
查看>>
由DBCursor的“can't switch cursor access methods”异常引发的思考
查看>>