题目:http://acm.zju.edu.cn/onlinejudge/showContestProblem.do?problemId=5501

思路:DFS,用了递归就溢出,所以可能得用非递归的。把所有可能到达终点的可能路径都计算,最后比较找最佳。限制条件很多,要细打细算。很烦,不想改了再试,写了很久了

  1 #include <iostream>
  2 #include <cstdio>
  3 #include <string>
  4 #include <map>
  5 #include <deque>
  6 #include <stack>
  7 #include <cstring>
  8 #include <algorithm>
  9 using namespace std;
 10 char dun[505][505];
 11 bool vis[505][505];
 12 struct node
 13 {
 14     int x,y;
 15     int peln,path;  //惩罚/ 第几层
 16     int s[55];     //遇到的陷阱,26开始就是A
 17 };
 18 stack<node> que;
 19 deque<node> anslist;
 20
 21 int stax, stay;
 22 int tarx, tary;
 23 int n, m;
 24 int fat, mins;
 25
 26 void init(node &tmp)        //初始化怪物标记s
 27 {
 28     for(int i=0; i<55; i++)
 29         tmp.s[i]='0';
 30 }
 31 bool tar(node tmp)      //判断是否是目的地
 32 {
 33     if( tmp.x==tarx && tmp.y==tary )
 34         return true;
 35     else
 36         return false;
 37 }
 38 bool cango(node tmp)      //判断能不能走
 39 {
 40     if(tmp.x>=0&&tmp.x<n&&tmp.y>=0&&tmp.y<m )
 41     {
 42         if( dun[tmp.x][tmp.y]=='#' )
 43             return false;
 44         else
 45             return true;
 46     }
 47     else
 48         return false;
 49 }
 50 void adj_mon(node &tmp)   //计算怪物惩罚
 51 {
 52     char c1='0',c2='0',c3='0',c4='0', c5='0';       //走过可能干掉3只怪物
 53
 54     if( isupper( dun[tmp.x][tmp.y] )  )  //是怪物
 55     {
 56         c1=dun[tmp.x][tmp.y];
 57     }
 58
 59     node node1=tmp;
 60     node1.x-=1;
 61     if( cango(node1)&&isupper( dun[tmp.x-1][tmp.y] )  )  //是怪物
 62     {
 63         c2=dun[tmp.x-1][tmp.y];
 64         //cout<<"begin"<<endl;
 65     }
 66
 67     node1=tmp;
 68     node1.x+=1;
 69     if( cango(node1)&&isupper( dun[tmp.x+1][tmp.y] )  )  //是怪物
 70     {
 71         c3=dun[tmp.x+1][tmp.y];
 72         //cout<<"begin"<<endl;
 73     }
 74
 75     node1=tmp;
 76     node1.y+=1;
 77     if( cango(node1)&&isupper( dun[tmp.x][tmp.y+1] )  )  //是怪物
 78     {
 79         c3=dun[tmp.x][tmp.y+1];
 80         //cout<<"begin"<<endl;
 81     }
 82
 83     node1=tmp;
 84     node1.y-=1;
 85     if( cango(node1)&&isupper( dun[tmp.x][tmp.y-1] )  )  //是怪物
 86     {
 87         //cout<<"begin"<<endl;
 88         c5=dun[tmp.x][tmp.y-1];
 89     }
 90
 91 //*************************************
 92     if(c1!='0'&&tmp.s[ c1-'A'+26 ]=='0')
 93     {
 94         tmp.s[ c1-'A'+26 ]=true;
 95         tmp.peln+=( c1-'A'+1 );
 96         //cout<<"begin"<<endl;
 97     }
 98
 99     if(c2!='0'&&tmp.s[ c2-'A'+26 ]=='0')
100     {
101         tmp.s[ c2-'A'+26 ]=true;
102         tmp.peln+=( c2-'A'+1 );
103         //cout<<"begin"<<endl;
104     }
105
106     if(c3!='0'&&tmp.s[ c3-'A'+26 ]=='0')
107     {
108         tmp.s[ c3-'A'+26 ]=true;
109         tmp.peln+=(c3-'A'+1);
110         //cout<<"begin"<<endl;
111     }
112
113     if(c4!='0'&&tmp.s[ c4-'A'+26 ]=='0')
114     {
115         tmp.s[ c4-'A'+26 ]=true;
116         tmp.peln+=(c4-'A'+1);
117         //cout<<"begin"<<endl;
118     }
119
120     if(c5!='0'&&tmp.s[ c5-'A'+26 ]=='0')
121     {
122         tmp.s[ c5-'A'+26 ]=true;
123         tmp.peln+= c5-'A'+1;
124         //cout<<"begin"<<endl;
125     }
126
127
128 }
129 void trap(node &tmp)        //计算陷阱惩罚
130 {
131     if( islower(dun[tmp.x][tmp.y]) )   //陷阱
132         tmp.peln += (dun[tmp.x][tmp.y] - 'a'+1);
133 }
134 void cal_pel(node &tmp)      //计算惩罚
135 {
136     trap(tmp);
137     adj_mon(tmp);
138 }
139
140 int bfs(node tmp)
141 {
142     //判断四周有没有能进盏的
143     node node1=tmp;
144     node1.x-=1;
145     if(cango(node1))    //合法
146     {
147
148         cal_pel(node1);
149         node1.path++;
150         if(tar(node1))
151             anslist.push_back(node1);
152         else if(vis[node1.x][node1.y]==false)                           //非目的
153         {
154             que.push(node1);
155             vis[node1.x][node1.y]=true;
156             bfs(node1);
157             que.pop();
158             vis[node1.x][node1.y]=false;
159
160         }
161
162     }
163
164     node1=tmp;
165     node1.x+=1;
166     if(cango(node1))    //合法
167     {
168         cal_pel(node1); node1.path++;
169         if(tar(node1))
170             anslist.push_back(node1);
171         else if(vis[node1.x][node1.y]==false)                 //非目的
172         {
173             que.push(node1);
174             vis[node1.x][node1.y]=true;
175             bfs(node1) ;
176             que.pop();
177             vis[node1.x][node1.y]=false;
178         }
179     }
180
181     node1=tmp;
182     node1.y-=1;
183     if(cango(node1))    //合法
184     {
185         cal_pel(node1); node1.path++;
186         if(tar(node1))
187             anslist.push_back(node1);
188         else if(vis[node1.x][node1.y]==false)                 //非目的
189         {
190             que.push(node1);
191             vis[node1.x][node1.y]=true;
192             bfs(node1) ;
193             que.pop();
194             vis[node1.x][node1.y]=false;
195         }
196     }
197
198     node1=tmp;
199     node1.y+=1;
200     if(cango(node1))    //合法
201     {
202         cal_pel(node1); node1.path++;
203         if(tar(node1))
204             anslist.push_back(node1);
205         else if(vis[node1.x][node1.y]==false)               //非目的
206         {
207             que.push(node1);
208             vis[node1.x][node1.y]=true;
209             bfs(node1) ;
210             que.pop();
211             vis[node1.x][node1.y]=false;
212         }
213
214     }
215     return 0;
216 }
217
218 int main()
219 {
220     //freopen("input.txt", "r", stdin);
221     int t;
222     cin>>t;
223     while(t--)
224     {
225         anslist.clear();
226         memset(dun, 0, sizeof(dun));
227         memset(vis, 0, sizeof(vis));
228
229         cin>>n>>m;
230         cin>>stax>>stay>>tarx>>tary;
231         stax-=1;stay-=1;tarx-=1;tary-=1;
232
233
234         for(int i=0; i<n; i++)
235             for(int j=0; j<m; j++)
236                 cin>>dun[i][j];
237
238
239         node tmp;
240         init(tmp);
241         tmp.x=stax; tmp.y=stay; tmp.peln=0; tmp.path=0; //起点
242         vis[tmp.x][tmp.y]=true;
243         que.push(tmp); //先进队
244         bfs(que.top());
245
246         deque<node>::iterator it=anslist.begin();
247         int min_path=1247483647;
248         int min_pel=1247483647;
249         for( ; it!=anslist.end(); it++)
250         {
251             //cout<<it->peln<<" "<<it->path<<endl;
252
253             if(it->peln<=min_pel&&it->path<=min_path)
254             {
255                 min_pel=it->peln;
256                 min_path=it->path;
257             }
258
259
260         }
261         cout<<min_pel<<" "<<min_path<<endl;
262
263
264
265
266     }
267
268
269     return 0;
270 }

