题目链接: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. CentOS 6.5安装配置LAMP服务器(Apache+PHP5+MySQL)

    1.配置防火墙,开启80端口.3306端口vi /etc/sysconfig/iptables-A INPUT -m state --state NEW -m tcp -p tcp --dport 8 ...

  2. web.py 笔记

    1.涉及到id=‘id’的情况,需要加入  vars=locals()  ,因为id在python里有id() 函数 db.delete('entries', where = 'id = $id', ...

  3. react-自定义事件

    没有嵌套关系的组件(如兄弟组件)之间的通信,只能通过自定义事件的方式来进行. var EventEmitter = require('events').EventEmitter; import Rea ...

  4. 转发---[沧海拾遗]java并发之CountDownLatch、Semaphore和CyclicBarrier

    JAVA并发包中有三个类用于同步一批线程的行为,分别是CountDownLatch.Semaphore和CyclicBarrier. CountDownLatch CountDownLatch是一个计 ...

  5. php获取字符串的编码格式的方法(函数)

    // 检测字符的编码格式 $encode = mb_detect_encoding($string, array('ASCII','UTF-8','GB2312','GBK','BIG5')); ec ...

  6. BSGS和扩展BSGS

    BSGS: 求合法的\(x\)使得\(a ^ x \quad mod \quad p = b\) 先暴力预处理出\(a^0,a^1,a^2.....a^{\sqrt{p}}\) 然后把这些都存在map ...

  7. String和stringbuffer和stringbuilder的区别

    String 字符串常量 StringBuffer 字符串变量(线程安全) StringBuilder 字符串变量(非线程安全) 简要的说, String 类型和 StringBuffer 类型的主要 ...

  8. JS中如何使用EL表达式中的对象

    JS中如何使用EL表达式中的对象 2017年09月25日 15:33:09 lhpnba 阅读数:4859   1.js中使用el表达式要加双引号或单引号:'${list}' 2.js变量获取el表达 ...

  9. stout代码分析之一:Duration类

    Duration类用于表示时间长度,可精确到纳秒. 代码实现在duration.hpp中,测试代码:duration_tests.cpp 相关api如下: parse, 将字符串转化成Duration ...

  10. C++实现人员信息管理系统模拟

    利用C++语言实现基本的学生信息管理系统: 要求: 1-设置管理员密码 2-人员数据有:姓名,性别等基本的信息 3-可以添加,删除,保存,统计 #include<iostream> #in ...