openjudge 拯救公主
点击打开题目
看到这道题,第一感觉是我有一句 m2p m^2p不知当讲不当讲
传送门就算了,你提莫还来宝石,还不给我每种最多有几个~~
在一般的迷宫问题里,无论已经走了多少步,只要到达同一个点,状态便是等价的,但在这道题中,当持有不同种类宝石到达同一个地方,可能对结果会有影响,那就在BFS的地图中多开一维,来存宝石的状态
200·200的地图,DFS就算了,但BFS宝石的状态怎么办?
细看一下,宝石只有五种,凑齐k种不计个数,就可救出公主
用二进制五位,即十进制32即可解决,二进制中的每一位表示一种宝石是否获得
代码实现,就看自己的修行了,反正我写完时,第一感觉是我有一句 m2p m^2p现在就要讲
代码如下:
#include<cstdio>
#include<cstring>
#include<cmath>
#include<queue>
#include<algorithm>
using namespace std;
struct node{int x,y,dmt,step;};
queue<node>Q;
int m,n,k;
bool v[201][201][32];
int map[201][201],u[4]={-1,1,0,0},z[4]={0,0,-1,1};
int d[11][2],dcnt,xz,yz;
int getint()
{int num=0,flag=1;char c;while((c=getchar())<'0'||c>'9')if(c=='-')flag=-1;while(c>='0'&&c<='9')num=num*10+c-48,c=getchar();return num*flag;
}
bool check1(int s,int x)
{int i;while(s&&++i){if(s&1&&i==x)return 1;s/=2;}return 0;
}
bool check2(int s)
{int z=0;while(s)z+=(s&1),s/=2;if(z>=k)return 1;return 0;
}
int main()
{int tt=getint(),i,j;char c;while(tt--){memset(v,0,sizeof v);memset(map,0,sizeof map);memset(d,0,sizeof d);dcnt=0;while(!Q.empty())Q.pop();m=getint(),n=getint(),k=getint();for(i=1;i<=m;i++)for(j=1;j<=n;j++){c=getchar();if(c=='\n'){j--;continue;}if(c=='#')map[i][j]=-1;if(c>='0'&&c<='9')map[i][j]=c-47;if(c=='$')d[++dcnt][0]=i,d[dcnt][1]=j,map[i][j]=6;if(c=='S'){node t;t.x=i,t.y=j,t.dmt=0,t.step=0;Q.push(t);v[i][j][0]=1;}if(c=='E')xz=i,yz=j;}while(!Q.empty()){node t=Q.front();Q.pop();for(i=0;i<4;i++)if(map[t.x+u[i]][t.y+z[i]]!=-1&&t.x+u[i]>0&&t.x+u[i]<=m&&t.y+z[i]>0&&t.y+z[i]<=n){node w;w.x=t.x+u[i],w.y=t.y+z[i],w.dmt=t.dmt,w.step=t.step+1;if(map[w.x][w.y]==6)for(j=1;j<=dcnt;j++)if(!v[d[j][0]][d[j][1]][w.dmt]){node hh;hh.x=d[j][0],hh.y=d[j][1],hh.dmt=w.dmt,hh.step=w.step;Q.push(hh);}if(!map[w.x][w.y]&&!v[w.x][w.y][w.dmt])v[w.x][w.y][w.dmt]=1,Q.push(w);if(map[w.x][w.y]>0&&map[w.x][w.y]<6){if(!check1(w.dmt,map[w.x][w.y]))w.dmt+=int(pow(2*1.0,map[w.x][w.y]-1));if(!v[w.x][w.y][w.dmt]){v[w.x][w.y][w.dmt]=1;Q.push(w);}}if(w.x==xz&&w.y==yz)if(check2(w.dmt)){printf("%d\n",w.step);goto hehe;}}}printf("oop!\n");hehe:;}
}
openjudge 拯救公主相关推荐
- OpenJudge/Poj 2001 Shortest Prefixes
1.链接地址: http://bailian.openjudge.cn/practice/2001 http://poj.org/problem?id=2001 2.题目: Shortest Pref ...
- OpenJudge/Poj 2027 No Brainer
1.链接地址: http://bailian.openjudge.cn/practice/2027 http://poj.org/problem?id=2027 2.题目: 总Time Limit: ...
- [OpenJudge] 百练2754 八皇后
八皇后 Description 会下国际象棋的人都很清楚:皇后可以在横.竖.斜线上不限步数地吃掉其他棋子.如何将8个皇后放在棋盘上(有8 * 8个方格),使它们谁也不能被吃掉!这就是著名的八皇后问题. ...
- OpenJudge 2739 计算对数
1.链接地址: http://bailian.openjudge.cn/practice/2739/ 2.题目: 总时间限制: 1000ms 内存限制: 65536kB 描述 给定两个正整数a和b.可 ...
- OpenJudge 2757 最长上升子序列 / Poj 2533 Longest Ordered Subsequence
1.链接地址: http://poj.org/problem?id=2533 http://bailian.openjudge.cn/practice/2757 2.题目: 总Time Limit: ...
- OpenJudge/Poj 1163 The Triangle
1.链接地址: http://bailian.openjudge.cn/practice/1163 http://poj.org/problem?id=1163 2.题目: 总时间限制: 1000ms ...
- c语言 3个人比饭量大小,OpenJudge计算概论-比饭量【枚举法、信息数字化】
/*====================================================================== 比饭量 总时间限制: 1000ms 内存限制: 655 ...
- OpenJudge 2796: 数字求和
2019独角兽企业重金招聘Python工程师标准>>> 总时间限制: 3000ms 内存限制: 65536kB 描述 给定一个正整数a,以及另外的5个正整数,问题是:这5个整数中,小 ...
- OpenJudge/Poj 1915 Knight Moves
1.链接地址: http://bailian.openjudge.cn/practice/1915 http://poj.org/problem?id=1915 2.题目: 总Time Limit: ...
最新文章
- Eclipse下配置struts很让我痛苦
- 一道经典的JavaScript面试题
- [转载] --- 让线程按顺序执行8种方法
- python的输入来源包括网络输入法_python可以调用计算机上的输入法进行输入吗?比如调用输入法在其他程序的输入框中写上字符串...
- CSS修改iframe内的样式问题
- 查找字符串中元素出现的次数
- Java面试准备(一)
- 智能手机玩转Smart3D三维建模介绍
- 读入外挂:ios::sync_with_stdio(false)
- 读《好好学习:个人知识管理精进指南》
- C/C++ isalpha、isalnum、islower、isupper函数详解
- linux命令 执行间隙,linux 定时执行任务 at atq atrm命令的使用
- dos命令之 assoc 用法详解
- 制作可执行的JAR文件包及jar命令详解
- 计算方法 差商与牛顿插值
- STM32中的RCC是什么意思
- 第一章 人机交互概述
- 信号与系统学习笔记 第三章
- Memory 基础知识介绍
- 基于java的springboot在线外卖点餐系统毕业设计springboot开题报告