递归式的,seg错

转载于:https://www.cnblogs.com/xcw0754/p/4456353.html

The 12th Zhejiang Provincial Collegiate Programming Contest - I Earthstone Keeper浙江省赛相关推荐

  1. ZOJ 3872 Beauty of Array (The 12th Zhejiang Provincial Collegiate Programming Contest )

    [题目链接]click here~~ [题目大意]定义Beauty数是一个序列里所有不相同的数的和,求一个序列所有字序列的Beauty和 1 <= N <= 100000 [解题思路]由于 ...

  2. 2019 浙江省赛部分题解(The 16th Zhejiang Provincial Collegiate Programming Contest Sponsored by TuSimple)

    签到题 GLucky 7 in the Pocket Time Limit: 1 Second      Memory Limit: 65536 KB BaoBao loves number 7 bu ...

  3. The 16th Zhejiang Provincial Collegiate Programming Contest Sponsored by TuSimple

    Problem A Vertices in the Pocket 比赛地址:http://acm.zju.edu.cn/onlinejudge/showContestProblem.do?proble ...

  4. 【ZJCPC2018 第15届 浙江省赛】The 15th Zhejiang Provincial Collegiate Programming Contest(MABLJK 6题)

    补题地址:https://zoj.pintia.cn/home/news 搜索15th 本文按照通过率补的题 M. Lucky 7 题意:如果存在从给出的长为n的序列中选择一个数+b 可以被7整除,就 ...

  5. 【ZJCPC2019 第16届 浙江省赛】The 16th Zhejiang Provincial Collegiate Programming Contest(GFHIJ 5题)

    补题地址:https://zoj.pintia.cn/home/news 搜索16th 本文按照通过率补的题 G .Lucky 7 in the Pocket 题意:给出T个数,对于每个数,找出一个能 ...

  6. The 10th Zhejiang Provincial Collegiate Programming Contest 蒻菜的水题题解。

    http://acm.zju.edu.cn/onlinejudge/contestInfo.do?contestId=347 今天参加了今年的浙江省赛网络同步赛(?),被虐得很惨... 做了五道水题只 ...

  7. HZNU Training 2 for Zhejiang Provincial Collegiate Programming Contest 2019

    赛后总结: T:今天下午参加了答辩比赛,没有给予队友很大的帮助.远程做题的时候发现队友在H上遇到了挫折,然后我就和她们说我看H吧,她们就开始做了另外两道题.今天一人一道题.最后我们在研究一道dp的时候 ...

  8. HZNU Training 4 for Zhejiang Provincial Collegiate Programming Contest 2019

    今日这场比赛我们准备的题比较全面,二分+数论+最短路+计算几何+dp+思维+签到题等.有较难的防AK题,也有简单的签到题.为大家准备了一份题解和AC代码. A - Meeting with Alien ...

  9. The 19th Zhejiang Provincial Collegiate Programming Contest

    文章目录 [A.JB Loves Math](https://codeforces.com/gym/103687/problem/A) [B.JB Loves Comma](https://codef ...

最新文章

  1. 干货! AI 推断解决方案栈 Vitis AI 全流程独家解析
  2. REST中的PUT与POST
  3. python线程池及其原理和使用
  4. centos7 系统缓存清理
  5. 12.通过HTTP API对InfluxDB数据库作操作
  6. mysql union as 注入_sql注入入门 之 mysql 常规注入 [ union方式 ]
  7. 去年微软颁发1360万美元奖励,中国提交的漏洞报告数量位列前三强
  8. template 不能分别在.h和.cpp中定义模板
  9. YOLO在服务器上训练,日志文件中不显示GFLOPs参数的问题
  10. 《科学》:基因编辑婴儿入选年度“科学崩坏”事件
  11. python能开发小程序吗_微信小程序用什么语言开发?Python能开发小程序么
  12. ISIS-P2P网络的LSDB更新
  13. PhyGeoNet一种可用于不规则区域的物理信息极限学习机
  14. 基于java医院门诊管理系统设计
  15. CSS (Cascading Style Sheets)
  16. Python+Streamlit aggrid+MongoDB GridFS构建低代码文档管理应用(文档查询下载实用篇)
  17. 如何安装CocoaPods
  18. 阿里大鱼使用详细步骤-Java
  19. 流形学习(manifold learning)综述
  20. ES6飞机大战篇-敌机自动移动发射子弹

热门文章

  1. LocalDB连接失败
  2. vs2013新建web项目异常 0x80070002 处理
  3. POJ 3104 Drying 二分
  4. silverlight textblock 自动换行
  5. usaco1.2.2 transform
  6. listView动态加载数据分页
  7. 海思Hi3519A 进行4k60 h264编码帧率不足的问题
  8. 实用技巧:Linux操作系统Vim/Vi编程提速
  9. python异常处理结构_python-异常处理
  10. LD(Levenshtein distance)莱文斯坦距离----编辑距离