[Gym-100625J] 搜索
题目链接:https://cn.vjudge.net/problem/Gym-100625J
具体思路:首先,具体思路是两个人一起走到一个点,然后两个人按照同样的道路走出去,听了别人的思路,还有一种特殊情况需要考虑,就是中间一堵墙,两个人都直接在边界上,这个时候可以新加一个点,然后两个人到这个点的开门数都是0,然后三个搜索,新加的点到图里面所有能走的点的最小路径,两个人分别到达图内的点和新加的点的最小开门数,然后枚举每一个点,找最小就可以了。
AC代码:
#include<iostream>
#include<stack>
#include<iomanip>
#include<queue>
#include<iomanip>
#include<cmath>
#include<stdio.h>
#include<map>
#include<string>
#include<cstring>
#include<set>
#include<vector>
using namespace std;
# define inf 100000
# define ll long long
# define maxn 100+100
int dis[maxn][maxn];// 计算从外面的点到里面每个点的最小开门数
int num1[maxn][maxn];// 计算第一个人到达某一个点的最小开门数
int num2[maxn][maxn];//计算第二个人到达某一个点的最小开门数
int vis[maxn][maxn];//dfs的过程中标记
char Map[maxn][maxn];
int n,m,k;
int f[2][4]= {{1,-1,0,0},{0,0,1,-1}};
bool judge(int t1,int t2)
{
if(t1>=0&&t2>=0&&t1<=n+1&&t2<=m+1)return true;
return false;
}
struct node
{
int x,y,num;
node(int xx,int yy,int zz)
{
x=xx;
y=yy;
num=zz;
}
friend bool operator < (node t1,node t2)
{
return t2.num<t1.num;//把想要的放在后面,符号方向不变
}
};
void bfs1(int x,int y)
{
priority_queue<node>q;
q.push(node(x,y,0));
vis[x][y]=1;
dis[x][y]=0;
while(!q.empty())
{
node temp=q.top();
q.pop();
for(int i=0; i<4; i++)
{
int t1=temp.x+f[0][i];
int t2=temp.y+f[1][i];
if(judge(t1,t2)&&vis[t1][t2]==0&&Map[t1][t2]!='*')
{
vis[t1][t2]=1;
if(Map[t1][t2]=='#')
{
q.push(node(t1,t2,temp.num+1));
dis[t1][t2]=dis[temp.x][temp.y]+1;
}
else
{
q.push(node(t1,t2,temp.num));
dis[t1][t2]=dis[temp.x][temp.y];
}
}
}
}
}
void bfs2(int x,int y)
{
priority_queue<node>q;
q.push(node(x,y,0));
vis[x][y]=1;
num1[x][y]=0;
while(!q.empty())
{
node temp=q.top();
q.pop();
for(int i=0; i<4; i++)
{
int t1=temp.x+f[0][i];
int t2=temp.y+f[1][i];
if(judge(t1,t2)&&vis[t1][t2]==0&&Map[t1][t2]!='*')
{
vis[t1][t2]=1;
if(Map[t1][t2]=='#')
{
q.push(node(t1,t2,temp.num+1));
num1[t1][t2]=num1[temp.x][temp.y]+1;
}
else
{
q.push(node(t1,t2,temp.num));
num1[t1][t2]=num1[temp.x][temp.y];
}
}
}
}
}
void bfs3(int x,int y)
{
priority_queue<node>q;
q.push(node(x,y,0));
vis[x][y]=1;
num2[x][y]=0;
while(!q.empty())
{
node temp=q.top();
q.pop();
for(int i=0; i<4; i++)
{
int t1=temp.x+f[0][i];
int t2=temp.y+f[1][i];
if(judge(t1,t2)&&vis[t1][t2]==0&&Map[t1][t2]!='*')
{
vis[t1][t2]=1;
if(Map[t1][t2]=='#')
{
q.push(node(t1,t2,temp.num+1));
num2[t1][t2]=num2[temp.x][temp.y]+1;
}
else
{
q.push(node(t1,t2,temp.num));
num2[t1][t2]=num2[temp.x][temp.y];
}
}
}
}
}
int main()
{
int T;
scanf("%d",&T);
int x1,y1,x2,y2;
while(T--)
{
int flag=1;
scanf("%d%d",&n,&m);
for(int i=1; i<=n; i++)
{
scanf("%s",Map[i]+1);
}
for(int i=0; i<=m+1; i++)
{
Map[0][i]='.';
Map[n+1][i]='.';
}
for(int i=0; i<=n+1; i++)
{
Map[i][0]='.';
Map[i][m+1]='.';
}
for(int i=1; i<=n; i++)
{
for(int j=1; j<=m; j++)
{
if(Map[i][j]!='$')continue;
if(flag==1)
{
x1=i;
y1=j;
flag=0;
}
else if(flag==0)
{
x2=i;
y2=j;
break;
}
}
}
for(int i=0; i<=n+1; i++)
for(int j=0; j<m+1; j++)
{
dis[i][j]=inf;
num1[i][j]=inf;
num2[i][j]=inf;
}
memset(vis,0,sizeof(vis));
bfs1(0,0);
memset(vis,0,sizeof(vis));
bfs2(x1,y1);
memset(vis,0,sizeof(vis));
bfs3(x2,y2);
int minn=inf;
minn=min(minn,dis[0][0]+num1[0][0]+num2[0][0]);
for(int i=1; i<=n; i++)
{
for(int j=1; j<=m; j++)
{
if(Map[i][j]=='#')
minn=min(minn,dis[i][j]+num1[i][j]+num2[i][j]-2);
else
minn=min(minn,dis[i][j]+num1[i][j]+num2[i][j]);
}
}
printf("%d\n",minn);
}
return 0;
}
/*
5 9
****#****
*..#.#..*
****.****
*$#.#.#$*
*********
5 11
*#*********
*$*...*...*
*$*.*.*.*.*
*...*...*.*
*********.*
9 9
*#**#**#*
*#**#**#*
*#**#**#*
*#**.**#*
*#*#.#*#*
*$##*##$*
*.#.#.#.*
*********
*/
[Gym-100625J] 搜索的更多相关文章
- Gym - 100625J Jailbreak 最短路+搜索
http://codeforces.com/gym/100625/attachments/download/3213/2013-benelux-algorithm-programming-contes ...
- 暑假集训-WHUST 2015 Summer Contest #0.2
ID Origin Title 10 / 55 Problem A Gym 100625A Administrative Difficulties 4 / 6 Problem B Gym 1006 ...
- Codeforces Gym 100231G Voracious Steve 记忆化搜索
Voracious Steve 题目连接: http://codeforces.com/gym/100231/attachments Description 有两个人在玩一个游戏 有一个盆子里面有n个 ...
- Codeforces Gym 100431B Binary Search 搜索+组合数学+高精度
原题链接:http://codeforces.com/gym/100431/attachments/download/2421/20092010-winter-petrozavodsk-camp-an ...
- ACM: Gym 100935G Board Game - DFS暴力搜索
Board Game Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Gym 100 ...
- Codeforces Gym 101142C:CodeCoder vs TopForces(搜索)
http://codeforces.com/gym/101142/attachments 题意:每个人在TC和CF上分别有两个排名,如果有一个人在任意一个网站上大于另一个人的排名,那么这个人可以打败另 ...
- Codeforces Gym 100231F Solitaire 折半搜索
Solitaire 题目连接: http://codeforces.com/gym/100231/ Description 给你一个8*8棋盘,里面有4个棋子,每个棋子可以做一下某个操作之一: 1.走 ...
- GYM 100608G 记忆化搜索+概率 2014-2015 Winter Petrozavodsk Camp, Andrew Stankevich Contest 47 (ASC 47)
https://codeforces.com/gym/100608 题意: 两个人玩游戏,每个人有一个长为d的b进制数字,两个人轮流摇一个$[0,b-1]$的骰子,并将选出的数字填入自己的d个空位之中 ...
- Codeforces Gym 191033 E. Explosion Exploit (记忆化搜索+状压)
E. Explosion Exploit time limit per test 2.0 s memory limit per test 256 MB input standard input out ...
- 【每日dp】 Gym - 101889E Enigma 数位dp 记忆化搜索
题意:给你一个长度为1000的串以及一个数n 让你将串中的‘?’填上数字 使得该串是n的倍数而且最小(没有前导零) 题解:dp,令dp[len][mod]为是否出现过 填到第len位,余数为mod 的 ...
随机推荐
- CentOS安装crontab及使用方法(转)
CentOS安装crontab及使用方法(转) 安装crontab:[root@CentOS ~]# yum install vixie-cron[root@CentOS ~]# yum ins ...
- 【Linux】- cat命令的源码历史
转自:Cat 命令的源码历史 以前我和我的一些亲戚争论过计算机科学的学位值不值得读.当时我正在上大学,并要决定是不是该主修计算机.我姨和我表姐觉得我不应该主修计算机.她们承认知道如何编程肯定是很有用且 ...
- 剖析Vue原理&实现双向绑定MVVM-1
本文能帮你做什么?1.了解vue的双向数据绑定原理以及核心代码模块2.缓解好奇心的同时了解如何实现双向绑定为了便于说明原理与实现,本文相关代码主要摘自vue源码, 并进行了简化改造,相对较简陋,并未考 ...
- java和mysql的length()区别及char_length()
一. mysql里面的有length和char_length两个长度函数,区别在于: length: 一个汉字是算三个字符,一个数字或字母算一个字符. char_length: 不管汉字还是数字或者是 ...
- mysql(五)查询缓存
mysql的逻辑架构图如下: 当开启查询缓存时,mysql会将查询结果缓存到查询缓存区域,结果对应的key是使用查询语句,数据库名称,客户端协议的版本等因素算出的一个hash值. 在下次查询时,根据一 ...
- 【刷题】洛谷 P2675 《瞿葩的数字游戏》T3-三角圣地
题目背景 国王1带大家到了数字王国的中心:三角圣地. 题目描述 不是说三角形是最稳定的图形嘛,数字王国的中心便是由一个倒三角构成.这个倒三角的顶端有一排数字,分别是1 ~ N.1 ~ N可以交换位置. ...
- 【省选水题集Day1】一起来AK水题吧! 题解(更新到B)
题目:http://www.cnblogs.com/ljc20020730/p/6937936.html 水题A:[AHOI2001]质数和分解 安徽省选OI原题!简单Dp. 一看就是完全背包求方案数 ...
- BZOJ2733 永无乡 【splay启发式合并】
2733: [HNOI2012]永无乡 Time Limit: 10 Sec Memory Limit: 128 MB Submit: 4190 Solved: 2226 [Submit][Sta ...
- POJ.2251 Dungeon Master (三维BFS)
POJ.2251 Dungeon Master (三维BFS) 题意分析 你被困在一个3D地牢中且继续寻找最短路径逃生.地牢由立方体单位构成,立方体中不定会充满岩石.向上下前后左右移动一个单位需要一分 ...
- 浴谷金秋线上集训营 T11738 伪神(树链剖分)
先树链剖分,一棵子树的编号在数组上连续,一条链用树链剖分,把这些线段全部取出来,做差分,找到有多少点被>=t条线段覆盖即可. #include<iostream> #include& ...