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

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

 #include <iostream>
#include <cstdio>
#include <string>
#include <map>
#include <deque>
#include <stack>
#include <cstring>
#include <algorithm>
using namespace std;
char dun[][];
bool vis[][];
struct node
{
int x,y;
int peln,path; //惩罚/ 第几层
int s[]; //遇到的陷阱,26开始就是A
};
stack<node> que;
deque<node> anslist; int stax, stay;
int tarx, tary;
int n, m;
int fat, mins; void init(node &tmp) //初始化怪物标记s
{
for(int i=; i<; i++)
tmp.s[i]='';
}
bool tar(node tmp) //判断是否是目的地
{
if( tmp.x==tarx && tmp.y==tary )
return true;
else
return false;
}
bool cango(node tmp) //判断能不能走
{
if(tmp.x>=&&tmp.x<n&&tmp.y>=&&tmp.y<m )
{
if( dun[tmp.x][tmp.y]=='#' )
return false;
else
return true;
}
else
return false;
}
void adj_mon(node &tmp) //计算怪物惩罚
{
char c1='',c2='',c3='',c4='', c5=''; //走过可能干掉3只怪物 if( isupper( dun[tmp.x][tmp.y] ) ) //是怪物
{
c1=dun[tmp.x][tmp.y];
} node node1=tmp;
node1.x-=;
if( cango(node1)&&isupper( dun[tmp.x-][tmp.y] ) ) //是怪物
{
c2=dun[tmp.x-][tmp.y];
//cout<<"begin"<<endl;
} node1=tmp;
node1.x+=;
if( cango(node1)&&isupper( dun[tmp.x+][tmp.y] ) ) //是怪物
{
c3=dun[tmp.x+][tmp.y];
//cout<<"begin"<<endl;
} node1=tmp;
node1.y+=;
if( cango(node1)&&isupper( dun[tmp.x][tmp.y+] ) ) //是怪物
{
c3=dun[tmp.x][tmp.y+];
//cout<<"begin"<<endl;
} node1=tmp;
node1.y-=;
if( cango(node1)&&isupper( dun[tmp.x][tmp.y-] ) ) //是怪物
{
//cout<<"begin"<<endl;
c5=dun[tmp.x][tmp.y-];
} //*************************************
if(c1!=''&&tmp.s[ c1-'A'+ ]=='')
{
tmp.s[ c1-'A'+ ]=true;
tmp.peln+=( c1-'A'+ );
//cout<<"begin"<<endl;
} if(c2!=''&&tmp.s[ c2-'A'+ ]=='')
{
tmp.s[ c2-'A'+ ]=true;
tmp.peln+=( c2-'A'+ );
//cout<<"begin"<<endl;
} if(c3!=''&&tmp.s[ c3-'A'+ ]=='')
{
tmp.s[ c3-'A'+ ]=true;
tmp.peln+=(c3-'A'+);
//cout<<"begin"<<endl;
} if(c4!=''&&tmp.s[ c4-'A'+ ]=='')
{
tmp.s[ c4-'A'+ ]=true;
tmp.peln+=(c4-'A'+);
//cout<<"begin"<<endl;
} if(c5!=''&&tmp.s[ c5-'A'+ ]=='')
{
tmp.s[ c5-'A'+ ]=true;
tmp.peln+= c5-'A'+;
//cout<<"begin"<<endl;
} }
void trap(node &tmp) //计算陷阱惩罚
{
if( islower(dun[tmp.x][tmp.y]) ) //陷阱
tmp.peln += (dun[tmp.x][tmp.y] - 'a'+);
}
void cal_pel(node &tmp) //计算惩罚
{
trap(tmp);
adj_mon(tmp);
} int bfs(node tmp)
{
//判断四周有没有能进盏的
node node1=tmp;
node1.x-=;
if(cango(node1)) //合法
{ cal_pel(node1);
node1.path++;
if(tar(node1))
anslist.push_back(node1);
else if(vis[node1.x][node1.y]==false) //非目的
{
que.push(node1);
vis[node1.x][node1.y]=true;
bfs(node1);
que.pop();
vis[node1.x][node1.y]=false; } } node1=tmp;
node1.x+=;
if(cango(node1)) //合法
{
cal_pel(node1); node1.path++;
if(tar(node1))
anslist.push_back(node1);
else if(vis[node1.x][node1.y]==false) //非目的
{
que.push(node1);
vis[node1.x][node1.y]=true;
bfs(node1) ;
que.pop();
vis[node1.x][node1.y]=false;
}
} node1=tmp;
node1.y-=;
if(cango(node1)) //合法
{
cal_pel(node1); node1.path++;
if(tar(node1))
anslist.push_back(node1);
else if(vis[node1.x][node1.y]==false) //非目的
{
que.push(node1);
vis[node1.x][node1.y]=true;
bfs(node1) ;
que.pop();
vis[node1.x][node1.y]=false;
}
} node1=tmp;
node1.y+=;
if(cango(node1)) //合法
{
cal_pel(node1); node1.path++;
if(tar(node1))
anslist.push_back(node1);
else if(vis[node1.x][node1.y]==false) //非目的
{
que.push(node1);
vis[node1.x][node1.y]=true;
bfs(node1) ;
que.pop();
vis[node1.x][node1.y]=false;
} }
return ;
} int main()
{
//freopen("input.txt", "r", stdin);
int t;
cin>>t;
while(t--)
{
anslist.clear();
memset(dun, , sizeof(dun));
memset(vis, , sizeof(vis)); cin>>n>>m;
cin>>stax>>stay>>tarx>>tary;
stax-=;stay-=;tarx-=;tary-=; for(int i=; i<n; i++)
for(int j=; j<m; j++)
cin>>dun[i][j]; node tmp;
init(tmp);
tmp.x=stax; tmp.y=stay; tmp.peln=; tmp.path=; //起点
vis[tmp.x][tmp.y]=true;
que.push(tmp); //先进队
bfs(que.top()); deque<node>::iterator it=anslist.begin();
int min_path=;
int min_pel=;
for( ; it!=anslist.end(); it++)
{
//cout<<it->peln<<" "<<it->path<<endl; if(it->peln<=min_pel&&it->path<=min_path)
{
min_pel=it->peln;
min_path=it->path;
} }
cout<<min_pel<<" "<<min_path<<endl; } return ;
}

