题目: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. c# 指针unsafe/fixed -- 【一】

    指针C#unsafefixed 目录(?)[-] 概述 unsafe fixed 1.1 概述 unsafe关键字表示不安全上下文,该上下文是任何涉及指针的操作所必需的.可以在属性.方法.类的声明中使 ...

  2. C#中类和结构体

    结构体 类 自己的一些理解 首先结构中不能给字段赋值  而类可以 结构调用方法是  例如 People p1: 类的调用方法是  Book b =new Book(): 1.类能够实例化 而结构不可以 ...

  3. .net core 深入了解配置文件加载过程

    前言     配置文件中程序运行中,担当着不可或缺的角色:通常情况下,使用 visual studio 进行创建项目过程中,项目配置文件会自动生成在项目根目录下,如 appsettings.json, ...

  4. The Largest Generation (25)(BFS)(PAT甲级)

    #include<bits/stdc++.h>using namespace std;int n,m,l,t;int a[1307][137][67];int vis[1307][137] ...

  5. [CodeChef] The Street

    给定两个长度为n的数列A和B,开始数组A中每一项值为0,数组B中每一项值为负无穷大.接下来有m次操作:1.数组A区间加一个等差数列:2.数组B区间对一个等差数列取max:3.询问ai+bi的值.n&l ...

  6. 洛谷P2202 [USACO13JAN]方块重叠Square Overlap

    P2202 [USACO13JAN]方块重叠Square Overlap 题目描述 Farmer John is planning to build N (2 <= N <= 50,000 ...

  7. RRTI的概念以及Class对象作用

    深入理解Class对象 RRTI的概念以及Class对象作用 认识Class对象之前,先来了解一个概念,RTTI(Run-Time Type Identification)运行时类型识别,对于这个词一 ...

  8. IDEA调试方法总结及各种Step的区别

    1.打断点 IDEA 添加断点的方式还是比较简单的,我们可以直接在某一行的代码行号后点击鼠标左键进行添加 2.启动调试 如果我们想要调试我们的程序,那我们必须以DEBUG的形式启动我们的程序,以DEB ...

  9. CentOS编译安装GCC 4.9.2成功

    在Linux上编译安装gcc是个寻烦恼的活,对于像我这样习惯于在Windows上面使用二进制安装包的人来说,自已编译安装gcc是个相当大的挑战,今天直接挑战最新版的gcc,是4.9.2版本的,做之前查 ...

  10. PHP var_export

    var_export可以将一个数组转为一个字符串 不同于var_dump,var_export并不会输出数据的类型以及字符大小等,只会简单把数组的key跟value拼接成一个字符串 <?php ...