题目链接: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] 搜索的更多相关文章

  1. Gym - 100625J Jailbreak 最短路+搜索

    http://codeforces.com/gym/100625/attachments/download/3213/2013-benelux-algorithm-programming-contes ...

  2. 暑假集训-WHUST 2015 Summer Contest #0.2

    ID Origin Title 10 / 55 Problem A Gym 100625A Administrative Difficulties   4 / 6 Problem B Gym 1006 ...

  3. Codeforces Gym 100231G Voracious Steve 记忆化搜索

    Voracious Steve 题目连接: http://codeforces.com/gym/100231/attachments Description 有两个人在玩一个游戏 有一个盆子里面有n个 ...

  4. Codeforces Gym 100431B Binary Search 搜索+组合数学+高精度

    原题链接:http://codeforces.com/gym/100431/attachments/download/2421/20092010-winter-petrozavodsk-camp-an ...

  5. ACM: Gym 100935G Board Game - DFS暴力搜索

    Board Game Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u  Gym 100 ...

  6. Codeforces Gym 101142C:CodeCoder vs TopForces(搜索)

    http://codeforces.com/gym/101142/attachments 题意:每个人在TC和CF上分别有两个排名,如果有一个人在任意一个网站上大于另一个人的排名,那么这个人可以打败另 ...

  7. Codeforces Gym 100231F Solitaire 折半搜索

    Solitaire 题目连接: http://codeforces.com/gym/100231/ Description 给你一个8*8棋盘,里面有4个棋子,每个棋子可以做一下某个操作之一: 1.走 ...

  8. GYM 100608G 记忆化搜索+概率 2014-2015 Winter Petrozavodsk Camp, Andrew Stankevich Contest 47 (ASC 47)

    https://codeforces.com/gym/100608 题意: 两个人玩游戏,每个人有一个长为d的b进制数字,两个人轮流摇一个$[0,b-1]$的骰子,并将选出的数字填入自己的d个空位之中 ...

  9. 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 ...

  10. 【每日dp】 Gym - 101889E Enigma 数位dp 记忆化搜索

    题意:给你一个长度为1000的串以及一个数n 让你将串中的‘?’填上数字 使得该串是n的倍数而且最小(没有前导零) 题解:dp,令dp[len][mod]为是否出现过 填到第len位,余数为mod 的 ...

随机推荐

  1. ajax跨域问题(三种解决方案)

    为什么会出现跨域 跨域问题来源于JavaScript的同源策略,即只有 协议+主机名+端口号 (如存在)相同,则允许相互访问.也就是说JavaScript只能访问和操作自己域下的资源,不能访问和操作其 ...

  2. Vue2.0 render:h => h(App)

    new Vue({ router, store, //components: { App } vue1.0的写法 render: h => h(App) vue2.0的写法 }).$mount( ...

  3. idea 复制数据库查询结果,粘贴直接生成insert into 语句

    遇到一个场景,需要将数据库查询的结果导入到另外一个数据库中,给我的第一感受是,写程序,从数据库A中获取到数据,在插入到数据库B中. 但是Idea 可以直接复制查询结果,然后粘贴生成insert语句. ...

  4. DBGRID控件里可以实现SHIFT复选吗?怎么设置?

    //////////////////////////////////////////////////    功能概述:公用的列表框选择框,是用DBGrid网格////    注意事项:希望用Query ...

  5. 【转】史上最浅显易懂的Git教程!

    之前一直在找git的学习教程,网上搜到很多,但是大多数写的都非常简单或者混乱,你知道技术男的思维就是以为他抛一个专业术语出来,以为你都懂……或者简单写两句,插个图,他觉得他懂了,你也能懂,事实上初学者 ...

  6. 多realm以及jdbcRealm配置

    多realm配置 public class MyRealm1 implements Realm { public String getName() { return "myrealm1&qu ...

  7. Contest 7

    A:搜索好难啊根本不会啊. B:原题都能写挂没救了啊.考虑求出每个数作为最小值时能向左向右扩展到的最远位置,那么这段区间里的所有数就不可能作为唯一的最小值成为最优解了,否则假设可以的话这段区间里的数都 ...

  8. 【JavaScript】事件

    一.前言         继续上一章的内容,继续今天的Js学习. 二.内容         事件处理程序 事件就是用户或浏览器自身执行的某种动作.而响应某个事件的函数就叫做事件处理程序 //HTML事 ...

  9. Kerberos的白银票据详解

    0x01白银票据(Silver Tickets)定义 白银票据(Silver Tickets)是伪造Kerberos票证授予服务(TGS)的票也称为服务票据.如下图所示,与域控制器没有AS-REQ 和 ...

  10. Zend Hash table 详解--转

    原文地址:http://www.phppan.com/2009/12/zend-hashtable/ 在PHP的Zend引擎中,有一个数据结构非常重要,它无处不在,是PHP数据存储的核心,各种常量.变 ...