递归式的,seg错

The 12th Zhejiang Provincial Collegiate Programming Contest - I Earthstone Keeper浙江省赛的更多相关文章

  1. zoj The 12th Zhejiang Provincial Collegiate Programming Contest Capture the Flag

    http://acm.zju.edu.cn/onlinejudge/showContestProblem.do?problemId=5503 The 12th Zhejiang Provincial ...

  2. zoj The 12th Zhejiang Provincial Collegiate Programming Contest Team Formation

    http://acm.zju.edu.cn/onlinejudge/showContestProblem.do?problemId=5494 The 12th Zhejiang Provincial ...

  3. zoj The 12th Zhejiang Provincial Collegiate Programming Contest Beauty of Array

    http://acm.zju.edu.cn/onlinejudge/showContestProblem.do?problemId=5496 The 12th Zhejiang Provincial ...

  4. zoj The 12th Zhejiang Provincial Collegiate Programming Contest Lunch Time

    http://acm.zju.edu.cn/onlinejudge/showContestProblem.do?problemId=5499 The 12th Zhejiang Provincial ...

  5. zoj The 12th Zhejiang Provincial Collegiate Programming Contest Convert QWERTY to Dvorak

    http://acm.zju.edu.cn/onlinejudge/showContestProblem.do?problemId=5502  The 12th Zhejiang Provincial ...

  6. zoj The 12th Zhejiang Provincial Collegiate Programming Contest May Day Holiday

    http://acm.zju.edu.cn/onlinejudge/showContestProblem.do?problemId=5500 The 12th Zhejiang Provincial ...

  7. zoj The 12th Zhejiang Provincial Collegiate Programming Contest Demacia of the Ancients

    http://acm.zju.edu.cn/onlinejudge/showContestProblem.do?problemId=5504  The 12th Zhejiang Provincial ...

  8. zjuoj The 12th Zhejiang Provincial Collegiate Programming Contest Ace of Aces

    http://acm.zju.edu.cn/onlinejudge/showContestProblem.do?problemId=5493 The 12th Zhejiang Provincial ...

  9. 140 - The 12th Zhejiang Provincial Collegiate Programming Contest(第二部分)

    Floor Function Time Limit: 10 Seconds      Memory Limit: 65536 KB a, b, c and d are all positive int ...

随机推荐

  1. 20169219 linux内核原理与分析第二周作业

    "linux内核分析"的第一讲主要讲了计算机的体系结构,和各寄存器之间对数据的处理过程. 通用寄存器 AX:累加器 BX:基地址寄存器 CX:计数寄存器 DX:数据寄存器 BP:堆 ...

  2. VR中运动控制器的传送系统

    创建一个VRPawn 新建一个BluePrint,父类选择Pawn,我们命名为VRPawn,打开它. 添加一个Scene命名为CameraRoot 在CameraRoot节点下添加一个Camera 在 ...

  3. 浅谈js模块加载方式(初级)

    1.简介:  前端模块化开发日渐鼎盛,如何将零散的插件或者是普通的js脚本文件统一管理及引用,是众多开发者共同的目标.本人是从事.net开发的,最近对前端的一些东西特别的感兴趣,也会尝试的夹杂一点自己 ...

  4. CodeForces 114B 【STL应用】

    思路: 原来string类能sort 和 swap....太强了.... 注意:字典序最小输出,因为某个地方写挫了,sort了n发,代码挫. #include <bits/stdc++.h> ...

  5. PAT 1043【BST与二叉树】

    考察: 1.二叉树的建树 2.前序遍历,后序遍历 3.BST的特性 这题的思路: 告诉你数组是先序遍历的,so 根已经知道了(数组首位元素),那么按照BST,建一下树(要两次,另外一次是镜像的): 跑 ...

  6. PIL基础操作

    PIL基础操作 新建图片 from PIL import Image # 通常使用RGB模式就可以了 new_img = Image.new('RGB', (100, 100), 'red') new ...

  7. CF24D Broken robot 后效性DP

    这题咕了好久..... 设$f[i][j]$表示从$(i,j)$到最后一行的期望步数: 则有 $ f[i][1]=\frac{1}{3}(f[i][1]+f[i][2]+f[i+1][1])+1$ $ ...

  8. Helvetic Coding Contest 2016 online mirror C1

    Description One particularly well-known fact about zombies is that they move and think terribly slow ...

  9. Win7 桌面图标消失

    win7 桌面图标消失或任务栏也消失,可以按Ctrl+Shift+Esc键调出任务管理器,然后点击文件——新建任务,输入explorer.

  10. Java学习笔记--关于面向对象的思考

    1.不可改变的类生成对象以及变量的范围 2. 关键词this的使用 3.用类抽象的思想制作软件 4.通过关系模型建立类 5.使用面向对象的范例来设计程序,遵循类设计指导. 已经学习了:怎么定义类已经创 ...