题目: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. swift中的类拓展 extension

    以添加颜色为例,new 一个swift文件夹 不是cocoa类 也不需要继承什么. 然后 import UIKit protocol ColorDalegate{ class func mainCol ...

  2. Python:列表也能拆包?

    前几天,微信学习群里有个小伙伴在看书时遇到了这样一个问题,在群里提问,看下图: 这是常用的 matplotlib 库,只是一般我们调用 plot 方法绘图时,不会去关心它的返回值.然而 plt1, = ...

  3. JS的几条规则

    总结一下,有这么几条规则需要遵守: 不要使用new Number().new Boolean().new String()创建包装对象: 用parseInt()或parseFloat()来转换任意类型 ...

  4. IOS UIWebView与js的简单交互swift3版

    在开发过程中,我们可能遇到ios代码与js交互的情况,本人第一次使用遇到了很多坑,这里纪录一下,方便自己,也方便需要的人. 1.第一步先建一个接口(协议)并继承JSExport 这里实现两个方法提供给 ...

  5. plpython 中文分词Windows下 PG数据库jieba分词

    windows 下安装版本匹配python-3.4.3.amd64.msipostgresql-10.1-2-windows-x64.exe create language plpython3u;se ...

  6. JMeter - 如何创建可重用和模块化测试脚本

    概述: 我的应用程序几乎没有业务关键流程,我们可以从中提出不同的业务工作流程.当我试图在JMeter中提出性能测试脚本时,我需要找到一些方法来创建可重用/模块化的测试脚本.这样我就可以创建不同的工作流 ...

  7. element input搜索框探索

    转(https://blog.csdn.net/qq_37746973/article/details/78402812) 在script中添加下面两个函数 //queryString 为在框中输入的 ...

  8. 023 Merge k Sorted Lists 合并K个有序链表

    合并K个有序链表,并且作为一个有序链表的形式返回.分析并描述它的复杂度. 详见:https://leetcode.com/problems/merge-k-sorted-lists/descripti ...

  9. (转)Linux最常用指令及快捷键 Linux学习笔记

    Linux最常用指令及快捷键 Linux学习笔记 原文:http://blog.csdn.net/yanghongche/article/details/50827478 [摘自 鸟叔的私房菜]--转 ...

  10. 还不知道如何使用 IDEA ?教你三招快速掌握 IDEA

    前言 IntelliJ IDEA 是一个非常强大的 IDE,拥有许多功能.在 IDEA 中大部分功能都可以用快捷键去完成,如果掌握了大部分快捷键,可以只使用键盘开发了. ps: 最近正在练习快捷键,准 ...